'use strict';
require('./_init.js');
var moduleName = 'base',
$p = Module.prototype;
function Module() {
if (!(this instanceof Module)) {
return new Module();
}
Object.defineProperty(this, 'name', {
value: moduleName,
writable: false,
configurable: false
});
}
module.exports = Module;
//窗体宽、窗体高
$p.winW = window.innerWidth;
$p.winH = window.innerHeight;
//空函数
$p.noop = function() {};
//对象扩展
$p.extend = function(target, objN) {
for (var item, x, i = 1; i < arguments.length; i++) {
item = arguments[i];
if (item instanceof Object) {
for (x in item) {
if ([].hasOwnProperty.call(item, x)) {
target[x] = item[x];
}
}
}
}
return target;
};
//设置对象中的key为只读(不指定key则表示所有)
$p.setReadOnly = function(obj, key) {
if (obj instanceof Object) {
var setReadOnly = function(obj, key) {
if ([].hasOwnProperty.call(obj, key)) {
Object.defineProperty(obj, key, {
value: obj[key],
writable: false,
configurable: false
});
}
};
if (typeof key === 'string') {
setReadOnly(obj, key);
} else if (arguments.length === 1) {
for (key in obj) {
setReadOnly(obj, key);
}
}
}
};
//移除字符串中的html标签
$p.removeHtmlTag = (function() {
var e_div = document.createElement('div');
return function(str) {
e_div.innerHTML = str;
str = e_div.textContent;
e_div.innerHTML = '';
return str;
};
})();
//字符串拼接 .formatStr('<input name="{0}" value="{1}">', name, val)
$p.formatStr = function() {
var str = arguments[0];
for (var i = 1; i < arguments.length; i++) {
var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
str = str.replace(re, arguments[i]);
}
return (typeof str === 'string' ? str : '');
};
//获取hash
$p.getHash = function(key) {
var type = arguments[1] === '_s' ? 'search' : 'hash';
var hash = location[type].slice(1);
if (!hash) {
return undefined;
}
var arr = hash.split('&');
for (var index, i = 0; i < arr.length; i++) {
index = arr[i].indexOf('=');
if (index > -1 && arr[i].slice(0, index) === key) {
return arr[i].slice(index + 1);
}
}
return undefined;
};
//设置hash(在fn中手动设置)
$p.setHash = function(key, val, fn) {
var type = arguments[3] === '_s' ? 'search' : 'hash';
var hitIndex, arr = location[type] ? location[type].slice(1).split('&') : [];
for (var index, i = 0; i < arr.length; i++) {
index = arr[i].indexOf('=');
if (index > -1 && arr[i].slice(0, index) === key) {
hitIndex = i;
break;
}
}
if (hitIndex === undefined) {
arr.push(key + '=' + val);
} else {
arr.splice(hitIndex, 1, key + '=' + val);
}
return fn((type === 'search' ? '?' : '#') + arr.join('&'));
};
//获取search
$p.getSearch = function(key) {
return $p.getHash(key, '_s');
};
//设置search(在fn中手动设置)
$p.setSearch = function(key, val, fn) {
return $p.setHash(key, val, fn, '_s');
};
//添加loading .addLoading(element, option) --- option可能值: small | unmask
$p.addLoading = require('./_loading/loading.js').addLoading;
//移除loading .removeLoading(element)
$p.removeLoading = require('./_loading/loading.js').removeLoading;
//递归查找最近的元素(fn为条件)
$p.findClosestEl = function(element, fn, endElement) {
var result;
if (element instanceof HTMLElement) {
endElement = endElement instanceof HTMLElement ? endElement : document.documentElement;
if (fn) {
var findTarget = function(_el) {
if (fn.call(_el)) {
result = _el;
return;
}
if (_el !== endElement && _el.parentElement) {
findTarget(_el.parentElement);
}
};
findTarget(element);
}
}
return result;
};
//XMLHttpRequest
$p.ajax = (function() {
var neatenOptions = (function() {
var defaults = {
url: '',
type: 'GET',
cache: true, //只针对GET(为false时加上时间戳)
data: null, //GET时只能是简单的键值对(queryStr)
success: $p.noop,
error: $p.noop,
complete: $p.noop
};
return function(options) {
var _o = $p.extend({}, defaults, options);
_o.type = _o.type.toUpperCase().trim();
return _o;
};
})();
return function(options) {
options = neatenOptions(options);
if (!options.url || (options.type !== 'GET' && options.type !== 'POST')) {
return;
}
var xhr = new XMLHttpRequest();
if (options.type === 'GET') {
var queryStr = '';
for (var x in options.data) {
if ([].hasOwnProperty.call(options.data, x)) {
queryStr += x + '=' + options.data[x] + '&';
}
}
queryStr += (!options.cache ? (Date.now() + '&') : '');
options.url += (queryStr ? ('?' + queryStr.slice(0, -1)) : '');
}
xhr.open(options.type, options.url);
xhr.addEventListener('readystatechange', function() {
if (xhr.readyState === 4) {
if ((xhr.status >= 200 && xhr.status <= 300) || xhr.status === 304) {
options.success.call(xhr, xhr.responseText);
} else {
options.error.call(xhr);
}
options.complete.call(xhr);
}
});
if (options.type === 'POST') {
xhr.send(JSON.stringify(options.data));
} else {
xhr.send();
}
};
})();