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