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