|
|
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: new Array(),
|
|
|
get: function(id) {
|
|
|
return id === undefined ? this.list : this.list[id];
|
|
|
},
|
|
|
fn: new Function(),
|
|
|
inherit: function(childClass, parentClass) {
|
|
|
|
|
|
var Constructor = new Function();
|
|
|
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) {
|
|
|
|
|
|
if (typeof obj !== 'object') {
|
|
|
return obj;
|
|
|
}
|
|
|
var value = obj.valueOf();
|
|
|
|
|
|
if (obj != value) {
|
|
|
return new obj.constructor(value);
|
|
|
}
|
|
|
|
|
|
var o;
|
|
|
|
|
|
if (obj instanceof obj.constructor && obj.constructor !== Object) {
|
|
|
if (targetClass) {
|
|
|
o = new targetClass();
|
|
|
} else {
|
|
|
o = me.clone(obj.constructor.prototype);
|
|
|
}
|
|
|
|
|
|
for (var key in obj) {
|
|
|
if (targetClass || obj.hasOwnProperty(key)) {
|
|
|
o[key] = obj[key];
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
o = {};
|
|
|
for (var key in obj) {
|
|
|
o[key] = obj[key];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (newProperties) {
|
|
|
for (var key in newProperties) {
|
|
|
o[key] = newProperties[key];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return o;
|
|
|
},
|
|
|
clone: function(obj) {
|
|
|
me.__cloneFunc.prototype = obj;
|
|
|
return new me.__cloneFunc();
|
|
|
},
|
|
|
__cloneFunc: function() {
|
|
|
},
|
|
|
delegate: function(func, scope) {
|
|
|
scope = scope || window;
|
|
|
|
|
|
if (arguments.length > 2) {
|
|
|
var 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;
|
|
|
|
|
|
for (var i = 0; i < elsLen; i++) {
|
|
|
name = els[i].className;
|
|
|
if (name.indexOf(classCss) > -1) {
|
|
|
name = name.split(classCss)[1];
|
|
|
}
|
|
|
if (name) {
|
|
|
DOM[name] = $(els[i], wrap)
|
|
|
}
|
|
|
}
|
|
|
return DOM
|
|
|
},
|
|
|
//模板引擎
|
|
|
template: function() {
|
|
|
var args = arguments, result;
|
|
|
|
|
|
if (args.length > 0) {
|
|
|
|
|
|
if (me.isString(args[0])) {
|
|
|
result = args[0];
|
|
|
|
|
|
if (args.length == 2 && me.isObject(args[1])) {
|
|
|
|
|
|
for (var key in args[1]) {
|
|
|
|
|
|
if (args[1][key] != undefined) {
|
|
|
var reg = new RegExp("({" + key + "})", "g");
|
|
|
result = result.replace(reg, args[1][key]);
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
for (var i = 1; i < args.length; i++) {
|
|
|
|
|
|
if (args[i] != undefined) {
|
|
|
var 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) {
|
|
|
|
|
|
for (var key in _e) {
|
|
|
|
|
|
if (win[_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 && document instanceof DocumentTouch) ? true : false;
|
|
|
},
|
|
|
Prefix: function() {
|
|
|
var props = ['perspectiveProperty', 'WebkitPerspective', 'MozPerspective', 'OPerspective', 'msPerspective'];
|
|
|
var obj = document.createElement('div');
|
|
|
|
|
|
for (var 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');
|
|
|
a.href = url;
|
|
|
return {
|
|
|
source: url,
|
|
|
protocol: a.protocol.replace(':', ''),
|
|
|
host: a.hostname,
|
|
|
port: a.port,
|
|
|
query: a.search,
|
|
|
params: (function() {
|
|
|
var 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) || [, ''])[1],
|
|
|
hash: a.hash.replace('#', ''),
|
|
|
path: a.pathname.replace(/^([^\/])/, '/$1'),
|
|
|
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
|
|
|
segments: a.pathname.replace(/^\//, '').split('/')
|
|
|
};
|
|
|
}
|
|
|
},
|
|
|
Array: {
|
|
|
indexOf: function(array, val) {
|
|
|
|
|
|
for (var 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;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
var 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; |
|
|
\ No newline at end of file |
...
|
...
|
|