-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
147 lines (138 loc) · 4.56 KB
/
index.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
var url = "(image)";
var select = document.getElementsByClassName("image-picker")[0];
var view = document.getElementById("view");
// Load
function load() {
var directory = window.location.search.substr(Math.min(window.location.search.length, 1));
view.style.visibility = "hidden";
select.innerHTML = '';
var folders = {};
for (var i in images) {
var path = images[i];
if (path.startsWith(directory)) {
path = path.substr(directory.length);
var index = path.indexOf('/');
if (index == -1) {
add(directory + path);
} else {
var folder = path.substr(0, index);
if (folder in folders) {
folders[folder]++;
} else {
add(folder);
folders[folder] = 1;
}
}
}
}
select.selectedIndex = -1;
$("select").imagepicker({
show_label: true,
selected: function(select, option, event) {
var id = select.option[0].getAttribute("id");
if (id.indexOf('.') == -1) {
directory = id + "/";
history.pushState(history.state, directory, "?" + directory);
load();
} else {
url = id;
updateView();
}
}
});
}
window.onpopstate = load;
function add(item) {
var option = document.createElement("option");
option.setAttribute("id", item);
var image = "icons/folder.png";
if (item.indexOf('.') != -1) {
image = src_min + item;
item = item.substr(1 + item.lastIndexOf('/'));
var text = item.substr(0, item.lastIndexOf('.')).replace("_", " ").replace( /([A-Z])/g, " $1" );;
option.text = text;
} else {
option.text = item;
}
option.setAttribute("data-img-src", image);
select.add(option);
}
function copyToClipboard(elem) {
var disabled = elem.disabled;
elem.disabled = false;
// create hidden text element, if it doesn't already exist
var targetId = "_hiddenCopyText_";
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
var origSelectionStart, origSelectionEnd;
if (isInput) {
// can just use the original source element for the selection and copy
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
} else {
// must use a temporary form element for the selection and copy
target = document.getElementById(targetId);
if (!target) {
var target = document.createElement("textarea");
target.style.position = "absolute";
target.style.left = "-9999px";
target.style.top = "0";
target.id = targetId;
document.body.appendChild(target);
}
target.textContent = elem.textContent;
}
// select the content
var currentFocus = document.activeElement;
target.focus();
target.setSelectionRange(0, target.value.length);
// copy the selection
var succeed;
try {
succeed = document.execCommand("copy");
} catch(e) {
succeed = false;
}
// restore original focus
if (currentFocus && typeof currentFocus.focus === "function") {
currentFocus.focus();
}
if (isInput) {
// restore prior selection
elem.setSelectionRange(origSelectionStart, origSelectionEnd);
} else {
// clear temporary content
target.textContent = "";
}
elem.disabled = disabled;
var alert = document.getElementById("alert");
alert.hidden = false;
var newone = alert.cloneNode(true);
alert.parentNode.replaceChild(newone, alert);
return succeed;
}
function updateView() {
var input = document.getElementById("command");
var output = document.getElementById("output");
var dest = src_local + url;
if (!src_local.startsWith("file://")) dest = encodeURI(dest);
else if (dest.indexOf(' ') != -1) dest = '"' + dest + '"';
output.value = input.value.replace("%image%", dest);
copyToClipboard(output);
view.style.visibility = "visible";
view.setAttribute("src", src_max + url);
if (typeof variable !== 'undefined' && prevent_copy) {
$('img').on({
"contextmenu": function (e) {
e.preventDefault();
},
"mousedown": function (e) {
e.preventDefault();
},
"mouseup": function (e) {
e.preventDefault();
}
});
}
}
load();