-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminCustomFiles.js
More file actions
200 lines (178 loc) · 5.17 KB
/
Copy pathAdminCustomFiles.js
File metadata and controls
200 lines (178 loc) · 5.17 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
/**
* Admin Custom Files (ProcessWire 3.x)
*
* This module enables you to add custom scripts and files to the admin.
*
* Admin Custom Files
* Copyright (C) 2016 by Martijn Geerts
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
*/
var AdminCustomFiles = (function () {
"use strict";
var func = {};
var obj = {};
var el = {};
func.tableMarkup = function () {
var options = "";
$("#Inputfield_processes option").each(function () {
options += "<option value='" + this.value + "'>" +
this.innerHTML +
"</option>";
});
return "<table id='acfTable' class='acfTable'>" +
"<tr>" +
"<th>" + obj.text.headers.select + "</th>" +
"<th id='acfHeader'>" + obj.text.headers.file + "</th>" +
"<th></th>" +
"</tr>" +
"<tr>" +
"<td>" +
"<select class='uk-select' id='acfSelect'>" +
"<option></option>" +
options +
"</select>" +
"</td>" +
"<td>" +
"<input class='uk-input acfInput' id='acfInput'>" +
"</td>" +
"<td>" +
"<button class='ui-button ui-widget acfButton' id='acfButton'>" +
"<i class='fa fa-plus'></i>" +
"</button>" +
"</td>" +
"</tr>" +
"</table>";
};
func.listMarkup = function () {
return "<ul class='files asmList asmListSortable' id='acfList'></ul>";
};
func.itemMarkup = function (process, file) {
obj.itemWidth = obj.itemWidth || $("td:first", el.table).width();
return "<li class='asmListItem ui-state-default' data-item='" + JSON.stringify({"process": process, "file": file}) + "'>" +
"<i class='fa fa-fw fa-arrows asmListItemHandle'></i>" +
"<div class='asmListItemLabel'>" +
"<span class='acfLabelProcess' style='width:" + obj.itemWidth + "px'>" + process + "</span>" +
"<span class='acfLabelFile'>" + file + "</span>" +
"</div>" +
"<a href='#' class='asmListItemRemove acfDelete'>" +
"<i class='fa fa-trash'></i>" +
"</a>" +
"</li>";
};
func.setData = function () {
var data = [];
$("li", el.list).each(function () {
data.push($(this).data("item"));
});
el.input.value = JSON.stringify(data);
};
func.itemExists = function (url) {
var exists = false;
$.each(JSON.parse(el.input.value), function () {
if (this.file === url && el.select.value === this.process) {
exists = true;
return;
}
if (!this.process && this.file === url) {
exists = true;
return;
}
});
return exists;
};
func.getUrl = function () {
return el.acfInput.value.indexOf("/") !== 0
? el.input.dataset.path + el.acfInput.value
: el.acfInput.value;
};
func.addItem = function (process, file) {
el.select.value = "";
el.acfInput.value = "";
el.list.insertAdjacentHTML("beforeend", func.itemMarkup(process, file));
$(".acfDelete", el.list.lastChild).on("click", func.removeItem);
func.setData();
};
func.addItems = function () {
$(el.list).sortable({
revert: 50,
stop: func.setData,
axis: "y"
});
$.each((el.input.value ? JSON.parse(el.input.value) : []), function () {
func.addItem(this.process, this.file);
});
func.setData();
};
func.removeItem = function (event) {
event.preventDefault();
$(this).closest("li").remove();
func.setData();
};
func.showError = function (message) {
$(el.wrap).addClass("InputfieldStateError uk-alert-danger");
$(el.acfHeader).text(message);
setTimeout(function () {
$(el.wrap).removeClass("InputfieldStateError uk-alert-danger");
$(el.acfHeader).text(obj.text.headers.file);
$(el.button).removeClass("ui-state-active");
}, 1000);
};
func.ajax = function(jqXHR) {
if (jqXHR.status === 200) {
if (el.acfInput.value.indexOf(".js") > -1 || el.acfInput.value.indexOf(".css") > -1) {
func.addItem(el.select.value, func.getUrl());
func.setData();
} else {
func.showError(obj.text.messages.invalid);
}
} else {
func.showError(obj.text.messages.notfound);
}
};
func.buttonClick = function (event) {
event.preventDefault();
var url = func.getUrl();
var exists = func.itemExists(url);
if (el.acfInput.value && !exists) {
$.ajax({
type: "HEAD",
url: url,
async: true,
complete: func.ajax
});
} else if (exists) {
func.showError(obj.text.messages.exists);
} else {
setTimeout(function () {
$(el.button).removeClass("ui-state-active");
}, 300);
}
};
func.init = function () {
el.wrap = document.getElementById("wrap_Inputfield_dependencies");
el.input = document.getElementById("Inputfield_dependencies");
obj.text = $(el.input).data();
el.input.insertAdjacentHTML("beforebegin", func.listMarkup());
el.input.insertAdjacentHTML("beforebegin", func.tableMarkup()); // including set obj.text
el.list = document.getElementById("acfList");
el.table = document.getElementById("acfTable");
el.acfHeader = document.getElementById("acfHeader");
el.select = document.getElementById("acfSelect");
el.acfInput = document.getElementById("acfInput");
el.button = document.getElementById("acfButton");
func.addItems();
el.button.addEventListener("click", func.buttonClick);
$(el.button).css({
"height": $(el.acfInput).outerHeight() + "px",
"line-height": $(el.acfInput).outerHeight() + "px"
});
};
return {
init: func.init
};
}());
$(function () {
"use strict";
AdminCustomFiles.init();
});