|
|
/**
|
|
|
* MUI核心JS
|
|
|
* @type _L4.$|Function
|
|
|
*/
|
|
|
var mui = (function(document, undefined) {
|
|
|
var readyRE = /complete|loaded|interactive/;
|
|
|
var idSelectorRE = /^#([\w-]+)$/;
|
|
|
var classSelectorRE = /^\.([\w-]+)$/;
|
|
|
var tagSelectorRE = /^[\w-]+$/;
|
|
|
var translateRE = /translate(?:3d)?\((.+?)\)/;
|
|
|
var translateMatrixRE = /matrix(3d)?\((.+?)\)/;
|
|
|
|
|
|
var $ = function(selector, context) {
|
|
|
context = context || document;
|
|
|
if (!selector)
|
|
|
return wrap();
|
|
|
if (typeof selector === 'object')
|
|
|
if ($.isArrayLike(selector)) {
|
|
|
return wrap($.slice.call(selector), null);
|
|
|
} else {
|
|
|
return wrap([selector], null);
|
|
|
}
|
|
|
if (typeof selector === 'function')
|
|
|
return $.ready(selector);
|
|
|
if (typeof selector === 'string') {
|
|
|
try {
|
|
|
selector = selector.trim();
|
|
|
if (idSelectorRE.test(selector)) {
|
|
|
var found = document.getElementById(RegExp.$1);
|
|
|
return wrap(found ? [found] : []);
|
|
|
}
|
|
|
return wrap($.qsa(selector, context), selector);
|
|
|
} catch (e) {}
|
|
|
}
|
|
|
return wrap();
|
|
|
};
|
|
|
|
|
|
var wrap = function(dom, selector) {
|
|
|
dom = dom || [];
|
|
|
Object.setPrototypeOf(dom, $.fn);
|
|
|
dom.selector = selector || '';
|
|
|
return dom;
|
|
|
};
|
|
|
|
|
|
$.uuid = 0;
|
|
|
|
|
|
$.data = {};
|
|
|
/**
|
|
|
* extend(simple)
|
|
|
* @param {type} target
|
|
|
* @param {type} source
|
|
|
* @param {type} deep
|
|
|
* @returns {unresolved}
|
|
|
*/
|
|
|
$.extend = function() { //from jquery2
|
|
|
var options, name, src, copy, copyIsArray, clone,
|
|
|
target = arguments[0] || {},
|
|
|
i = 1,
|
|
|
length = arguments.length,
|
|
|
deep = false;
|
|
|
|
|
|
if (typeof target === "boolean") {
|
|
|
deep = target;
|
|
|
|
|
|
target = arguments[i] || {};
|
|
|
i++;
|
|
|
}
|
|
|
|
|
|
if (typeof target !== "object" && !$.isFunction(target)) {
|
|
|
target = {};
|
|
|
}
|
|
|
|
|
|
if (i === length) {
|
|
|
target = this;
|
|
|
i--;
|
|
|
}
|
|
|
|
|
|
for (; i < length; i++) {
|
|
|
if ((options = arguments[i]) != null) {
|
|
|
for (name in options) {
|
|
|
src = target[name];
|
|
|
copy = options[name];
|
|
|
|
|
|
if (target === copy) {
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
if (deep && copy && ($.isPlainObject(copy) || (copyIsArray = $.isArray(copy)))) {
|
|
|
if (copyIsArray) {
|
|
|
copyIsArray = false;
|
|
|
clone = src && $.isArray(src) ? src : [];
|
|
|
|
|
|
} else {
|
|
|
clone = src && $.isPlainObject(src) ? src : {};
|
|
|
}
|
|
|
|
|
|
target[name] = $.extend(deep, clone, copy);
|
|
|
|
|
|
} else if (copy !== undefined) {
|
|
|
target[name] = copy;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return target;
|
|
|
};
|
|
|
/**
|
|
|
* mui noop(function)
|
|
|
*/
|
|
|
$.noop = function() {};
|
|
|
/**
|
|
|
* mui slice(array)
|
|
|
*/
|
|
|
$.slice = [].slice;
|
|
|
/**
|
|
|
* mui filter(array)
|
|
|
*/
|
|
|
$.filter = [].filter;
|
|
|
|
|
|
$.type = function(obj) {
|
|
|
return obj == null ? String(obj) : class2type[{}.toString.call(obj)] || "object";
|
|
|
};
|
|
|
/**
|
|
|
* mui isArray
|
|
|
*/
|
|
|
$.isArray = Array.isArray ||
|
|
|
function(object) {
|
|
|
return object instanceof Array;
|
|
|
};
|
|
|
/**
|
|
|
* mui isArrayLike
|
|
|
* @param {Object} obj
|
|
|
*/
|
|
|
$.isArrayLike = function(obj) {
|
|
|
var length = !!obj && "length" in obj && obj.length;
|
|
|
var type = $.type(obj);
|
|
|
if (type === "function" || $.isWindow(obj)) {
|
|
|
return false;
|
|
|
}
|
|
|
return type === "array" || length === 0 ||
|
|
|
typeof length === "number" && length > 0 && (length - 1) in obj;
|
|
|
};
|
|
|
/**
|
|
|
* mui isWindow(需考虑obj为undefined的情况)
|
|
|
*/
|
|
|
$.isWindow = function(obj) {
|
|
|
return obj != null && obj === obj.window;
|
|
|
};
|
|
|
/**
|
|
|
* mui isObject
|
|
|
*/
|
|
|
$.isObject = function(obj) {
|
|
|
return $.type(obj) === "object";
|
|
|
};
|
|
|
/**
|
|
|
* mui isPlainObject
|
|
|
*/
|
|
|
$.isPlainObject = function(obj) {
|
|
|
return $.isObject(obj) && !$.isWindow(obj) && Object.getPrototypeOf(obj) === Object.prototype;
|
|
|
};
|
|
|
/**
|
|
|
* mui isEmptyObject
|
|
|
* @param {Object} o
|
|
|
*/
|
|
|
$.isEmptyObject = function(o) {
|
|
|
for (var p in o) {
|
|
|
if (p !== undefined) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
};
|
|
|
/**
|
|
|
* mui isFunction
|
|
|
*/
|
|
|
$.isFunction = function(value) {
|
|
|
return $.type(value) === "function";
|
|
|
};
|
|
|
/**
|
|
|
* mui querySelectorAll
|
|
|
* @param {type} selector
|
|
|
* @param {type} context
|
|
|
* @returns {Array}
|
|
|
*/
|
|
|
$.qsa = function(selector, context) {
|
|
|
context = context || document;
|
|
|
return $.slice.call(classSelectorRE.test(selector) ? context.getElementsByClassName(RegExp.$1) : tagSelectorRE.test(selector) ? context.getElementsByTagName(selector) : context.querySelectorAll(selector));
|
|
|
};
|
|
|
/**
|
|
|
* ready(DOMContentLoaded)
|
|
|
* @param {type} callback
|
|
|
* @returns {_L6.$}
|
|
|
*/
|
|
|
$.ready = function(callback) {
|
|
|
if (readyRE.test(document.readyState)) {
|
|
|
callback($);
|
|
|
} else {
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
|
callback($);
|
|
|
}, false);
|
|
|
}
|
|
|
return this;
|
|
|
};
|
|
|
/**
|
|
|
* 将 fn 缓存一段时间后, 再被调用执行
|
|
|
* 此方法为了避免在 ms 段时间内, 执行 fn 多次. 常用于 resize , scroll , mousemove 等连续性事件中;
|
|
|
* 当 ms 设置为 -1, 表示立即执行 fn, 即和直接调用 fn 一样;
|
|
|
* 调用返回函数的 stop 停止最后一次的 buffer 效果
|
|
|
* @param {Object} fn
|
|
|
* @param {Object} ms
|
|
|
* @param {Object} context
|
|
|
*/
|
|
|
$.buffer = function(fn, ms, context) {
|
|
|
var timer;
|
|
|
var lastStart = 0;
|
|
|
var lastEnd = 0;
|
|
|
var ms = ms || 150;
|
|
|
|
|
|
function run() {
|
|
|
if (timer) {
|
|
|
timer.cancel();
|
|
|
timer = 0;
|
|
|
}
|
|
|
lastStart = $.now();
|
|
|
fn.apply(context || this, arguments);
|
|
|
lastEnd = $.now();
|
|
|
}
|
|
|
|
|
|
return $.extend(function() {
|
|
|
if (
|
|
|
(!lastStart) || // 从未运行过
|
|
|
(lastEnd >= lastStart && $.now() - lastEnd > ms) || // 上次运行成功后已经超过ms毫秒
|
|
|
(lastEnd < lastStart && $.now() - lastStart > ms * 8) // 上次运行或未完成,后8*ms毫秒
|
|
|
) {
|
|
|
run.apply(this, arguments);
|
|
|
} else {
|
|
|
if (timer) {
|
|
|
timer.cancel();
|
|
|
}
|
|
|
timer = $.later(run, ms, null, $.slice.call(arguments));
|
|
|
}
|
|
|
}, {
|
|
|
stop: function() {
|
|
|
if (timer) {
|
|
|
timer.cancel();
|
|
|
timer = 0;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
};
|
|
|
/**
|
|
|
* each
|
|
|
* @param {type} elements
|
|
|
* @param {type} callback
|
|
|
* @returns {_L8.$}
|
|
|
*/
|
|
|
$.each = function(elements, callback, hasOwnProperty) {
|
|
|
if (!elements) {
|
|
|
return this;
|
|
|
}
|
|
|
if (typeof elements.length === 'number') {
|
|
|
[].every.call(elements, function(el, idx) {
|
|
|
return callback.call(el, idx, el) !== false;
|
|
|
});
|
|
|
} else {
|
|
|
for (var key in elements) {
|
|
|
if (hasOwnProperty) {
|
|
|
if (elements.hasOwnProperty(key)) {
|
|
|
if (callback.call(elements[key], key, elements[key]) === false) return elements;
|
|
|
}
|
|
|
} else {
|
|
|
if (callback.call(elements[key], key, elements[key]) === false) return elements;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return this;
|
|
|
};
|
|
|
$.focus = function(element) {
|
|
|
if ($.os.ios) {
|
|
|
setTimeout(function() {
|
|
|
element.focus();
|
|
|
}, 10);
|
|
|
} else {
|
|
|
element.focus();
|
|
|
}
|
|
|
};
|
|
|
/**
|
|
|
* trigger event
|
|
|
* @param {type} element
|
|
|
* @param {type} eventType
|
|
|
* @param {type} eventData
|
|
|
* @returns {_L8.$}
|
|
|
*/
|
|
|
$.trigger = function(element, eventType, eventData) {
|
|
|
element.dispatchEvent(new CustomEvent(eventType, {
|
|
|
detail: eventData,
|
|
|
bubbles: true,
|
|
|
cancelable: true
|
|
|
}));
|
|
|
return this;
|
|
|
};
|
|
|
/**
|
|
|
* getStyles
|
|
|
* @param {type} element
|
|
|
* @param {type} property
|
|
|
* @returns {styles}
|
|
|
*/
|
|
|
$.getStyles = function(element, property) {
|
|
|
var styles = element.ownerDocument.defaultView.getComputedStyle(element, null);
|
|
|
if (property) {
|
|
|
return styles.getPropertyValue(property) || styles[property];
|
|
|
}
|
|
|
return styles;
|
|
|
};
|
|
|
/**
|
|
|
* parseTranslate
|
|
|
* @param {type} translateString
|
|
|
* @param {type} position
|
|
|
* @returns {Object}
|
|
|
*/
|
|
|
$.parseTranslate = function(translateString, position) {
|
|
|
var result = translateString.match(translateRE || '');
|
|
|
if (!result || !result[1]) {
|
|
|
result = ['', '0,0,0'];
|
|
|
}
|
|
|
result = result[1].split(",");
|
|
|
result = {
|
|
|
x: parseFloat(result[0]),
|
|
|
y: parseFloat(result[1]),
|
|
|
z: parseFloat(result[2])
|
|
|
};
|
|
|
if (position && result.hasOwnProperty(position)) {
|
|
|
return result[position];
|
|
|
}
|
|
|
return result;
|
|
|
};
|
|
|
/**
|
|
|
* parseTranslateMatrix
|
|
|
* @param {type} translateString
|
|
|
* @param {type} position
|
|
|
* @returns {Object}
|
|
|
*/
|
|
|
$.parseTranslateMatrix = function(translateString, position) {
|
|
|
var matrix = translateString.match(translateMatrixRE);
|
|
|
var is3D = matrix && matrix[1];
|
|
|
if (matrix) {
|
|
|
matrix = matrix[2].split(",");
|
|
|
if (is3D === "3d")
|
|
|
matrix = matrix.slice(12, 15);
|
|
|
else {
|
|
|
matrix.push(0);
|
|
|
matrix = matrix.slice(4, 7);
|
|
|
}
|
|
|
} else {
|
|
|
matrix = [0, 0, 0];
|
|
|
}
|
|
|
var result = {
|
|
|
x: parseFloat(matrix[0]),
|
|
|
y: parseFloat(matrix[1]),
|
|
|
z: parseFloat(matrix[2])
|
|
|
};
|
|
|
if (position && result.hasOwnProperty(position)) {
|
|
|
return result[position];
|
|
|
}
|
|
|
return result;
|
|
|
};
|
|
|
$.hooks = {};
|
|
|
$.addAction = function(type, hook) {
|
|
|
var hooks = $.hooks[type];
|
|
|
if (!hooks) {
|
|
|
hooks = [];
|
|
|
}
|
|
|
hook.index = hook.index || 1000;
|
|
|
hooks.push(hook);
|
|
|
hooks.sort(function(a, b) {
|
|
|
return a.index - b.index;
|
|
|
});
|
|
|
$.hooks[type] = hooks;
|
|
|
return $.hooks[type];
|
|
|
};
|
|
|
$.doAction = function(type, callback) {
|
|
|
if ($.isFunction(callback)) { //指定了callback
|
|
|
$.each($.hooks[type], callback);
|
|
|
} else { //未指定callback,直接执行
|
|
|
$.each($.hooks[type], function(index, hook) {
|
|
|
return !hook.handle();
|
|
|
});
|
|
|
}
|
|
|
};
|
|
|
/**
|
|
|
* setTimeout封装
|
|
|
* @param {Object} fn
|
|
|
* @param {Object} when
|
|
|
* @param {Object} context
|
|
|
* @param {Object} data
|
|
|
*/
|
|
|
$.later = function(fn, when, context, data) {
|
|
|
when = when || 0;
|
|
|
var m = fn;
|
|
|
var d = data;
|
|
|
var f;
|
|
|
var r;
|
|
|
|
|
|
if (typeof fn === 'string') {
|
|
|
m = context[fn];
|
|
|
}
|
|
|
|
|
|
f = function() {
|
|
|
m.apply(context, $.isArray(d) ? d : [d]);
|
|
|
};
|
|
|
|
|
|
r = setTimeout(f, when);
|
|
|
|
|
|
return {
|
|
|
id: r,
|
|
|
cancel: function() {
|
|
|
clearTimeout(r);
|
|
|
}
|
|
|
};
|
|
|
};
|
|
|
$.now = Date.now || function() {
|
|
|
return +new Date();
|
|
|
};
|
|
|
var class2type = {};
|
|
|
$.each(['Boolean', 'Number', 'String', 'Function', 'Array', 'Date', 'RegExp', 'Object', 'Error'], function(i, name) {
|
|
|
class2type["[object " + name + "]"] = name.toLowerCase();
|
|
|
});
|
|
|
if (window.JSON) {
|
|
|
$.parseJSON = JSON.parse;
|
|
|
}
|
|
|
/**
|
|
|
* $.fn
|
|
|
*/
|
|
|
$.fn = {
|
|
|
each: function(callback) {
|
|
|
[].every.call(this, function(el, idx) {
|
|
|
return callback.call(el, idx, el) !== false;
|
|
|
});
|
|
|
return this;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
* 兼容 AMD 模块
|
|
|
**/
|
|
|
if (typeof define === 'function' && define.amd) {
|
|
|
define('mui', [], function() {
|
|
|
return $;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
return $;
|
|
|
})(document);
|
|
|
//window.mui = mui;
|
|
|
//'$' in window || (window.$ = mui);
|