-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathutil.js
More file actions
209 lines (196 loc) · 6.26 KB
/
Copy pathutil.js
File metadata and controls
209 lines (196 loc) · 6.26 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
// util.js v3.4.0
(function() {
var api = pzpr.env.API,
eventMouseDown = ["mousedown"],
eventMouseMove = ["mousemove"],
eventMouseUp = ["mouseup"],
eventMouseCancel = [""];
if (pzpr.env.bz.AndroidBrowser) {
eventMouseDown = [""];
eventMouseMove = [""];
eventMouseUp = [""];
}
if (api.pointerevent) {
eventMouseDown = ["pointerdown"];
eventMouseMove = ["pointermove"];
eventMouseUp = ["pointerup"];
eventMouseCancel = ["pointercancel"];
} else if (api.mspointerevent) {
eventMouseDown = ["MSPointerDown"];
eventMouseMove = ["MSPointerMove"];
eventMouseUp = ["MSPointerUp"];
eventMouseCancel = ["MSPointerCancel"];
} else if (api.touchevent) {
eventMouseDown.push("touchstart");
eventMouseMove.push("touchmove");
eventMouseUp.push("touchend");
eventMouseCancel.push("touchcancel");
}
//----------------------------------------------------------------------
// EventやDOM関連のツール的関数群
//----------------------------------------------------------------------
pzpr.util = {
//---------------------------------------------------------------
// pzpr.jsが読み込まれているスクリプトのパスを取得する
getpath: function() {
if (pzpr.env.browser) {
var srcs = document.getElementsByTagName("script");
for (var i = 0; i < srcs.length; i++) {
var result = srcs[i].src.match(/^(.*\/)pzpr\.js(?:\?.*)?$/);
if (result) {
return result[1] + (!result[1].match(/\/$/) ? "/" : "");
}
}
} else {
return require("path").dirname(__filename) + "/";
}
return "";
},
//---------------------------------------------------------------
// 現在の時間を取得
currentTime: function() {
return new Date().getTime();
},
//---------------------------------------------------------------
// Elementの生成関連
//---------------------------------------------------------------
unselectable: function(el) {
el.style.MozUserSelect = "none";
el.style.KhtmlUserSelect = "none";
el.style.webkitUserSelect = "none";
el.style.msUserSelect = "none";
el.style.userSelect = "none";
el.unselectable = "on";
return this;
},
//----------------------------------------------------------------------
// pzpr.util.addEvent() addEventListener()を呼び出す
//----------------------------------------------------------------------
addEvent: function(el, type, self, callback, capt) {
var types = [type];
if (type === "mousedown") {
types = eventMouseDown;
} else if (type === "mousemove") {
types = eventMouseMove;
} else if (type === "mouseup") {
types = eventMouseUp;
} else if (type === "mousecancel") {
types = eventMouseCancel;
}
function executer(e) {
callback.call(self, e);
}
types.forEach(function(type) {
el.addEventListener(type, executer, !!capt);
});
return function remover() {
types.forEach(function(type) {
el.removeEventListener(type, executer, !!capt);
});
};
},
//---------------------------------------------------------------------------
// pzpr.util.getMouseButton() 左/中/右ボタンが押されているかチェックする
//---------------------------------------------------------------------------
getMouseButton: function(e) {
if (e.touches !== void 0) {
/* touchイベントだった場合 */
return e.touches.length === 1 ? "left" : "";
} else if (!!e.pointerType && e.pointerType !== "mouse") {
/* pointerイベントだった場合 */
return e.isPrimary ? "left" : "";
}
return (
["left", "middle", "right"][
e.button !== void 0 ? e.button : e.which - 1
] || ""
);
},
//----------------------------------------------------------------------
// pzpr.util.getPagePos() イベントが起こったページ上の座標を返す
// pzpr.util.pageX() イベントが起こったページ上のX座標を返す
// pzpr.util.pageY() イベントが起こったページ上のY座標を返す
//----------------------------------------------------------------------
getPagePos: function(e) {
return { px: this.pageX(e), py: this.pageY(e) };
},
pageX: function(e) {
if (e.touches !== void 0 && e.touches.length > 0) {
var len = e.touches.length,
pos = 0;
if (len > 0) {
for (var i = 0; i < len; i++) {
pos += e.touches[i].pageX;
}
return pos / len;
}
}
return e.pageX || 0;
},
pageY: function(e) {
if (e.touches !== void 0 && e.touches.length > 0) {
var len = e.touches.length,
pos = 0;
if (len > 0) {
for (var i = 0; i < len; i++) {
pos += e.touches[i].pageY;
}
return pos / len;
}
}
return e.pageY || 0;
},
//--------------------------------------------------------------------------------
// pzpr.util.getRect() エレメントの四辺の座標を返す
//--------------------------------------------------------------------------------
getRect: function(el) {
if (!pzpr.env.browser) {
return { top: 0, bottom: 0, left: 0, right: 0, height: 0, width: 0 };
}
var rect = el.getBoundingClientRect(),
scrollLeft,
scrollTop;
if (window.scrollX !== void 0) {
scrollLeft = window.scrollX;
scrollTop = window.scrollY;
} else {
/* IE11以下向け */
var _html = document.documentElement;
scrollLeft = _html.scrollLeft;
scrollTop = _html.scrollTop;
}
var left = rect.left + scrollLeft;
var top = rect.top + scrollTop;
var right = rect.right + scrollLeft;
var bottom = rect.bottom + scrollTop;
return {
top: top,
bottom: bottom,
left: left,
right: right,
height: bottom - top,
width: right - left
};
},
//---------------------------------------------------------------------------
// pzpr.util.checkpid() メニューなどが表示対象のパズルかどうか返す
//---------------------------------------------------------------------------
checkpid: function(str, pid) {
var matches = str.match(/!?[a-z0-9-]+/g),
isdisp = true;
if (!!matches) {
isdisp = false;
for (var i = 0; i < matches.length; i++) {
if (matches[i].charAt(0) !== "!") {
if (matches[i] === pid) {
isdisp = true;
}
} else {
isdisp = matches[i].substr(1) !== pid;
}
}
}
return isdisp;
}
};
})();