forked from svn2github/AniDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddepm.js
260 lines (224 loc) · 6.24 KB
/
addepm.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
jsVersionArray.push({
"file":"addepm.js",
"version":"1.0",
"revision":"$Revision$",
"date":"$Date:: $",
"author":"$Author$",
"changelog":"Initial version"
});
function main_addepm() {
makeElement = (document.body ? (function (n) // would be just document.createElement but foxy throws
{
return document.createElement(n);
}) : (function (n)
{
return document.createElementNS("http://www.w3.org/1999/xhtml", n);
})
);
my_counter = -1;
my_form = document.getElementById('addepm.form');
if ( my_form ){
removeWS(my_form);
my_template = document.getElementById('e0');
if(!my_template)
return;
//this user has add rights
my_end = my_template.nextSibling;
my_form.removeChild(my_template);
my_eps = [];
for (var i = 0, n = my_form.firstChild, last; n; i++) {
var t = n.nextSibling;
if ( n.id && n.className == 'ep' ) {
var ep = new MyEp(n);
if(last){
last.next = ep;
ep.prev = last;
}
last =
my_eps[ep.id] = ep;
}
n = t;
}
my_form.onsubmit = do_submit;
}
};
function do_submit(){
var regex = /^[\x20-\x7E]{0,1000}$/;
for (var k in my_eps){
var ep = my_eps[k];
if(!ep.name.value){
alert("Ep "+ep.epno.value+": Main title can not be blank.");
return false;
}
if(!regex.test(ep.name.value)){
alert("Invalid: '"+ep.name.value+"'\nOnly printable 7-bit ASCII is allowed for main title. Max length is 1000.");
return false;
}
if(!regex.test(ep.romaji.value)){
alert("Invalid: '"+ep.romaji.value+"'\nOnly printable 7-bit ASCII is allowed for transcribed title. Max length is 1000.");
return false;
}
}
return true;
}
function removeWS(elem){
for (var i = 0, n = elem.firstChild; n; i++) {
var t = n.nextSibling;
if ( n.nodeType==3 )
elem.removeChild(n);
else
removeWS(n);
n = t;
}
}
function only7bit(n) {
var isascii = !/[^\x20-\x7E]/.test(n.value);
var ismarkederror = n.className.indexOf("g_error") >= 0;
if (isascii && ismarkederror) { n.className = n.className.replace(/\s*g_error/,""); }
else if (!isascii && !ismarkederror) { n.className += " g_error"; }
}
function onchange_check7bit() {
only7bit(this);
}
function MyEp(elem, id){
this.elem = elem;
this.id = id || elem.id.substring(1);
this.epno = elem.firstChild.firstChild;
var regexp = /^(s|c|t|p|o)(\d+)$/i;
if(regexp.test(this.epno.value)) {
this.type = RegExp.$1;
this.numb = RegExp.$2*1;
} else {
this.type = '';
this.numb = this.epno.value*1;
}
this.name = this.epno.nextSibling;
this.length = elem.firstChild.nextSibling.firstChild;
this.romaji = this.length.nextSibling;
this.aired = elem.firstChild.nextSibling.nextSibling.firstChild;
this.kanji = this.aired.nextSibling;
if(elem.firstChild.nextSibling.nextSibling.nextSibling)
this.extra = elem.firstChild.nextSibling.nextSibling.nextSibling.firstChild;
if(id<0){
this.epno.name = 'addepm.'+id+'.epno';
this.name.name = 'addepm.'+id+'.title4';
this.length.name = 'addepm.'+id+'.length';
this.romaji.name = 'addepm.'+id+'.title75';
this.aired.name = 'addepm.'+id+'.aired';
this.kanji.name = 'addepm.'+id+'.title2';
if ( this.extra ){
var x = this.extra.name.indexOf('.title');
this.extra.name = 'addepm.'+id+'.title'+this.extra.name.substring(x+6);
}
}
if(!this.epno.disabled){
this.elem.appendChild(mkButton(this.id, 'up', moveEpUp));
this.elem.appendChild(mkButton(this.id, 'down', moveEpDown));
}
if(my_template)
this.elem.appendChild(mkButton(this.id, 'new', newEp));
if(id<0)
this.elem.appendChild(mkButton(this.id, 'del', delEp));
this.setEpno = (
function(type,numb){
this.type = type;
this.numb = numb;
this.epno.value = type+numb;
}
);
only7bit(this.name);
only7bit(this.romaji);
this.name.onchange =
this.romaji.onchange = onchange_check7bit;
}
function mkButton(id, value, func) {
var butt = makeElement('input');
butt.id = id;
butt.type = 'button';
butt.value = value;
butt.onclick = func;
return butt;
}
function newEp(event){
var eid = this.id;
var cur = my_eps[eid];
var nex = cur.next;
if(nex && cur.type==nex.type && nex.epno.disabled)
return;
var ins = my_form.insertBefore(my_template.cloneNode(true), nex ? nex.elem : my_end );
var ep = my_eps[my_counter] = new MyEp(ins, my_counter--);
ep.setEpno(cur.type, cur.numb+1);
ep.name.value = 'Episode '+ep.epno.value;
ep.next = nex;
ep.prev = cur;
cur.next = ep;
if(nex) nex.prev = ep;
updateEpno(ep.next, ep.type, 1);
}
function delEp(event){
var eid = this.id;
var cur = my_eps[eid];
var pre = cur.prev;
var nex = cur.next;
if(pre) pre.next = cur.next;
if(nex) nex.prev = pre;
my_form.removeChild(cur.elem);
my_eps[eid] = null;
if(nex) updateEpno(nex, cur.type, -1);
}
function moveEpDown(event){
var eid = this.id;
var cur = my_eps[eid];
var pre = cur.prev;
var nex = cur.next;
if(!nex || cur.type!=nex.type || nex.epno.disabled) return;
if(pre) pre.next = nex;
var end = nex.next;
if(end) end.prev = cur;
nex.prev = pre;
nex.next = cur;
cur.prev = nex;
cur.next = end;
my_form.removeChild(cur.elem);
my_form.insertBefore(cur.elem, end ? end.elem : my_end);
var tmp = cur.numb;
cur.setEpno(nex.type, nex.numb);
nex.setEpno(nex.type, tmp);
}
function moveEpUp(event){
var eid = this.id;
var cur = my_eps[eid];
var pre = cur.prev;
var nex = cur.next;
if(!pre || cur.type!=pre.type || pre.epno.disabled) return;
if(nex) nex.prev = pre;
var beg = pre.prev;
if(beg) beg.next = cur;
pre.next = nex;
pre.prev = cur;
cur.prev = beg;
cur.next = pre;
my_form.removeChild(cur.elem);
my_form.insertBefore(cur.elem, pre.elem);
var tmp = cur.numb;
cur.setEpno(pre.type, pre.numb);
pre.setEpno(pre.type, tmp);
}
function parseEpNum(str,type,val) {
var num = (!type) ? Number(str) : Number(str.substr(1));
if (val) num += val;
return num;
}
function updateEpno(elem, type, val){
if(elem && elem.type==type){
var cur = parseEpNum(elem.epno.value,type);
var after = parseEpNum(elem.epno.value,type,val);
var prev = parseEpNum(elem.prev.epno.value,type);
var next = parseEpNum(elem.next.epno.value,type);
var tgt = (val > 0) ? next : prev;
if (!isNaN(tgt)) elem.setEpno(type, elem.numb+val);
if (isNaN(tgt) && ((after == next) || (cur == prev))) elem.setEpno(type, elem.numb+val);
updateEpno(elem.next, type, val);
}
}
addLoadEvent(main_addepm);