forked from L3au/prism-pretty
-
Notifications
You must be signed in to change notification settings - Fork 5
/
popup.js
127 lines (96 loc) · 2.9 KB
/
popup.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
var options, initOptions;
var form = $('form');
var lang = navigator.language.match('zh');
var template = tplEngine($('#template').html());
var background = chrome.extension.getBackgroundPage();
function getOptions() {
var json;
var disabledEls = $('[disabled]');
if (disabledEls.length > 0) {
disabledEls.attr('data-disabled', true);
disabledEls.removeAttr('disabled');
}
json = form.serializeJSON();
// restore
disabledEls.attr('disabled', '');
disabledEls.removeAttr('data-disabled');
return json;
}
function updateView() {
form.html(template.render($.extend(true, {
lang: lang
}, options)));
$('[data-toggle="radio"]').radiocheck();
$('[data-toggle="checkbox"]').radiocheck();
$('[data-toggle="switch"]').bootstrapSwitch();
changeBtnStatus();
}
function tplEngine(str) {
var strFunc = "var out = ''; out+=" + "'" +
str.replace(/[\r\t\n]/g, " ")
.replace(/'(?=[^}]*}})/g, "\t")
.split("'").join("\\'")
.split("\t").join("'")
.replace(/{{=(.+?)}}/g, "'; out += $1; out += '")
.split("{{").join("';")
.split("}}").join("out+='") + "'; return out;";
var fn = new Function("it", strFunc);
return {
render: function (data) {
return fn(data || {});
}
}
}
function changeBtnStatus() {
var curOptions = getOptions();
var submitBtn = $('[type=submit]');
if (JSON.stringify(initOptions) != JSON.stringify(curOptions)) {
submitBtn.addClass('btn-danger');
submitBtn.removeClass('btn-primary');
} else {
submitBtn.addClass('btn-primary');
submitBtn.removeClass('btn-danger');
}
}
$.extend($.serializeJSON.defaultOptions, {
parseAll : true,
checkboxUncheckedValue: 'false',
useIntKeysAsArrayIndex: true,
parseWithFunction : function (val, name) {
return val;
}
});
chrome.storage.sync.get(function (data) {
options = $.extend(true, {}, data);
updateView();
options = getOptions();
// compare with init options
initOptions = $.extend(true, {}, options);
changeBtnStatus();
$('html').addClass('popup-show');
$('.help').addClass('help-' + (lang ? 'zh' : 'en' ));
});
form.on('change', function () {
changeBtnStatus();
});
form.on('submit', function (e) {
e.preventDefault();
if (!$('[type=submit]').hasClass('btn-danger')) {
window.close();
return;
}
var options = getOptions();
background.setOptions(options);
window.close();
});
form.on('switchChange.bootstrapSwitch', function (event, state) {
var target = $(event.target);
changeBtnStatus();
if (target.hasClass('pretty-enabled')) {
options = getOptions();
setTimeout(function () {
options.enabled = state;
updateView();
}, 250);
}
});