forked from zombieFox/nightTab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
235 lines (215 loc) · 7.71 KB
/
Copy pathbackground.js
File metadata and controls
235 lines (215 loc) · 7.71 KB
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
var background = (function() {
var mod = {};
mod.import = function() {
// get files from input
var fileList = helper.e(".control-background-image-file").files;
// if file was added
if (fileList.length > 0) {
// validate the file
_validateImageFile(fileList);
};
};
mod.clear = {
file: function() {
helper.setObject({
object: state.get(),
path: "background.image.file.name",
newValue: ""
});
helper.setObject({
object: state.get(),
path: "background.image.file.data",
newValue: ""
});
}
};
mod.image = {
file: function(name, data) {
helper.setObject({
object: state.get(),
path: "background.image.file.name",
newValue: name
});
helper.setObject({
object: state.get(),
path: "background.image.file.data",
newValue: data
});
}
};
var bind = {};
bind.feedback = {
animation: {
set: function(animationClass, action) {
var controlBackgroundImageFileFeedback = helper.e(".control-background-image-file-feedback");
helper.addClass(controlBackgroundImageFileFeedback, animationClass);
var animationEndAction = function() {
if (action) {
action();
};
bind.feedback.animation.reset();
};
controlBackgroundImageFileFeedback.addEventListener("animationend", animationEndAction, false);
},
reset: function() {
var controlBackgroundImageFileFeedback = helper.e(".control-background-image-file-feedback");
helper.removeClass(controlBackgroundImageFileFeedback, "is-shake");
helper.removeClass(controlBackgroundImageFileFeedback, "is-pop");
helper.removeClass(controlBackgroundImageFileFeedback, "is-jello");
controlBackgroundImageFileFeedback.removeEventListener("animationend", bind.feedback.animation.reset, false);
}
}
};
var render = {};
render.color = function() {
var html = helper.e("html");
html.style.setProperty("--background-color-custom", "rgb(" + state.get().background.color.custom.r + ", " + state.get().background.color.custom.g + ", " + state.get().background.color.custom.b + ")");
};
render.image = function() {
var html = helper.e("html");
if (state.get().background.image.show) {
if (state.get().background.image.from == "file") {
html.style.setProperty("--background-image", "url(" + state.get().background.image.file.data + ")");
} else if (state.get().background.image.from == "url") {
html.style.setProperty("--background-image", "url(" + state.get().background.image.url + ")");
};
} else {
html.style.setProperty("--background-image", "url()");
};
};
render.blur = function() {
var html = helper.e("html");
html.style.setProperty("--background-blur", state.get().background.image.blur + "px");
};
render.grayscale = function() {
var html = helper.e("html");
html.style.setProperty("--background-grayscale", state.get().background.image.grayscale);
};
render.opacity = function() {
var html = helper.e("html");
html.style.setProperty("--background-opacity", state.get().background.image.opacity);
};
render.scale = function() {
var html = helper.e("html");
html.style.setProperty("--background-scale", state.get().background.image.scale);
};
render.accent = function() {
var html = helper.e("html");
html.style.setProperty("--background-accent", state.get().background.image.accent);
};
render.input = {
clear: function() {
var input = helper.e(".control-background-image-file");
input.value = "";
}
};
render.feedback = {
init: function() {
if (state.get().background.image.file.name != "") {
render.feedback.current();
} else {
render.feedback.empty();
};
},
empty: function() {
var controlBackgroundImageFileFeedback = helper.e(".control-background-image-file-feedback");
var para1 = helper.node("p:No image selected.|class:muted small");
controlBackgroundImageFileFeedback.appendChild(para1);
},
current: function() {
var controlBackgroundImageFileFeedback = helper.e(".control-background-image-file-feedback");
var para1 = helper.node("p:Image loaded.|class:muted small");
var para2 = helper.node("p:" + state.get().background.image.file.name);
controlBackgroundImageFileFeedback.appendChild(para1);
controlBackgroundImageFileFeedback.appendChild(para2);
},
success: function(name, action) {
var controlBackgroundImageFileFeedback = helper.e(".control-background-image-file-feedback");
var para1 = helper.node("p:Success! Setting Background image.|class:muted small");
var para2 = helper.node("p:" + name);
controlBackgroundImageFileFeedback.appendChild(para1);
controlBackgroundImageFileFeedback.appendChild(para2);
if (action) {
bind.feedback.animation.set("is-pop", action);
};
},
clear: function(override) {
var options = {
animate: null
};
if (override) {
options = helper.applyOptions(options, override);
};
var controlBackgroundImageFileFeedback = helper.e(".control-background-image-file-feedback");
while (controlBackgroundImageFileFeedback.lastChild) {
controlBackgroundImageFileFeedback.removeChild(controlBackgroundImageFileFeedback.lastChild);
};
if (options.animate) {
bind.feedback.animation.set("is-jello");
};
},
fail: {
filetype: function(name) {
var controlBackgroundImageFileFeedback = helper.e(".control-background-image-file-feedback");
var para1 = helper.node("p:Not the right kind of file. Make sure the selected file is an image.|class:small muted");
var para2 = helper.node("p:" + name);
controlBackgroundImageFileFeedback.appendChild(para1);
controlBackgroundImageFileFeedback.appendChild(para2);
bind.feedback.animation.set("is-shake");
},
size: function(name) {
var controlBackgroundImageFileFeedback = helper.e(".control-background-image-file-feedback");
var para1 = helper.node("p:File size is too big. Max file size of 5MB.|class:small muted");
var para2 = helper.node("p:" + name);
controlBackgroundImageFileFeedback.appendChild(para1);
controlBackgroundImageFileFeedback.appendChild(para2);
bind.feedback.animation.set("is-shake");
}
}
};
var _validateImageFile = function(fileList) {
// make new file reader
var reader = new FileReader();
// define the on load event for the reader
reader.onload = function(event) {
if (fileList.item(0).size <= 5000000) {
if (fileList.item(0).type.split("/")[0] == "image") {
mod.image.file(fileList[0].name, event.target.result);
data.save();
render.feedback.clear();
render.feedback.success(fileList[0].name, render.image);
render.input.clear();
} else {
// not an image file
render.feedback.clear();
render.feedback.fail.filetype(fileList[0].name);
render.input.clear();
};
} else {
// file size too big
render.feedback.clear();
render.feedback.fail.size(fileList[0].name);
render.input.clear();
};
};
// invoke the reader
reader.readAsDataurl(fileList.item(0));
};
var init = function() {
render.color();
render.image();
render.blur();
render.grayscale();
render.opacity();
render.scale();
render.accent();
render.feedback.init();
};
// exposed methods
return {
init: init,
mod: mod,
render: render,
bind: bind
};
})();