-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter.js
More file actions
413 lines (379 loc) · 11.8 KB
/
Copy pathadapter.js
File metadata and controls
413 lines (379 loc) · 11.8 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
/**
* @fileOverview 兼容kissy 和 jQuery 的适配器
* @ignore
*/
/**
* @private
* @class jQuery
* 原生的jQuery对象或者使用kissy时适配出来的对象
*/
;(function() {
window.BUI = window.BUI || {};
window.define = window.define || function(name, depends, fun){
if(KISSY.isFunction(depends)){
fun = depends;
depends = [];
}
if(depends && !KISSY.Node){
depends.unshift('core');
}
function require(name){
return KISSY.require.call(KISSY,name);
}
require.async = BUI.use;
function callback(S){
var exports = {};
var module = {};
return fun.call(window, require, exports, module) || (KISSY.isEmptyObject(exports) ? module.exports : exports);
}
KISSY.add(name,callback,{requires : depends});
};
if(!BUI.use){
BUI.use = function(modules,callback){
if(KISSY.isArray(modules)){
modules = modules.join();
};
KISSY.use(modules,function(S){
var args = KISSY.makeArray(arguments);
args.shift();
callback && callback.apply(S,args);
});
};
}
var adapterCallback = function(){
var S = KISSY,
DOM = S.DOM,
NLP = S.Node.prototype;
window.jQuery = window.jQuery || (function () {
'use strict';
function excuteDuration(self,fn,speed, easing,callback){
var params = getDurationParams(speed, easing,callback);
fn.call(self,params.duration,params.complete,params.easing);
}
function getDurationParams(speed, easing,callback){
if(S.isPlainObject(speed)){
var obj = speed;
if(S.isNumber(obj.duration)){
obj.duration = obj.duration / 1000;
}
return obj;
}
if(S.isNumber(speed)){
speed = speed / 1000;
}else if(S.isString(speed)){
callback = easing;
easing = speed;
speed = undefined;
}else if(S.isFunction(speed)){
callback = speed;
speed = undefined;
}
if(S.isFunction(easing)){
callback = easing;
easing = undefined;
}
return {duration : speed,complete : callback,easing : easing};
}
function getOffsetParent(element) {
var doc = element.ownerDocument,
body = doc.body,
parent,
positionStyle = $(element).css('position'),
skipStatic = positionStyle == 'fixed' || positionStyle == 'absolute';
if (!skipStatic) {
return element.nodeName.toLowerCase() == 'html' ? null : element.parentNode;
}
for (parent = element.parentNode; parent && parent != body; parent = parent.parentNode) {
positionStyle = $(parent).css('position');
if (positionStyle != 'static') {
return parent;
}
}
return null;
}
var wrapNode = function(selector,content){
if(!(this instanceof wrapNode)){
return new wrapNode(selector,content);
}
//S.ready
if(S.isFunction(selector)){
return S.ready(selector);
}
if(S.isString(selector)){
if(content){
return new wrapNode(content).find(selector);
}
return new wrapNode(S.all(selector));
}
S.Node.call(this,selector);
};
S.extend(wrapNode,S.Node);
S.augment(wrapNode,{
bind : NLP.on,
off : NLP.detach,
trigger : NLP.fire,
//返回的结果不一致
/*children : function(selector){
return new wrapNode(DOM.children(this[0],selector));
},*/
sort : function(fn){
return Array.prototype.sort.call(this,fn);
},
filter : function(selector){
if(!selector){
return new wrapNode();
}
if(S.isString(selector)){
return new wrapNode(DOM.filter(this[0],selector));
}
var nodes = this.getDOMNodes(),
rst;
if(S.isFunction(selector)){
rst = [];
S.each(nodes,function(node,index){
var r = selector.call(node,index);
if(r){
rst.push(node);
}
});
return new wrapNode(rst);
}
S.each(nodes,function(node){
if(node === selector){
rst = node;
return false;
}
});
return new wrapNode(rst);
},
//返回的结果不一致
find : function(selector){
return new wrapNode(DOM.query(selector,this[0]));
},
/**
* 判断是否符合指定的选择器
*/
is : function(selector){
var splits = selector.split(','),
rst = false;
for (var i = 0; i < splits.length; i++) {
if(!rst && splits[i]){
rst = rst || DOM.test(this[0],splits[i]);
}
};
return rst;
},
//复写delegate,更改参数顺序
delegate : function(selector,eventType,fn){
return wrapNode.superclass.delegate.call(this,eventType,selector,fn);
},
//更改 便遍历函数的顺序
//修改this,和对象
each : function(fn){
return wrapNode.superclass.each.call(this,function(value,index){
return fn.call(this[0],index,value[0]);
});
},
//第一个子元素
first : function(){
return new wrapNode(this[0]);
},
//查找父节点,jQuery 的parent 和parents 有差异
parents : function(selector){
return this.parent(selector);
},
//最后一个子元素
last : function(){
var length = this.length;
return new wrapNode(this[length-1]);
},
/**
* kissy 未提供此方法
*/
offsetParent : function(){
return new wrapNode(getOffsetParent(this[0]));
},
//参数顺序不一致
animate : function(properties,speed, easing,callback){
var params = getDurationParams(speed, easing,callback);
wrapNode.superclass.animate.call(this,properties,params.duration,params.easing,params.complete);
},
/**
* kissy 未提供此方法
*/
position : function(){
var _self = this,
offset = this.offset(),
parent = _self.offsetParent();
if(parent.length){
var parentOffset = parent.offset();
offset.left -= parentOffset.left;
offset.top -= parentOffset.top;
}
return offset;
},
/**
* 将表单数据序列化成对象
* @return {Object} 表单元素的
*/
serializeArray:function(){
var form = this[0],
originElements = null,
elements = null,
arr =[],
checkboxElements = null,
result={};
if(S.isArray(form)){
originElements = form;
}else{
originElements = S.makeArray(form.elements);
}
elements = S.filter(originElements,function(item){
return (item.id ||item.name) && !item.disabled &&
(item.checked || /select|textarea/i.test(item.nodeName) ||
/text|hidden|password/i.test(item.type));
});
//checkbox 做特殊处理,如果所有checkbox都未选中时,设置字段为空
checkboxElements = S.filter(originElements,function(item){
return (item.id ||item.name) && !item.disabled &&(/checkbox/i.test(item.type));
});
S.each(elements,function(elem){
var val = S.one(elem).val(),
name = elem.name||elem.id,
obj = val == null ? {name: name, value: ''} : S.isArray(val) ?
S.map( val, function(val, i){
return {name: name, value: val};
}) :
{name: name, value: val};
if(obj){
arr.push(obj);
}
});
return arr;
}
});
//由于 kissy的动画单位和参数位置跟 jquery的不一致
var durationMethods = ['fadeIn','fadeOut','fadeToggle','slideDown','slideUp','slideToggle','show','hide'];
S.each(durationMethods,function(fnName){
wrapNode.prototype[fnName] = function(speed, easing,callback){
excuteDuration(this,NLP[fnName],speed,easing,callback);
};
});
//jquery上的很多DOM的方法在kissy的Node上不支持
var domMethods = ['change','blur','focus','select'];
S.each(domMethods,function(fnName){
wrapNode.prototype[fnName] = function(){
var el = this[0];
if(el){
if(el[fnName]){
el[fnName]();
}else{
this.fire(fnName);
}
}
}
});
//由于返回的对象的类型是S.Node,所以要更改类型
var nodeMethods = ['children','parent','next','prev','siblings','closest'];
S.each(nodeMethods,function(fnName){
wrapNode.prototype[fnName] = function(selector){
return new wrapNode(DOM[fnName](this[0],selector));
}
});
S.mix(wrapNode,S);
S.mix(wrapNode,{
/**
* 是否包含指定DOM
*/
contains : function(container, contained){
return S.DOM.contains(container, contained);
},
/**
* 实现$.extend
* @return {Object} 结果
*/
extend : function(){
var args = S.makeArray(arguments),
deep = false,
obj;
if(S.isBoolean(arguments[0])){
deep = args.shift();
}
obj = args[0];
if(obj){
for(var i = 1; i < args.length;i++){
if(S.isObject(args[i]) || S.isArray(args[i])){
S.mix(obj,args[i],undefined,undefined,deep);
}
}
}
return obj;
},
/**
* kissy 的此方法跟 jQuery的接口不一致
*/
each : function(elements,fn){
S.each(elements,function(value,index){
return fn(index,value);
});
},
/**
* 返回结果不一致
*/
inArray : function(elem,arr){
return S.indexOf(elem,arr);
},
/**
* jQuery 的map函数将返回为 null 和 undefined的项不返回
*/
map : function(arr,callback){
var rst = [];
S.each(arr,function(item,index){
var val = callback(item,index);
if(val != null){
rst.push(val);
}
});
return rst;
},
/**
* 空操作
*/
noop : function(){},
parseJSON : S.JSON.parse
})
return wrapNode;
})();
window.$ = window.$ || window.jQuery;
return KISSY;
};
define('bui/adapter', ['core'], adapterCallback);
if(KISSY.Node){
adapterCallback();
}
define('jquery', ['bui/adapter'], function(){
return window.jQuery;
});
;(function() {
//from seajs
function getScriptAbsoluteSrc(node) {
return node.hasAttribute ? // non-IE6/7
node.src :
// see http://msdn.microsoft.com/en-us/library/ms536429(VS.85).aspx
node.getAttribute("src", 4);
}
var scripts = document.getElementsByTagName('script'),
loaderScript = scripts[scripts.length - 1],
src = getScriptAbsoluteSrc(loaderScript),
loaderPath = src.substring(0, src.lastIndexOf('/'));
BUI.loaderScript = loaderScript;
KISSY.config({
packages: [{
name:"bui",
path: loaderPath,
charset:'utf-8',
ignorePackageNameInUri: true
}]
});
})();
})();