This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.js
250 lines (242 loc) · 7.32 KB
/
settings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
(function(){
var _ = function(m)
{
if("babelfish" in window)
return window.babelfish.translate(m);
return m;
};
var settings = {
configuration: {},
defaults: {},
hook: function()
{
var button = $(
'<li id="userspace" class="nav-left__item">'+
'<i class="nav-left__item-icon fa fa-cogs" title="'+_('Userspace addons')+'"></i>'+
'</li>'
);
button.click(function(){ window.settings.toggle(); });
$('.nav--desktop .list-unstyled.extension').append(button);
window.freshy.systemLoaded('settings');
},
loadConfiguration: function()
{
if("localStorage" in window && window.localStorage != null)
{
if('Userspace-Config' in window.localStorage && window.localStorage['Userspace-Config'])
{
try
{
window.settings.configuration = JSON.parse(window.localStorage['Userspace-Config']);
}
catch(e)
{
console.log(e);
window.settings.configuration = {};
}
}
else
{
setTimeout(function(){ window.settings.open(); }, 5000); // Open settings on first time load
}
for(system in window.settings.defaults)
if(!(system in window.settings.configuration))
window.settings.configuration = JSON.parse(JSON.stringify(window.settings.defaults[system]));
}
else
window.freshy.waitFor('chatalert', function(){
window.chatAlert.showError(
_('Configuration unavailable'),
_('Your browser does not support local storage, unable to support persistent configuration!')
);
});
},
pushConfiguration: function(system)
{
window[system].configure(window.settings.configuration[system]);
},
saveConfiguration: function()
{
if("localStorage" in window && window.localStorage != null)
window.localStorage['Userspace-Config'] = JSON.stringify(window.settings.configuration);
},
setDefaults: function(system, defaults)
{
window.settings.defaults[system] = defaults;
if(!(system in window.settings.configuration))
window.settings.configuration[system] = JSON.parse(JSON.stringify(defaults));
for(setting in window.settings.defaults[system])
if(!(setting in window.settings.configuration[system]))
window.settings.configuration[system][setting] = defaults[setting];
window.settings.pushConfiguration(system);
},
toggle: function()
{
if($('#the-user-addons').length === 0)
window.settings.open();
else
window.settings.cleanup();
},
open: function()
{
for(var plugin in window.freshy.systems)
{
var n = 0;
if(!(plugin in window))
{
n++;
freshy.waitFor(plugin, function(){ n--; if(n == 0) window.settings.open(); });
}
if(n > 0)
return;
}
window.settings.cleanup();
var sections = $('<div class="container" style="height:85%;margin:2% 0 0 0;overflow:auto"></div>');
var side = {
left: $('<div style="float:left;width:49%;padding:1px"></div>'),
right: $('<div style="float:right;width:49%;padding:1px"></div>')
};
for(var plugin in window.freshy.systems)
if(plugin in window && "config" in window[plugin])
{
var config = window[plugin].config.get();
if(config && config.length > 0)
for(var i = 0; i < config.length; ++i)
side[config[i].type].append(window.settings.buildBlock(window[plugin], config[i]));
}
var view = $(
'<div id="the-user-addons" class="user-content" style="position:absolute;z-index: 20000; left:250px; right: 375px; top: 72px; bottom: 72px; background:rgba(0,0,0,0.8);">'+
'<div class="application section" style="height:100%">'+
'<div style="width:30px;height:30px;float:right;cursor:pointer" class="close-button"><i class="icon icon-x-white" /></div>'+
'<h1>'+_('Userspace addon settings')+'</h1>'+
'</div>'+
'</div>'
);
view.children('.section').append(sections.append(side.left, side.right));
$('body').append(view);
$('#the-user-addons .close-button').click(function(){window.settings.cleanup();});
},
buildBlock: function(plugin, config)
{
var block = $(
'<div style="padding:1px">'+
'<div class="header" style="margin:5px 0;"><span>'+config.title+'</span></div>'+
'</div>'
);
for(var i = 0; i < config.options.length; ++i)
block.append(window.settings.buildOption(plugin, config.options[i]));
return block;
},
buildOption: function(plugin, config)
{
if(config.type == 'select')
{
var select = $('<select>');
select.change(function(){ window.settings.optionChanged(this); });
select.data('plugin', plugin);
select.data('config', config);
for(var i = 0; i < config.options.length; ++i)
select.append($('<option value="'+config.options[i].value+'">'+config.options[i].label+'</option>'));
select.val(config.value);
return select;
}
if(config.type == 'custom')
return config.content;
if(config.type == 'numberbox')
{
var box = $('<input style="text-align:right" type="text" size="'+config.size+'" />');
box.keyup(function(){ window.settings.textChanged(this); });
box.data('plugin', plugin);
box.data('config', config);
box.val(config.value);
if("legend" in config)
return $('<span>'+config.legend+'</span>').prepend(box);
return box;
}
if(config.type == 'textbox')
{
var box = $('<input type="text" size="'+config.size+'" />');
box.keyup(function(){ window.settings.textChanged(this); });
box.data('plugin', plugin);
box.data('config', config);
box.val(config.value);
if("legend" in config)
return $('<span>'+config.legend+'</span>').prepend(box);
return box;
}
},
textChanged: function(element)
{
var box = $(element);
box.val(box.data('plugin').config.set(box.data('config'), box.val()));
},
optionChanged: function(element)
{
var select = $(element);
select.data('plugin').config.set(select.data('config'), select.val());
},
cleanup: function()
{
$('#the-user-addons').remove();
},
onChatCommand: function(command)
{
if(command == '/conf')
window.settings.open();
},
config:
{
values: { },
get: function()
{
return [
{
title: _('Reset configuration'),
type: 'right',
options: [
{ type: 'select', name: 'reset', value: 0, options: [{value:'0', label:''},{value:'userspace', label:_('Userspace settings')},{value:'localstorage', label:_('All settings')}] },
]
}
];
},
set: function(config, value)
{
if(config.name == 'reset')
{
if(value == 'userspace')
{
window.settings.configuration = {};
for(system in window.settings.defaults)
{
window.settings.configuration[system] = JSON.parse(JSON.stringify(window.settings.defaults[system]));
window.settings.pushConfiguration(system);
}
}
if(value == 'localstorage')
{
localStorage.clear();
window.location = window.location.href;
}
}
}
}
};
if("settings" in window)
window.settings.cleanup();
else
{
API.on(API.CHAT_COMMAND, function(e){window.settings.onChatCommand(e);});
window.freshy.waitFor('chatalert', function(){
window.freshy.waitFor('babelfish', function(){
window.chatalert.showInformation(
_('Userspace addons loaded'),
_('Type /conf to access user addon settings')
);
});
});
$('#user-menu .item').click(function(){window.settings.cleanup();});
}
window.settings = settings;
window.settings.loadConfiguration();
window.settings.hook();
})();