pox 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,33 @@
1
+ //= require ./core
2
+
3
+ (function() {
4
+ "use strict";
5
+
6
+ /**
7
+ * @namespace
8
+ */
9
+ Pox.Browser = (
10
+ /**
11
+ * @lends Pox.Browser
12
+ */
13
+ {
14
+ /**
15
+ * 是否支持 WebkitTransition
16
+ * @type boolean
17
+ */
18
+ supportWebkitTransition: false,
19
+
20
+ /**
21
+ * 是否支持 WebkitTransform
22
+ * @type boolean
23
+ */
24
+ supportWebkitTransform: false
25
+ })
26
+
27
+ if ('WebKitTransitionEvent' in window) {
28
+ Pox.extend(Pox.Browser, {
29
+ supportWebkitTransition: true,
30
+ supportWebkitTransform: true
31
+ })
32
+ }
33
+ })()
@@ -0,0 +1,289 @@
1
+ //= require_self
2
+ //= require ./debugger
3
+
4
+ /**
5
+ * Pox 基础类
6
+ * @namespace Pox
7
+ */
8
+ window.Pox = {};
9
+
10
+ (function() {
11
+ "use strict";
12
+
13
+ /** @lends Pox */
14
+ var Basic = {
15
+ /**
16
+ * @param {object} target
17
+ * @param {object} exts
18
+ * @returns {object} target
19
+ */
20
+ extend: function(target, exts) {
21
+ exts = exts || {};
22
+ for(var m in exts) {
23
+ target[m] = exts[m];
24
+ }
25
+ return target;
26
+ },
27
+
28
+ /**
29
+ * 注册在 window 对象 onload 完成后执行的回调
30
+ * @param {function} callback
31
+ */
32
+ ready: function(callback) {
33
+ if (this._doReadyed) {
34
+ callback();
35
+ } else {
36
+ this._readyCallbacks = this._readyCallbacks || [];
37
+ this._readyCallbacks.push(callback);
38
+ }
39
+ },
40
+
41
+ /**
42
+ * 执行在 window 对象 onload 事件上绑定的回调
43
+ * @example <caption>Example usage of doReady.</caption>
44
+ * <pre>&lt;body onload="Pox.doReady()"&gt;
45
+ * &lt;/body&gt;</pre>
46
+ */
47
+ doReady: function() {
48
+ this._doReadyed = true;
49
+ if (this._readyCallbacks) {
50
+ for(var i = 0; i < this._readyCallbacks.length; i++) {
51
+ this._readyCallbacks[i]();
52
+ this._readyCallbacks[i] = null;
53
+ }
54
+
55
+ this._readyCallbacks = null;
56
+ }
57
+ },
58
+
59
+ /**
60
+ * 在 window 对象 unload 完成后执行的回调
61
+ * @param {function} callback
62
+ */
63
+ leave: function(callback) {
64
+ this._unloadCallbacks = this._unloadCallbacks || [];
65
+ this._unloadCallbacks.push(callback);
66
+ },
67
+
68
+ doLeave: function() {
69
+ this._unloadCallbacks = this._unloadCallbacks || [];
70
+ for(var i = 0; i < this._unloadCallbacks.length; i++) {
71
+ this._unloadCallbacks[i]();
72
+ }
73
+ },
74
+
75
+
76
+ /**
77
+ * 页面跳转
78
+ * @param {String} url
79
+ */
80
+ goto: function(url) {
81
+ if (url.indexOf("://")) {
82
+ location.href = url;
83
+ } else {
84
+ var fullUrl = location.protocol + "//" + location.hostname + ":" + location.port + url;
85
+ location.href = fullUrl;
86
+ }
87
+ },
88
+
89
+
90
+ /**
91
+ * 根据 Class 创建类
92
+ * @param {Class} fn1
93
+ * @param {Class} fn2
94
+ * @param {Class} fn3
95
+ * @param {...Class} fnN
96
+ * @returns {Function}
97
+ */
98
+
99
+ clazz: function(fn1, fn2, fn3, fn4, fn5, fn6) {
100
+ // ipanel don't full support "arguments" object
101
+ var clz = function(pm1, pm2, pm3, pm4, pm5, pm6) {
102
+ if (this.init) {
103
+ this.init.call(this, pm1, pm2, pm3, pm4, pm5, pm6);
104
+ }
105
+ }
106
+
107
+ var args = [ fn1, fn2, fn3, fn4, fn5, fn6 ];
108
+ for (var i = 0; i < args.length; i++) {
109
+ var obj = args[i];
110
+ if (obj == null) { continue; }
111
+
112
+ if (this.isFunction(obj)) {
113
+ this.extend(clz.prototype, obj.prototype);
114
+ } else {
115
+ this.extend(clz.prototype, obj);
116
+ }
117
+ };
118
+
119
+ return clz;
120
+ },
121
+
122
+ /**
123
+ * 根据 Class 创建对象
124
+ * @param {Class} fn1
125
+ * @param {Class} fn2
126
+ * @param {Class} fn3
127
+ * @param {...Class} fnN
128
+ * @returns {object}
129
+ */
130
+ instance: function(fn1, fn2, fn3, fn4, fn5, fn6) {
131
+ var ins = {};
132
+ // ipanel don't full support "arguments" object
133
+ var args = [ fn1, fn2, fn3, fn4, fn5, fn6 ];
134
+ for (var i = 0; i < args.length; i++) {
135
+ var obj = args[i];
136
+ if (obj == null) { continue; }
137
+
138
+ if (this.isFunction(obj)) {
139
+ this.extend(ins, obj.prototype);
140
+ } else {
141
+ this.extend(ins, obj);
142
+ }
143
+ };
144
+
145
+ return ins;
146
+ },
147
+
148
+ /**
149
+ * 判断是否为函数
150
+ * @param {object} obj
151
+ * @returns {boolean}
152
+ */
153
+ isFunction: function(obj) {
154
+ return typeof obj == "function";
155
+ },
156
+
157
+ /**
158
+ * 绑定函数的执行对象
159
+ * @param {object} context
160
+ * @param {Function} fn
161
+ * @returns {Function}
162
+ */
163
+ proxy: function(context, fn) {
164
+ return function() {
165
+ fn.apply(context, arguments);
166
+ }
167
+ },
168
+
169
+ /**
170
+ * 当前时间
171
+ * @returns {Date} returns date.
172
+ */
173
+ now: function() {
174
+ if (this._nowBaseLine) {
175
+ var current = new Date();
176
+ var offset = current.getTime() - this._nowActual.getTime();
177
+ var time = this._nowBaseLine.getTime() + offset;
178
+ var result = new Date();
179
+ result.setTime(time);
180
+ return result;
181
+ }
182
+ return new Date();
183
+ },
184
+
185
+ nowBaseline: function(dateStr) {
186
+ this._nowActual = new Date();
187
+ this._nowBaseLine = this.parseIso8601(dateStr);
188
+ },
189
+
190
+ /**
191
+ * 把格式为iso8601的字符串转换为Date对象
192
+ * @param {string} str
193
+ * @returns {Date}
194
+ */
195
+ parseIso8601: function(str) {
196
+ var block = str.split("T")
197
+ var dateStr = block[0];
198
+ var timeWithZoneStr = block[1];
199
+
200
+ var year = 0;
201
+ var month = 0;
202
+ var day = 1;
203
+
204
+ if (dateStr) {
205
+ var d = dateStr.split("-");
206
+ year = parseInt(d[0] || "0", 10);
207
+ month = parseInt(d[1] || "1", 10) - 1;
208
+ day = parseInt(d[2] || "1", 10);
209
+ }
210
+
211
+ var hours = 0;
212
+ var minutes = 0;
213
+ var seconds = 0;
214
+ var milliseconds = 0;
215
+ var minutesOffset = 0;
216
+
217
+ if (timeWithZoneStr) {
218
+ var m = timeWithZoneStr.match(/([^\+\-|Z]+)(Z|[\-\+].+)?/)
219
+ var time = m[1];
220
+ var zone = m[2];
221
+
222
+ var t = time.match(/(\d{2}):?(\d{2})?:?(\d{2})?(\.\d+)?/)
223
+ if (t[1]) {
224
+ hours = parseInt(t[1] || "0", 10);
225
+ }
226
+ if (t[2]) {
227
+ minutes = parseInt(t[2] || "0", 10);
228
+ }
229
+ if (t[3]) {
230
+ seconds = parseInt(t[3] || "0", 10);
231
+ }
232
+ if (t[4]) {
233
+ milliseconds = parseInt(t[4].substing(1) || "0", 10);
234
+ }
235
+
236
+ if (zone && zone[0] != "Z") {
237
+ var z = zone.match(/([\+\-])(\d{2}):?(\d+)/);
238
+ minutesOffset = parseInt(z[2], 10) * 60 + parseInt(z[3] || "0", 10);
239
+ if (z[1] == "+") {
240
+ minutesOffset = 0 - minutesOffset;
241
+ }
242
+ }
243
+ }
244
+
245
+ var localMinutesOffset = minutesOffset - new Date().getTimezoneOffset();
246
+ var date = new Date(year, month, day, hours, minutes, seconds)
247
+ var time = date.getTime() + localMinutesOffset * 60 * 1000;
248
+ date.setTime(time);
249
+ return date;
250
+ },
251
+
252
+ /**
253
+ * 判断数组是否含有该对象
254
+ * @param {array} array
255
+ * @param {object} obj
256
+ * @returns {boolean}
257
+ */
258
+ contains: function(array, obj) {
259
+ for(var i=0; i < array.length; i++){
260
+ if (array[i] == obj) {
261
+ return true;
262
+ }
263
+ }
264
+ return false;
265
+ },
266
+
267
+ /**
268
+ * 判断对象是否为空
269
+ * @todo 需要支持数组
270
+ * @param {object} hash
271
+ * @returns {boolean}
272
+ */
273
+ isEmpty: function(hash) {
274
+ for (var name in hash) {
275
+ return false;
276
+ }
277
+
278
+ return true;
279
+ },
280
+
281
+ debug: function(msg) {
282
+
283
+ }
284
+ }
285
+
286
+
287
+
288
+ Basic.extend(Pox, Basic);
289
+ })();
@@ -0,0 +1,54 @@
1
+ (function() {
2
+ "use strict";
3
+
4
+ var Debugger = {
5
+ messages: [],
6
+ ready: false,
7
+ enabled: true,
8
+ element: null,
9
+ sequence: 1,
10
+
11
+ debug: function(msg) {
12
+ if (this.enabled) {
13
+ var message = "<li>" + this.sequence + " : " + (msg || "") + "</li>";
14
+ this.sequence += 1;
15
+ this.messages.unshift(message);
16
+ this.flush();
17
+ }
18
+ },
19
+
20
+ disable: function() {
21
+ this.enabled = false;
22
+ this.messages = [];
23
+ },
24
+
25
+ flush: function() {
26
+ if (!this.ready) { return; }
27
+
28
+ var output = Array.prototype.slice.call(this.messages, 0, 20);
29
+ this.element.innerHTML = output.join("\n");
30
+ }
31
+ }
32
+
33
+ Pox.ready(function() {
34
+ var box = Pox.getById("debug");
35
+ if (box) {
36
+ Debugger.element = box;
37
+ Debugger.ready = true;
38
+ Debugger.flush();
39
+ } else {
40
+ Debugger.disable();
41
+ }
42
+ });
43
+
44
+ /** @lends Pox */
45
+ Pox.extend(Pox, {
46
+ /**
47
+ * 输出调试日志
48
+ * @param {string} msg
49
+ */
50
+ debug: function(msg) {
51
+ Debugger.debug(msg);
52
+ }
53
+ });
54
+ })();
@@ -0,0 +1,261 @@
1
+ //= require ./core
2
+ (function() {
3
+ "use strict";
4
+
5
+ /** @lends Pox */
6
+ var PoxDom = {
7
+
8
+ /**
9
+ * 增加 dom 对象的 样式
10
+ * @param {HTMLElement} element
11
+ * @param {string} className
12
+ */
13
+ addClassName: function(element, className) {
14
+ if (this.hasClassName(element, className)) { return; }
15
+
16
+ var classNames = element.className || "";
17
+ classNames += " " + className;
18
+ element.className = classNames;
19
+ },
20
+
21
+
22
+
23
+ /**
24
+ * 删除 dom 对象的 样式
25
+ * @param {HTMLElement} element
26
+ * @param {string} className
27
+ */
28
+ removeClassName: function(element, className) {
29
+ var classList = this.getClassNames(element);
30
+ var newClassList = [];
31
+ for (var i = 0; i < classList.length; i++) {
32
+ var theClassName = classList[i];
33
+ if (theClassName != "" && theClassName != className) {
34
+ newClassList.push(theClassName)
35
+ }
36
+ }
37
+
38
+ element.className = newClassList.join(" ");
39
+ },
40
+
41
+ /**
42
+ * @param {HTMLElement} element
43
+ * @param {string} className
44
+ * @param {boolean} toggle
45
+ */
46
+ toggleClassName: function(element, className, toggle) {
47
+ if (toggle) {
48
+ this.addClassName(element, className);
49
+ } else {
50
+ this.removeClassName(element, className);
51
+ }
52
+ },
53
+
54
+ /**
55
+ * 获取dom对象的样式
56
+ * @param {HTMLElement} element
57
+ * @param {string} className
58
+ * @param {boolean} toggle
59
+ * @returns {string}
60
+ */
61
+ getClassNames: function(element) {
62
+ var classNames = element.className || "";
63
+ return classNames.split(" ");
64
+ },
65
+
66
+ /**
67
+ * 判断dom对象是否含有该样式
68
+ * @param {HTMLElement} element
69
+ * @param {string} className
70
+ * @returns {boolean}
71
+ */
72
+ hasClassName: function(element, className) {
73
+ var classList = this.getClassNames(element);
74
+ for (var i = 0; i < classList.length; i++) {
75
+ if (classList[i] == className) {
76
+ return true;
77
+ }
78
+ }
79
+ return false;
80
+ },
81
+
82
+ /**
83
+ * 删除dom对象
84
+ * @param {HTMLElement} element
85
+ * @returns {object}
86
+ */
87
+ remove: function(element) {
88
+ element.parentNode.removeChild(element);
89
+ },
90
+
91
+ /**
92
+ * 通过class查询dom元素
93
+ * @param {HTMLElement} element
94
+ * @param {string} className
95
+ * @returns {Array}
96
+ */
97
+ queryByClass: function(element, className) {
98
+ if (element.getElementsByClassName) {
99
+ return element.getElementsByClassName(className);
100
+ }
101
+
102
+ var tags = element.getElementsByTagName("*");
103
+ var result = [];
104
+
105
+ for (var i = 0; i < tags.length; i++) {
106
+ if (this.hasClassName(tags[i], className)) {
107
+ result.push(tags[i]);
108
+ }
109
+ }
110
+
111
+ return result;
112
+ },
113
+
114
+ /**
115
+ * 通过class查询第一个符合条件的dom元素
116
+ * @param {HTMLElement} element
117
+ * @param {string} className
118
+ * @returns {HTMLElement}
119
+ */
120
+ findByClass: function(element, className) {
121
+ var els = this.queryByClass(element, className) || [];
122
+ return els[0];
123
+ },
124
+
125
+ /**
126
+ * 通过id查询dom元素
127
+ * @param {string} id
128
+ * @returns {HTMLElement}
129
+ */
130
+ getById: function(id) {
131
+ return document.getElementById(id);
132
+ },
133
+
134
+ /**
135
+ * 切换dom元素的显示与隐藏状态
136
+ * @param {HTMLElement} element
137
+ * @param {boolean} flag
138
+ */
139
+ toggle: function(element, flag){
140
+ if (flag) {
141
+ element.style.display = "block";
142
+ } else {
143
+ element.style.display = "none";
144
+ }
145
+ },
146
+
147
+ /**
148
+ * <del>获得style的属性值</del>
149
+ * @deprecated 因为有些机顶盒会返回非 string 类型的值, 所以请用 {@linkcode Pox.getComputedStyle getComputedStyle} 代替
150
+ * @param {HTMLElement} element
151
+ * @param {string} cssProperty
152
+ * @returns {*}
153
+ */
154
+ getStyle: function(element, cssProperty) {
155
+ if (!element) {
156
+ return null;
157
+ }
158
+
159
+ if (element.currentStyle) {
160
+ return element.currentStyle[cssProperty];
161
+ } else {
162
+ return window.getComputedStyle(element)[cssProperty];
163
+ }
164
+ },
165
+
166
+ /**
167
+ * 获得计算后的属性值
168
+ * @param {HTMLElement} element
169
+ * @param {string} cssProperty
170
+ * @returns {string}
171
+ */
172
+ getComputedStyle: function(element, cssProperty) {
173
+ var value = this.getStyle(element, cssProperty);
174
+
175
+ if (cssProperty == "opacity" && typeof value == "number") {
176
+ return (value / 100.0).toString();
177
+ }
178
+
179
+ if (typeof value == "number") {
180
+ if (value > 100000000) { //茁壮盒子会将auto返回为undefined,百分比返回一个很大的数字
181
+ return null;
182
+ }
183
+
184
+ return value + "px";
185
+ }
186
+
187
+ return value;
188
+ },
189
+
190
+ /**
191
+ * 设置style的属性值
192
+ * @param {HTMLElement} element
193
+ * @param {string} cssProperty
194
+ * @param {string} cssValue
195
+ */
196
+ setStyle: function(element, cssProperty, cssValue) {
197
+ if (!element) {
198
+ return null;
199
+ }
200
+
201
+ element.style[cssProperty] = cssValue;
202
+ },
203
+
204
+
205
+ /**
206
+ * @typedef {object} DimensionReturn
207
+ * @property {number} top
208
+ * @property {number} left
209
+ * @property {number} width
210
+ * @property {number} height
211
+ */
212
+ /**
213
+ * 获得element相对于屏幕的座标和高宽
214
+ * @param {HTMLElement} element
215
+ * @returns {DimensionReturn}
216
+ */
217
+ getScreenDimension: function(element) {
218
+ var dimension = {
219
+ top: element.offsetTop,
220
+ left: element.offsetLeft,
221
+ width: element.offsetWidth,
222
+ height: element.offsetHeight
223
+ }
224
+
225
+ var parentElement = element.offsetParent;
226
+ while (parentElement) {
227
+ dimension.top += parentElement.offsetTop;
228
+ dimension.left += parentElement.offsetLeft;
229
+ parentElement = parentElement.offsetParent;
230
+ }
231
+
232
+ return dimension;
233
+ },
234
+
235
+ /**
236
+ * 创建dom对象
237
+ * @param {string} html
238
+ * @returns {HTMLElement}
239
+ */
240
+ createElement: function(html) {
241
+ var div = document.createElement('div')
242
+ div.innerHTML = html
243
+ return div.children[0];
244
+ },
245
+
246
+ /**
247
+ * 切换是否隐藏
248
+ * @param {HTMLElement} element
249
+ * @param {boolean} toggle
250
+ */
251
+ toggleVisibility: function(element, toggle) {
252
+ if (toggle) {
253
+ element.style.visibility = "visible";
254
+ } else {
255
+ element.style.visibility = "hidden";
256
+ }
257
+ }
258
+ }
259
+
260
+ Pox.extend(Pox, PoxDom);
261
+ })();