0b0a661e500b96cb1d976f6f5e5cfb9b90d94a5b
[KiwiIRC.git] / client / src / applets / settings.js
1 (function () {
2 var View = Backbone.View.extend({
3 events: {
4 'change [data-setting]': 'saveSettings',
5 'click [data-setting="theme"]': 'selectTheme',
6 'click .register_protocol': 'registerProtocol',
7 'click .enable_notifications': 'enableNotifications'
8 },
9
10 initialize: function (options) {
11 var text = {
12 tabs: _kiwi.global.i18n.translate('client_applets_settings_channelview_tabs').fetch(),
13 list: _kiwi.global.i18n.translate('client_applets_settings_channelview_list').fetch(),
14 large_amounts_of_chans: _kiwi.global.i18n.translate('client_applets_settings_channelview_list_notice').fetch(),
15 join_part: _kiwi.global.i18n.translate('client_applets_settings_notification_joinpart').fetch(),
16 count_all_activity: _kiwi.global.i18n.translate('client_applets_settings_notification_count_all_activity').fetch(),
17 timestamps: _kiwi.global.i18n.translate('client_applets_settings_timestamp').fetch(),
18 timestamp_24: _kiwi.global.i18n.translate('client_applets_settings_timestamp_24_hour').fetch(),
19 mute: _kiwi.global.i18n.translate('client_applets_settings_notification_sound').fetch(),
20 emoticons: _kiwi.global.i18n.translate('client_applets_settings_emoticons').fetch(),
21 scroll_history: _kiwi.global.i18n.translate('client_applets_settings_history_length').fetch(),
22 languages: _kiwi.app.translations,
23 default_client: _kiwi.global.i18n.translate('client_applets_settings_default_client').fetch(),
24 make_default: _kiwi.global.i18n.translate('client_applets_settings_default_client_enable').fetch(),
25 locale_restart_needed: _kiwi.global.i18n.translate('client_applets_settings_locale_restart_needed').fetch(),
26 default_note: _kiwi.global.i18n.translate('client_applets_settings_default_client_notice').fetch('<a href="chrome://settings/handlers">chrome://settings/handlers</a>'),
27 html5_notifications: _kiwi.global.i18n.translate('client_applets_settings_html5_notifications').fetch(),
28 enable_notifications: _kiwi.global.i18n.translate('client_applets_settings_enable_notifications').fetch(),
29 theme_thumbnails: _.map(_kiwi.app.themes, function (theme) {
30 return _.template($('#tmpl_theme_thumbnail').html().trim(), theme);
31 })
32 };
33 this.$el = $(_.template($('#tmpl_applet_settings').html().trim(), text));
34
35 if (!navigator.registerProtocolHandler) {
36 this.$el.find('.protocol_handler').remove();
37 }
38
39 if (!window.webkitNotifications) {
40 this.$el.find('notification_enabler').remove();
41 }
42
43 // Incase any settings change while we have this open, update them
44 _kiwi.global.settings.on('change', this.loadSettings, this);
45
46 // Now actually show the current settings
47 this.loadSettings();
48
49 },
50
51 loadSettings: function () {
52
53 var that = this;
54
55 $.each(_kiwi.global.settings.attributes, function(key, value) {
56
57 var $el = $('[data-setting="' + key + '"]', that.$el);
58
59 // Only deal with settings we have a UI element for
60 if (!$el.length)
61 return;
62
63 switch ($el.prop('type')) {
64 case 'checkbox':
65 $el.prop('checked', value);
66 break;
67 case 'radio':
68 $('[data-setting="' + key + '"][value="' + value + '"]', that.$el).prop('checked', true);
69 break;
70 case 'text':
71 $el.val(value);
72 break;
73 case 'select-one':
74 $('[value="' + value + '"]', that.$el).prop('selected', true);
75 break;
76 default:
77 $('[data-setting="' + key + '"][data-value="' + value + '"]', that.$el).addClass('active');
78 break;
79 }
80 });
81 },
82
83 saveSettings: function (event) {
84 var value,
85 settings = _kiwi.global.settings,
86 $setting = $(event.currentTarget, this.$el);
87
88 switch (event.currentTarget.type) {
89 case 'checkbox':
90 value = $setting.is(':checked');
91 break;
92 case 'radio':
93 case 'text':
94 value = $setting.val();
95 break;
96 case 'select-one':
97 value = $(event.currentTarget[$setting.prop('selectedIndex')]).val();
98 break;
99 default:
100 value = $setting.data('value');
101 break;
102 }
103
104 // Stop settings being updated while we're saving one by one
105 _kiwi.global.settings.off('change', this.loadSettings, this);
106 settings.set($setting.data('setting'), value);
107 settings.save();
108
109 // Continue listening for setting changes
110 _kiwi.global.settings.on('change', this.loadSettings, this);
111 },
112
113 selectTheme: function(event) {
114 event.preventDefault();
115
116 $('[data-setting="theme"].active', this.$el).removeClass('active');
117 $(event.currentTarget).addClass('active').trigger('change');
118 },
119
120 registerProtocol: function (event) {
121 event.preventDefault();
122
123 navigator.registerProtocolHandler('irc', document.location.origin + _kiwi.app.get('base_path') + '/%s', 'Kiwi IRC');
124 navigator.registerProtocolHandler('ircs', document.location.origin + _kiwi.app.get('base_path') + '/%s', 'Kiwi IRC');
125 },
126
127 enableNotifications: function(event){
128 event.preventDefault();
129
130 if ('webkitNotifications' in window) {
131 window.webkitNotifications.requestPermission();
132 } else if ('Notification' in window) {
133 Notification.requestPermission();
134 }
135 }
136
137 });
138
139
140 var Applet = Backbone.Model.extend({
141 initialize: function () {
142 this.set('title', _kiwi.global.i18n.translate('client_applets_settings_title').fetch());
143 this.view = new View();
144 }
145 });
146
147
148 _kiwi.model.Applet.register('kiwi_settings', Applet);
149 })();