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
210 lines (192 loc) · 7.01 KB
/
Copy pathbackground.js
File metadata and controls
210 lines (192 loc) · 7.01 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
var background = (function() {
var mod = {};
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: ""
});
}
};
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");
controlBackgroundImageFileFeedback.removeEventListener("animationend", bind.feedback.animation.reset, false);
}
}
};
var render = {};
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(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:" + state.get().background.image.file.name);
controlBackgroundImageFileFeedback.appendChild(para1);
controlBackgroundImageFileFeedback.appendChild(para2);
bind.feedback.animation.set("is-pop", action);
},
clear: function() {
var controlBackgroundImageFileFeedback = helper.e(".control-background-image-file-feedback");
while (controlBackgroundImageFileFeedback.lastChild) {
controlBackgroundImageFileFeedback.removeChild(controlBackgroundImageFileFeedback.lastChild);
};
},
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 importData = 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);
};
};
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") {
helper.setObject({
object: state.get(),
path: "background.image.file.name",
newValue: fileList[0].name
});
helper.setObject({
object: state.get(),
path: "background.image.file.data",
newValue: event.target.result
});
data.save();;
render.feedback.clear();
render.feedback.success(render.image);
render.input.clear();
} else {
render.feedback.clear();
render.feedback.fail.filetype(fileList[0].name);
render.input.clear();
};
} else {
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.image();
render.blur();
render.grayscale();
render.opacity();
render.scale();
render.accent();
render.feedback.init();
};
// exposed methods
return {
init: init,
mod: mod,
bind: bind,
render: render,
importData: importData
};
})();