|
|
var assembly;
|
|
|
var classtype = {
|
|
|
'[object Array]': 'array',
|
|
|
'[object Boolean]': 'boolean',
|
|
|
'[object Date]': 'date',
|
|
|
'[object Function]': 'function',
|
|
|
'[object Number]': 'number',
|
|
|
'[object Object]': 'object',
|
|
|
'[object RegExp]': 'regexp',
|
|
|
'[object String]': 'string'
|
|
|
};
|
|
|
var me = {
|
|
|
__Index: 0,
|
|
|
list: [],
|
|
|
get: function(id) {
|
|
|
return id === undefined ? this.list : this.list[id];
|
|
|
},
|
|
|
fn: function() { },
|
|
|
inherit: function(childClass, parentClass) {
|
|
|
var Constructor = me.fn;
|
|
|
|
|
|
Constructor.prototype = parentClass.prototype;
|
|
|
childClass.prototype = new Constructor();
|
|
|
|
|
|
childClass.prototype.constructor = childClass;
|
|
|
childClass.superclass = parentClass.prototype;
|
|
|
|
|
|
if (childClass.prototype.constructor === Object.prototype.constructor) {
|
|
|
childClass.prototype.constructor = parentClass;
|
|
|
}
|
|
|
},
|
|
|
extend: function(obj, newProperties) {
|
|
|
var key;
|
|
|
|
|
|
for (key in newProperties) {
|
|
|
if (newProperties.hasOwnProperty(key)) {
|
|
|
obj[key] = newProperties[key];
|
|
|
}
|
|
|
}
|
|
|
return obj;
|
|
|
},
|
|
|
copy: function(TargetClass, obj, newProperties) {
|
|
|
var value, o, key;
|
|
|
|
|
|
if (typeof obj !== 'object') {
|
|
|
return obj;
|
|
|
}
|
|
|
value = obj.valueOf();
|
|
|
if (obj !== value) {
|
|
|
return new obj.constructor(value);
|
|
|
}
|
|
|
if (obj instanceof obj.constructor && obj.constructor !== Object) {
|
|
|
if (TargetClass) {
|
|
|
o = new TargetClass();
|
|
|
} else {
|
|
|
o = me.clone(obj.constructor.prototype);
|
|
|
}
|
|
|
|
|
|
for (key in obj) {
|
|
|
if (TargetClass || obj.hasOwnProperty(key)) {
|
|
|
o[key] = obj[key];
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
o = {};
|
|
|
for (key in obj) {
|
|
|
if (o.hasOwnProperty(key)) {
|
|
|
o[key] = obj[key];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (newProperties) {
|
|
|
for (key in newProperties) {
|
|
|
if (o.hasOwnProperty(key)) {
|
|
|
o[key] = newProperties[key];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return o;
|
|
|
},
|
|
|
clone: function(obj) {
|
|
|
me.__cloneFunc.prototype = obj;
|
|
|
return new me.__cloneFunc();
|
|
|
},
|
|
|
__cloneFunc: function() {
|
|
|
},
|
|
|
delegate: function(func, scope) {
|
|
|
var args;
|
|
|
|
|
|
scope = scope || window;
|
|
|
|
|
|
if (arguments.length > 2) {
|
|
|
args = Array.prototype.slice.call(arguments, 2);
|
|
|
|
|
|
return function() {
|
|
|
return func.apply(scope, args);
|
|
|
};
|
|
|
} else {
|
|
|
return function() {
|
|
|
return func.call(scope);
|
|
|
};
|
|
|
}
|
|
|
},
|
|
|
dom: function($select, classCss) {
|
|
|
var wrap = $select;
|
|
|
var name, DOM = {
|
|
|
wrap: wrap
|
|
|
},
|
|
|
els = wrap[0].getElementsByTagName('*'),
|
|
|
elsLen = els.length;
|
|
|
var i;
|
|
|
|
|
|
for (i = 0; i < elsLen; i++) {
|
|
|
name = els[i].className;
|
|
|
if (name.indexOf(classCss) > -1) {
|
|
|
name = name.split(classCss)[1];
|
|
|
}
|
|
|
if (name) {
|
|
|
DOM[name] = wrap.find(els[i]);
|
|
|
}
|
|
|
}
|
|
|
return DOM;
|
|
|
},
|
|
|
|
|
|
// 模板引擎
|
|
|
template: function() {
|
|
|
var result, key, reg, i;
|
|
|
var args = arguments;
|
|
|
|
|
|
if (args.length > 0) {
|
|
|
if (me.isString(args[0])) {
|
|
|
result = args[0];
|
|
|
if (args.length === 2 && me.isObject(args[1])) {
|
|
|
for (key in args[1]) {
|
|
|
if (args[1][key] !== undefined) {
|
|
|
reg = new RegExp('({' + key + '})', 'g');
|
|
|
|
|
|
result = result.replace(reg, args[1][key]);
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
for (i = 1; i < args.length; i++) {
|
|
|
if (args[i] !== undefined) {
|
|
|
reg = new RegExp('({[' + (i - 1) + ']})', 'g');
|
|
|
|
|
|
result = result.replace(reg, args[i]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return result;
|
|
|
},
|
|
|
__type: function(obj) {
|
|
|
return obj == null ? String(obj) : classtype[Object.prototype.toString.call(obj)] || 'object';
|
|
|
},
|
|
|
isObject: function(obj) {
|
|
|
return this.isFunction(obj) || !!(obj && 'object' === typeof obj);
|
|
|
},
|
|
|
isFunction: function(obj) {
|
|
|
return this.__type(obj) === 'function';
|
|
|
},
|
|
|
isArray: Array.isArray || function(obj) {
|
|
|
return this.__type(obj) === 'array';
|
|
|
},
|
|
|
isNum: function(obj) {
|
|
|
return !isNaN(parseFloat(obj)) && isFinite(obj);
|
|
|
},
|
|
|
isString: function(obj) {
|
|
|
return this.__type(obj) === 'string';
|
|
|
},
|
|
|
each: function(data, callback, args) {
|
|
|
var i, len;
|
|
|
|
|
|
if (me.isArray(data)) {
|
|
|
for (i = 0, len = data.length; i < len; i++) {
|
|
|
if (callback.call(data[i], i, data[i], args) === false) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
for (i in data) {
|
|
|
if (callback.call(data[i], i, data[i], args) === false) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
funManager: {
|
|
|
__loadList: {},
|
|
|
__loadFun: function(item, callback, win) {
|
|
|
|
|
|
if (item.methord && me.isFunction(item.methord())) {
|
|
|
win = win || window;
|
|
|
item.methord()(item, function() {
|
|
|
callback();
|
|
|
}, win);
|
|
|
}
|
|
|
},
|
|
|
load: function(fns, statechange, win, __index) {
|
|
|
__index = __index || 0;
|
|
|
|
|
|
if (fns[__index]) {
|
|
|
me.funManager.__loadFun(fns[__index], function() {
|
|
|
me.funManager.load(fns, statechange, win, __index + 1);
|
|
|
}, win);
|
|
|
}
|
|
|
statechange(__index, win);
|
|
|
},
|
|
|
get: function(id) {
|
|
|
return this.__loadList[id];
|
|
|
}
|
|
|
},
|
|
|
log: function(msg) {
|
|
|
var console = window.console || {
|
|
|
log: function() { }
|
|
|
};
|
|
|
|
|
|
console.log(msg);
|
|
|
},
|
|
|
Event: {
|
|
|
mousewheel: function(e) {
|
|
|
var _eoe = e.originalEvent;
|
|
|
var _de = _eoe.detail ? _eoe.detail * -1 : _eoe.wheelDelta / 40;
|
|
|
var _direction = _de < 0 ? -1 : 1;
|
|
|
|
|
|
return {
|
|
|
direction: _direction,
|
|
|
unit: _de
|
|
|
};
|
|
|
},
|
|
|
__: function(_e, el, event, handle) {
|
|
|
var key;
|
|
|
|
|
|
for (key in _e) {
|
|
|
if (window[_e[key].validator]) {
|
|
|
el[_e[key].validator](_e[key].prefix + event, handle, false);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
add: function(el, event, handle) {
|
|
|
var _e = [
|
|
|
{
|
|
|
validator: 'addEventListener',
|
|
|
prefix: ''
|
|
|
},
|
|
|
{
|
|
|
validator: 'attachEvent',
|
|
|
prefix: 'on'
|
|
|
}
|
|
|
];
|
|
|
|
|
|
this.__(_e, el, event, handle);
|
|
|
},
|
|
|
remove: function(el, event, handle) {
|
|
|
var _e = [
|
|
|
{
|
|
|
validator: 'removeEventListener',
|
|
|
prefix: ''
|
|
|
},
|
|
|
{
|
|
|
validator: 'detachEvent',
|
|
|
prefix: 'on'
|
|
|
}
|
|
|
];
|
|
|
|
|
|
this.__(_e, el, event, handle);
|
|
|
}
|
|
|
},
|
|
|
getUid: function(_name) {
|
|
|
return me.template('me-{0}{1}-{2}', _name, new Date().getTime(), me.__Index++);
|
|
|
},
|
|
|
Browser: {
|
|
|
isTouch: function() {
|
|
|
var msGesture = window.navigator && window.navigator.msPointerEnabled && window.MSGesture;
|
|
|
|
|
|
return (('ontouchstart' in window) || msGesture || window.DocumentTouch) ? true : false;
|
|
|
},
|
|
|
Prefix: function() {
|
|
|
var i;
|
|
|
|
|
|
var props = ['perspectiveProperty', 'WebkitPerspective', 'MozPerspective', 'OPerspective', 'msPerspective'];
|
|
|
var obj = document.createElement('div');
|
|
|
|
|
|
for (i in props) {
|
|
|
if (obj.style[props[i]] !== undefined) {
|
|
|
return me.template('-{0}-', props[i].replace('Perspective', '').toLowerCase());
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
parseURL: function(url) {
|
|
|
var a = document.createElement('a');
|
|
|
var ret, seg, len, i, s;
|
|
|
|
|
|
a.href = url;
|
|
|
return {
|
|
|
source: url,
|
|
|
protocol: a.protocol.replace(':', ''),
|
|
|
host: a.hostname,
|
|
|
port: a.port,
|
|
|
query: a.search,
|
|
|
params: (function() {
|
|
|
ret = {},
|
|
|
seg = a.search.replace(/^\?/, '').split('&'),
|
|
|
len = seg.length, i = 0, s;
|
|
|
|
|
|
for (; i < len; i++) {
|
|
|
if (!seg[i]) {
|
|
|
continue;
|
|
|
}
|
|
|
s = seg[i].split('=');
|
|
|
ret[s[0]] = s[1];
|
|
|
}
|
|
|
return ret;
|
|
|
})(),
|
|
|
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [null, ''])[1],
|
|
|
hash: a.hash.replace('#', ''),
|
|
|
path: a.pathname.replace(/^([^\/])/, '/$1'),
|
|
|
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [null, ''])[1],
|
|
|
segments: a.pathname.replace(/^\//, '').split('/')
|
|
|
};
|
|
|
}
|
|
|
},
|
|
|
Array: {
|
|
|
indexOf: function(array, val) {
|
|
|
var i;
|
|
|
|
|
|
for (i = 0; i < array.length; i++) {
|
|
|
if (this[i] === val) {
|
|
|
return i;
|
|
|
}
|
|
|
}
|
|
|
return -1;
|
|
|
},
|
|
|
remove: function(array, val) {
|
|
|
var index = this.indexOf(array, val);
|
|
|
|
|
|
if (index > -1) {
|
|
|
array.splice(index, 1);
|
|
|
}
|
|
|
return array;
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
assembly = function(options) {
|
|
|
this.initialized = false;
|
|
|
this.registerEvent = {
|
|
|
before: [],
|
|
|
change: [],
|
|
|
after: []
|
|
|
};
|
|
|
this.options = options;
|
|
|
|
|
|
this.init(options);
|
|
|
};
|
|
|
|
|
|
assembly.output = function() {
|
|
|
me.log(me.list);
|
|
|
};
|
|
|
assembly.prototype.oninit = me.fn;
|
|
|
|
|
|
assembly.prototype.init = function(cfg) {
|
|
|
this.initialized = true;
|
|
|
|
|
|
// function _getClassName(_constructor, _constr) {
|
|
|
// var _constr = _constr || "";
|
|
|
// if (_constructor.superclass) {
|
|
|
// var args = /(\w+)\.superclass/.exec(_constructor.arguments.callee);
|
|
|
// if (args != null) {
|
|
|
// _constr += args[1] + "-";
|
|
|
// // return _getClassName(_constructor.superclass.constructor, _constr);
|
|
|
// }
|
|
|
// }
|
|
|
// return _constr;
|
|
|
// }
|
|
|
this.__Uid = me.getUid('me');
|
|
|
this.oninit(cfg);
|
|
|
me.list[this.__Uid] = this;
|
|
|
};
|
|
|
assembly.prototype.destory = function() {
|
|
|
this.initialized = false;
|
|
|
delete me.list[this.__Uid];
|
|
|
};
|
|
|
assembly.prototype.getUid = function() {
|
|
|
return this.__Uid;
|
|
|
};
|
|
|
assembly.prototype.getOptions = function() {
|
|
|
return this.options;
|
|
|
};
|
|
|
assembly.prototype.config = function() {
|
|
|
if (arguments.length > 0) {
|
|
|
if (typeof (arguments[0]) === 'string') {
|
|
|
if (arguments.length > 1) {
|
|
|
this.options[arguments[0]] = arguments[1];
|
|
|
} else {
|
|
|
return this.options[name];
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
return this.options;
|
|
|
}
|
|
|
};
|
|
|
assembly.prototype.on = function(name, callback) {
|
|
|
var __self = this;
|
|
|
var _e = __self.registerEvent[name];
|
|
|
|
|
|
if (_e) {
|
|
|
_e.push(callback);
|
|
|
}
|
|
|
return _e;
|
|
|
};
|
|
|
assembly.prototype.off = function(name, callback) {
|
|
|
var __self = this;
|
|
|
var _e = __self.registerEvent[name];
|
|
|
var e = [];
|
|
|
|
|
|
me.each(_e, function(name, _callback) {
|
|
|
|
|
|
if (_callback === callback) {
|
|
|
e.push(name);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
me.each(e.reverse(), function(name, _callback) {
|
|
|
_e.splice(_callback, 1);
|
|
|
});
|
|
|
};
|
|
|
me.assembly = assembly;
|
|
|
|
|
|
module.exports = me; |
...
|
...
|
|