-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathMenuArea.js
More file actions
428 lines (401 loc) · 12.9 KB
/
Copy pathMenuArea.js
File metadata and controls
428 lines (401 loc) · 12.9 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// MenuArea.js v3.4.0
/* global getEL:readonly, _doc:readonly */
// メニュー描画/取得/html表示系
ui.menuarea = {
captions: [], // 言語指定を切り替えた際のキャプションを保持する
menuitem: null, // メニューの設定切り替え用エレメント等を保持する
nohover: false, // :hover擬似クラスを使用しないでhover表示する
//---------------------------------------------------------------------------
// menuarea.reset() メニュー、サブメニュー、フロートメニューの初期設定を行う
//---------------------------------------------------------------------------
reset: function() {
this.createMenu();
this.display();
},
//---------------------------------------------------------------------------
// menuarea.createMenu() メニューの初期設定を行う
//---------------------------------------------------------------------------
createMenu: function() {
if (this.menuitem === null) {
this.modifySelector();
this.menuitem = {};
this.walkElement(getEL("menupanel"));
}
ui.misc.displayByPid(getEL("menupanel"));
this.stopHovering();
},
//---------------------------------------------------------------------------
// menuarea.walkElement() エレメントを探索して領域の初期設定を行う
//---------------------------------------------------------------------------
walkElement: function(parent) {
var menuarea = this;
function menufactory(role) {
return function(e) {
menuarea[role](e);
if (menuarea.nohover) {
e.preventDefault();
e.stopPropagation();
}
};
}
function addmenuevent(el, type, role) {
var func = typeof role === "function" ? role : menufactory(role);
pzpr.util.addEvent(el, type, menuarea, func);
}
ui.misc.walker(parent, function(el) {
if (el.nodeType === 1 && el.nodeName === "LI") {
var setevent = false;
var idname = ui.customAttr(el, "config");
if (!!idname) {
menuarea.menuitem[idname] = { el: el };
if (el.className === "check") {
addmenuevent(el, "mousedown", "checkclick");
setevent = true;
}
}
var value = ui.customAttr(el, "value");
if (!!value) {
var parent = el.parentNode.parentNode;
idname = ui.customAttr(parent, "config");
var item = menuarea.menuitem[idname];
if (!item.children) {
item.children = [];
}
item.children.push(el);
addmenuevent(el, "mousedown", "childclick");
setevent = true;
}
var role = ui.customAttr(el, "menuExec");
if (!!role) {
addmenuevent(el, "mousedown", role);
setevent = true;
}
role = ui.customAttr(el, "pressExec");
if (!!role) {
var roles = role.split(/,/);
addmenuevent(el, "mousedown", roles[0]);
if (!!role[1]) {
addmenuevent(el, "mouseup", roles[1]);
addmenuevent(el, "mouseleave", roles[1]);
addmenuevent(el, "touchcancel", roles[1]);
}
setevent = true;
}
role = ui.customAttr(el, "popup");
if (!!role) {
addmenuevent(el, "mousedown", "disppopup");
setevent = true;
}
if (el.className === "link") {
addmenuevent(el, "mousedown", "updatelink");
setevent = true; // bypass setting event below
}
if (!setevent) {
if (!menuarea.nohover || !el.querySelector("menu")) {
addmenuevent(el, "mousedown", function(e) {
e.preventDefault();
});
} else {
addmenuevent(el, "mousedown", function(e) {
menuarea.showHovering(e, el);
e.preventDefault();
e.stopPropagation();
});
}
}
var link = ui.customAttr(el, "pidlink");
if (!!link) {
el.firstChild.setAttribute("href", link + ui.puzzle.pid);
}
} else if (el.nodeType === 1 && el.nodeName === "MENU") {
var label = el.getAttribute("label");
if (!!label && label.match(/^__(.+)__(.+)__$/)) {
menuarea.captions.push({
menu: el,
str_jp: RegExp.$1,
str_en: RegExp.$2
});
if (menuarea.nohover) {
addmenuevent(el, "mousedown", function(e) {
e.stopPropagation();
});
}
}
} else if (el.nodeType === 3) {
if (el.data.match(/^__(.+)__(.+)__$/)) {
menuarea.captions.push({
textnode: el,
str_jp: RegExp.$1,
str_en: RegExp.$2
});
}
}
});
},
//--------------------------------------------------------------------------------
// menuarea.modifySelector() MenuAreaに関するCSSセレクタテキストを変更する (Android向け)
//--------------------------------------------------------------------------------
modifySelector: function() {
/* Android 4.0, iOS5.1以上向け処理です */
if (!pzpr.env.OS.mobile || !getEL("menupanel").classList) {
return;
}
var sheet = _doc.styleSheets[0];
var rules = sheet.cssRules || sheet.rules;
if (rules === null) {
} // Chromeでローカルファイルを開くとおかしくなるので、とりあえず何もしないようにします
for (var i = 0, len = rules.length; i < len; i++) {
var rule = rules[i];
if (!rule.selectorText) {
continue;
}
if (rule.selectorText.match(/\#menupanel.+\:hover.*/)) {
sheet.insertRule(rule.cssText.replace(":hover", ".hovering"), i);
sheet.deleteRule(i + 1);
}
}
this.nohover = true;
},
//--------------------------------------------------------------------------------
// menuarea.showHovering() MenuAreaのポップアップを表示する (Android向け)
// menuarea.stopHovering() MenuAreaのポップアップを消去する (Android向け)
//--------------------------------------------------------------------------------
showHovering: function(e, el0) {
if (!this.nohover) {
return;
}
el0.classList.toggle("hovering");
ui.misc.walker(getEL("menupanel"), function(el) {
if (el.nodeType === 1 && !!el.classList && !el.contains(el0)) {
el.classList.remove("hovering");
}
});
},
stopHovering: function() {
if (!this.nohover) {
return;
}
ui.misc.walker(getEL("menupanel"), function(el) {
if (el.nodeType === 1 && !!el.classList) {
el.classList.remove("hovering");
}
});
},
//---------------------------------------------------------------------------
// menuarea.display() 全てのメニューに対して文字列を設定する
// menuarea.setdisplay() サブメニューに表示する文字列を個別に設定する
//---------------------------------------------------------------------------
display: function() {
getEL("menupanel").style.display = "";
getEL("menu_imagesave").className = ui.enableSaveImage ? "" : "disabled";
var EDITOR = !ui.puzzle.playeronly;
getEL("menu_edit").style.display = EDITOR ? "" : "none";
getEL("menu_newboard").style.display = EDITOR ? "" : "none";
getEL("menu_urloutput").style.display = EDITOR ? "" : "none";
getEL("menu_adjust").style.display = EDITOR ? "" : "none";
getEL("menu_turnflip").style.display = EDITOR ? "" : "none";
for (var idname in this.menuitem) {
this.setdisplay(idname);
}
this.setdisplay("operation");
this.setdisplay("trialmode");
this.setdisplay("toolarea");
/* キャプションの設定 */
for (var i = 0; i < this.captions.length; i++) {
var obj = this.captions[i];
if (!!obj.textnode) {
obj.textnode.data = ui.selectStr(obj.str_jp, obj.str_en);
} else if (!!obj.menu) {
obj.menu.setAttribute("label", ui.selectStr(obj.str_jp, obj.str_en));
}
}
},
setdisplay: function(idname) {
if (idname === "trialmode") {
var trial = ui.puzzle.board.trialstage > 0;
getEL("menu_adjust").className = trial ? "disabled" : "";
getEL("menu_turnflip").className = trial ? "disabled" : "";
}
if (idname === "toolarea") {
var str;
if (!ui.menuconfig.get("toolarea")) {
str = ui.selectStr("ツールエリアを表示", "Show tool area");
} else {
str = ui.selectStr("ツールエリアを隠す", "Hide tool area");
}
getEL("menu_toolarea").textContent = str;
} else if (this.menuitem === null || !this.menuitem[idname]) {
/* DO NOTHING */
} else if (ui.menuconfig.valid(idname)) {
var menuitem = this.menuitem[idname];
menuitem.el.style.display = "";
/* セレクタ部の設定を行う */
if (!!menuitem.children) {
var children = menuitem.children;
var validval =
idname === "inputmode" ? ui.puzzle.mouse.getInputModeList() : null;
for (var i = 0; i < children.length; i++) {
var child = children[i],
value = ui.customAttr(child, "value"),
selected = value === "" + ui.menuconfig.get(idname);
child.className = selected ? "checked" : "";
child.style.display =
validval === null || validval.indexOf(value) >= 0 ? "" : "none";
}
} else if (!!menuitem.el) {
/* Check部の表記の変更 */
var cname = ui.menuconfig.get(idname) ? "checked" : "check";
var disabled = null;
if (ui.puzzle.config.getvariant(idname)) {
disabled = !ui.puzzle.editmode;
}
if (disabled === true) {
cname += " disabled";
}
menuitem.el.className = cname;
}
} else if (!!this.menuitem[idname]) {
this.menuitem[idname].el.style.display = "none";
}
},
//---------------------------------------------------------------------------
// menuarea.checkclick() メニューから設定値の入力があった時、設定を変更する
// menuarea.childclick() メニューから設定値の入力があった時、設定を変更する
//---------------------------------------------------------------------------
checkclick: function(e) {
var el = e.target;
if (el.nodeName === "SPAN") {
el = el.parentNode;
}
if (el.className.match(/disabled/)) {
return;
}
var idname = ui.customAttr(el, "config");
ui.menuconfig.set(idname, !ui.menuconfig.get(idname));
},
childclick: function(e) {
var el = e.target;
if (el.nodeName === "SPAN") {
el = el.parentNode;
}
var parent = el.parentNode.parentNode;
ui.menuconfig.set(
ui.customAttr(parent, "config"),
ui.customAttr(el, "value")
);
},
//---------------------------------------------------------------------------
// メニューがクリックされた時の動作を呼び出す
//---------------------------------------------------------------------------
// submenuから呼び出される関数たち
undo: function() {
ui.undotimer.startUndo();
},
undostop: function() {
ui.undotimer.stopUndo();
},
undoall: function() {
ui.puzzle.undoall();
},
redo: function() {
ui.undotimer.startRedo();
},
redostop: function() {
ui.undotimer.stopRedo();
},
redoall: function() {
ui.puzzle.redoall();
},
enterTrial: function() {
if (ui.puzzle.board.trialstage === 0) {
ui.puzzle.enterTrial();
}
},
enterFurtherTrial: function() {
ui.puzzle.enterTrial();
},
acceptTrial: function() {
ui.puzzle.acceptTrial();
},
rejectTrial: function() {
ui.puzzle.rejectTrial();
},
rejectCurrentTrial: function() {
ui.puzzle.rejectCurrentTrial();
},
toolarea: function() {
ui.menuconfig.set("toolarea", !ui.menuconfig.get("toolarea"));
ui.displayAll();
},
disppopup: function(e) {
var el = e.target;
if (el.nodeName === "SPAN") {
el = el.parentNode;
}
if (el.className !== "disabled") {
var idname = ui.customAttr(el, "popup");
if (!pzpr.env.OS.mobile) {
var pos = pzpr.util.getPagePos(e);
ui.popupmgr.open(idname, pos.px - 8, pos.py - 8);
} else {
var rect = pzpr.util.getRect(getEL("menupanel"));
ui.popupmgr.open(idname, 8, rect.bottom + 8);
}
this.stopHovering();
}
},
updatelink: function(e) {
var el = e.target;
var linktype = ui.customAttr(el.parentNode, "linktype");
switch (linktype) {
case "duplicate":
var url = ui.puzzle.getURL(
pzpr.parser.URL_PZPRFILE,
ui.puzzle.playeronly ? "player" : "editor"
);
el.setAttribute("href", url);
break;
}
},
//------------------------------------------------------------------------------
// menuarea.answercheck()「正答判定」ボタンを押したときの処理
// menuarea.answerclear() 「解答消去」ボタンを押したときの処理
// menuarea.submarkclear() 「補助消去」ボタンを押したときの処理
//------------------------------------------------------------------------------
answercheck: function() {
var check = ui.puzzle.check(true);
if (check.complete) {
ui.timer.stop();
if (ui.callbackComplete) {
ui.callbackComplete(ui.puzzle, check);
}
}
var str = "",
texts = check.text.split(/\n/);
for (var i = 0; i < texts.length; i++) {
str += '<div style="margin-bottom:6pt;">' + texts[i] + "</div>";
}
this.stopHovering();
ui.notify.alert(str);
},
answerclear: function() {
this.stopHovering();
ui.notify.confirm(
"解答を消去しますか?",
"Do you want to erase the answer?",
function() {
ui.puzzle.ansclear();
}
);
},
submarkclear: function() {
this.stopHovering();
ui.notify.confirm(
"補助記号を消去しますか?",
"Do you want to erase the auxiliary marks?",
function() {
ui.puzzle.subclear();
}
);
}
};