lib
/***
**
*
author : gp
mail : umissthestars@gmail.com
date : 2015-06-03
version : 1.1.3
TODO : 5
usedNotFor : louxia100
frame : double-deck
with : rr.css ; RR_***.js , RR_***.html(by extended)
closure : 2*2 (top->pub&pri ; bot->pub&pri ; Global->RR.ini.proto)
time-consuming : 2h+1h/+1h/+2h/+3h+1h/+1h+1h/
else : 实际上
,并没有什么卵用,现在的公司根本不需要用到js,呵呵。
*
**
***/
;
(function($, window, document, undefined) {
//RR内Global属性都统一放在这里
var RR,
VERSIONS = "1.1.3";
var RRSPACE = [], //全局RR实例空间
DATASPACE = {}, //RR内部数据空间
DEBUG = { //BUGS //TODO
BUGS: 0,
TYPE: undefined,
REMIND: null
};
//eventHostNeedVar
DATASPACE.page = [],
DATASPACE.mask = [],
DATASPACE.past = [],
DATASPACE.resize = {},
DATASPACE.mod = {};
//下层构造方法
RR = window.RR || function(rr) {
//outter
var rr = rr;
//下层可见变量需要提前声明的,都统一在这里
var Sizzle;
//给下层用的逗比值都将放在这里
var base_city_id = base_city_id || "",
base_url_site = base_url_site || "",
base_url_interface = base_url_interface || "";
//下层的类方法
// 因为下层的方法都比较2B,适用度低,所以请不要乱动上层的属性,在这里添加一个extend供下层自由扩充。
// 并且这里的方法都可以拆离,通过extend导入,独立存在。
// 但这里大量方法需要借助上层实现,完全拆分需要特殊的表演技巧。
/**
* @param {example} in $ :
* 1,RR.extend(函数主体,函数名)
* 2,RR.extend(对象)
*/
/*** extend ***/
rr.extend = function(obj, name) {
if (typeof obj === "object")
return $.extend(rr, obj);
return rr[name] = obj; //索引添加func作为返回值
};
/*** mask ***/
rr.setMask = function(obj, objAttr) {
if (DATASPACE.mask instanceof $) //不加判断的情况,组合使用时竟然递归调用了mask,怪我咯
return;
var obj = rr.tojq(obj),
objAttr = objAttr || {},
c = "rgba(0,0,0,0.6)" || objAttr.color, //暂用rgba防止opacity继承
// o = objAttr.opacity || 0.5,
s = objAttr.src, // 其实这里,目前并没有什么卵用,这叫高瞻远瞩,2333333333
mh = $(window).height(),
mw = $(window).width(),
oh = obj.height(),
ow = obj.width();
//此处定义的事件包括但不限于: window(resize)
//main
obj = obj.clone(true).addClass("RR_mask_obj").css("display", "block");
if (oh < mh) {
obj.css({
"marginTop": (mh - oh) / 2,
});
}
var $mask = $("<div/>").addClass("RR_mask_main").css({
"backgroundColor": c,
}).appendTo("body").append(obj);
$("body").addClass("RR_mask_body");
return DATASPACE.mask = $mask;
}
rr.clearMask = function() {
$("body").removeClass("RR_mask_body");
DATASPACE.mask.remove();
return rr.deleteData("mask");
}
/*** testForm ***/
rr.testForm = function(obj, inf, not) {
var $arr,
obj = $(obj),
arr = [],
str = "";
not ? $arr = obj.find("textarea,input,select").not(not) : $arr = obj.find("textarea,input,select");
$arr.each(function(i, v) {
switch (v.tagName) {
case "INPUT":
{
if ($(this).val() === "")
arr.push(this);
break;
}
case "TEXTAREA":
{
if ($(this).val() === "")
arr.push(this);
break;
}
case "SELECT":
{
if (!$(this).children("*").has(":selected"))
arr.push(this);
break;
}
}
})
if (!inf) return arr.join("; ");
arr.forEach(function(v) {
for (var i in inf) {
if ($(v).attr("id") === i)
str += inf[i] + "; ";
}
})
return str;
}
/*** page ***/
rr.page = function(wrap, url, rule, style) { //TODO
var l = DATASPACE.page.length,
dp = DATASPACE.page[l] = {};
dp.p = 0, //p
dp.l = l, //l
dp.parent = $(warp.parent), //parent
dp.child = $(wrap.child), //child
dp.len = child.length, //len
dp.count = wrap.count, //count
dp.pA = $(wrap.pA), //pA
dp.nA = $(wrap.nA), //nA
dp.cA = $(wrap.cA), //cA
dp.inp = $(wrap.inp); //inp
dp.urlS = url.urlS, //urlS
dp.urlE = url.urlE; //urlE
dp.rule = rule; //rule
dp.cAStyle = style.cAStyle; //cAStyle
//此处定义的事件包括但不限于:pA(click) nA(click) cA(click) inp(input property)
if (len) {
rr.togglePage(dp.p, dp.child, dp.len, dp.count, dp.cA);
dp.pA.on("click", function() { //pA
//Tip0
var dp = rr.findData(this, DATASPACE.page)
if (dp.p === 0) {
dp.p--;
rr.togglePage(dp.p, dp.child, dp.len, dp.count, dp.cA);
} else {
return;
}
});
dp.nA.on("click", function() { //nA //TODO
//Tip:1
});
dp.cA.on("click", function() { //cA
//Tip:2
});
dp.inp.on("input propertychange", function() { //inp
//Tip:3
});
}
}
rr.togglePage = function(p, child, len, count, cA) {
var s = p * count - 1,
e = s + count;
child.each(function(i, v) {
if (i > s && i < e) {
$(v).removeClass("RR_hidd").addClass("RR_visi"); //
} else {
$(v).removeClass("RR_visi").addClass("RR_hidd");
}
})
}
/*** resize ***/
rr.resize = function(sizzle, bool) {
var n;
//以下都为条件式声明
typeof sizzle == "string" ?
n = sizzle : sizzle instanceof $ ?
n = sizzle.attr("id") || sizzle.attr("class") : sizzle.nodeType === 1 ?
n = $(sizzle).attr("id") || $(sizzle).attr("class") : alert("你特么逗我呢!");
if (!bool) {
var d = DATASPACE.resize[n] = {},
w = d.width = $(sizzle).width(), // 其实这里,目前并没有什么卵用,这叫高瞻远瞩,2333333333
h = d.height = $(sizzle).height(), // 其实这里,目前并没有什么卵用,这叫高瞻远瞩,2333333333
wW = d.widthW = $(window).width(),
hW = d.heightW = $(window).height();
return rr;
} else {
var w = $(sizzle).width(), // 其实这里,目前并没有什么卵用,这叫高瞻远瞩,2333333333
h = $(sizzle).height(), // 其实这里,目前并没有什么卵用,这叫高瞻远瞩,2333333333
d = DATASPACE.resize[n],
wS = $(window).width() / d.widthW,
hS = $(window).height() / d.heightW;
return {
width: wS * d.width,
height: hS * d.height
}
}
}
/*** toTop ***/
rr.toTop = function(o, h, t) {
var h = h || 1000,
t = t || 500,
o = $(o),
d = $(document);
d.on("scroll", function() {
if ($(document).scrollTop() > h) {
o.removeClass("RR_toTop_hidd").addClass("RR_toTop_visi");
} else {
o.removeClass("RR_toTop_visi").addClass("RR_toTop_hidd");
}
})
o.on("click", function() {
$("body").animate({
"scrollTop": 0,
}, t);
})
}
/*** lazyload by RR ***/
/***
* @param {Object} modName
* @param {Object} sizzle
* @example {Object} mod
RR.mod("img", {
"img1": "img/2.jpg",
"img2": "img/2.jpg",
"img3": "img/2.jpg"
}, {}, {
"img1": "#i1",
"img2": "#i2",
"img3": "#i3"
})
*/
rr.lazyload = function(modName, sizzle, type) {
var mod = rr.getMod(modName),
mods = rr.getMod(modName, true),
type = type || "appear",
siz = $(sizzle);
siz.each(function() {
if (rr.position(this)[type]) {
for (var i in mods["sizzle"]) {
if ($(this).attr("id") == mods["sizzle"][i].slice(1)) {
if ($(this).attr("id") != mod[i])
$(this).attr("src", mod[i])
}
}
}
})
}
/*** carousel ***/
rr.carousel = function() {
//by extended //DONE
//RR_Carouse.js + RR_Carouse.css + RR_Carouse.html
}
/*** ani ***/
rr.animate = function(o, k, t) { //随机动画通道
var ran;
$(o).each(function(i) {
ran = k || parseInt(Math.random() * 10 / 2);
var that = this;
$(this).addClass('RR_ani_' + ran + "0");
if (!t)
setTimeout(function() {
$(that).removeClass('RR_ani_' + ran + "0");
}, 700)
})
}
/*** past ***/
rr.moveMask = function(m, mask, mA) {
//若此处 使用zepto.js 的$,动态构建标签返回值为object array ,
//索引还取不到,我真特么醉了
var m = m,
mA = mA,
mask = mask;
mask.css({
"position": "absolute",
"opacity": "1",
"left": mB[m][0] + "px", //赋予权值
"top": mB[m][1] + "px",
"display": "block",
}).stop(true).animate({
"opacity": "0.6",
"left": mA[3][0] + "px",
"top": mA[0][1] + "px",
}, 400);
}
rr.past = function(host, mask) {
var host = $(host),
mask = $(mask);
host.on("mouseenter", function(e) {
var m,
eTx = event.pageX, //鼠标值
eTy = event.pageY,
w = host.width(), //选择器相位
h = host.height(),
t = host.offset().top,
l = host.offset().left,
mA = [ //维护一个相位数组 //上右下左
[l, t],
[l + w, t],
[l, t + h],
[l, t],
];
//根据相位计算出一个附加值m,作为 权 传递给mask
eTy < 1.1 * mA[0][1] ? m = 0 : eTy > 0.9 * mA[2][1] ? m = 2 : eTx > 0.9 * mA[1][1] ? m = 1 : m = 3;
// if (eTx > mA[0][0] && eTx < mA[1][0] && eTy < 1.2 * h + mA[0][1])
// m = 0;
// if (eTx > 0.8 * w + mA[0][0] && eTy > mA[0][1] && eTy < mA[2][1])
// m = 1;
// if (eTx > mA[0][0] && eTx < mA[1][0] && eTy > 0.8 * h + mA[0][1])
// m = 2;
// if (eTx < 1.2 * mA[0][0] && eTy > mA[0][1] && eTy < [2][1])
// m = 3;
mB = [ //相位修正
[l, t - h],
[l + w + w, t],
[l, t + h + h],
[l - w, t],
]
rr.moveMask(m, mask, mA, mB);
});
host.on("mouseleave", function(e) {
mask.animate({
"opacity": "0.1",
}, 400, function() {
mask.css("display", "none");
})
});
}
/*** ajaxWrap ***/
rr.ajaxWrap = function(obj) { //args
var obj = obj;
return function(func, obj1) { //before
obj.beforeSend = func;
return function(func, obj2) { //comlpete
obj.complete = func;
return function(func, obj3) { //success
obj.success = func;
return function(func, obj4) { //error
obj.error = func;
return function(func, obj5) {
$.ajax(obj);
}
}
}
}
}
}
/*** wx ***/
rr.wx = function() { //TODO
alert(1)
}
/*** *** *** *** *** *** ***/
//这里需要一个保存操作
RRSPACE[RRSPACE.length] = rr;
// rr替换new对象
return rr;
};
//top
RR.ini = function() {
// var //内置私有属性 位于 主体顶层 , 上层可见 ,不会被 后置 覆盖
var pri = "pri";
// this. //内置对象属性 位于 主体上层 ,全层可见 ,且会被 后置 覆盖
this.modArr = []; //push mod
this.VERSIONS = VERSIONS;
this.DEBUG = DEBUG;
// this.func
//因为构造函数RR的参数rr指向ini的实例,并最后返回rr而舍弃了new,
//所以这里的this定义的属性将作为rr声明时起预置的上层环境。
//上层的方法都可以拆离,但问题是拆了就装不回去了233333333
//可以绑在RR.ini.prototype上,作为原型属性令其继承,但副作用是很明显的。
/*** sizzle ***/
this.sizzle = function(Sizzle) {
var that = this; // 保存this
Sizzle instanceof $ ? Sizzle : Sizzle = $(Sizzle);
this.Sizzle = Sizzle;
this.Sizzle.back = function() { //推出sizzle
return that;
}
return this.Sizzle;
}
/*** toJQ ***/
//当返回值为jq的实例且有链式需求时
this.tojq = function(obj) {
if (obj && !(obj instanceof $))
obj = this.sizzle(obj);
return obj;
}
/*** requirejs ***/
this.require = function(main, src) {
if (typeof main === "string")
var main = main,
src = src || "require.js";
$('<script data-main="' + main + '"src="' + src + '" />').appendTo("head");
return this;
};
/*** cookie ***/
this.setCookie = function(name, value, day) {
//day 表示过期时间 cookie中 = 号表示添加,不是赋值
var oDate = new Date();
oDate.setDate(oDate.getDate() + day);
document.cookie = name + '=' + value + ';expires=' + oDate;
return this;
}
this.getCookie = function(name) {
// 获取浏览器所有cookie将其拆分成数组
var arr = document.cookie.split('; ');
for (var i = 0; i < arr.length; i++) {
//将cookie名称和值拆分进行判断
var arr2 = arr[i].split('=');
if (arr2[0] == name) {
return arr2[1];
}
}
return false;
}
this.clearCookie = function() {
var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
if (keys) {
for (var i = keys.length; i--;)
document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
}
return this;
}
/*** setRRSPACE ***/
this.newRR = function(obj) {
//1, 内置原型属性 位于 主体上层的原型链中 ,上层也可以通过 new运算符反补自身后->全层可见 ,
//但此过程会丢弃当前实例 ,强制更新上层原型环境 。
//2, 这里的改动会通过继承,进而污染RRSPACE内的其他RR ,
//因此obj属性的设置需谨慎 ,建议选择DATASPACE作为当前RR的私有存储空间。
//3, 如果你决定了要这么做,也可以考虑在完成某些操作后,使用{}或提前存储的proto 重置ini.proto。
//4, 当然,如果把后面的RR.ini.prototype 去掉注释 也可以起到重置作用 ,不过别指望我会这么作 ,
//因为这本身就是为了提供一组Global属性供全体RR对象继承而设计的。
//5, 没看完前面4条就使用 带参数的该方法 ,一定是在反人类,请有关部门火速抓捕之。
if (typeof obj === "object")
$.extend(true, RR.ini.prototype, obj); //参数为true的$.extend 实质为深度扩充 而非替换
RRSPACE[RRSPACE.length] = new RR(new RR.ini());
this.setDefaultRR(RRSPACE[RRSPACE.length - 1]);
return RRSPACE[RRSPACE.length - 1];
}
this.setDefaultRR = function(objRR) {
if (RRSPACE.indexOf(objRR))
return window.RR = objRR;
else
return false;
}
this.deleteRR = function(objRR) {
var n = RRSPACE[RRSPACE.indexOf(objRR)];
if (n) {
RRSPACE = RRSPACE.slice(0, n).concat(RRSPACE.slice(n + 1));
this.setDefaultRR(RRSPACE[RRSPACE.length - 1] || new RR(new RR.ini()));
return true;
} else
return false;
}
/*** DATASPACE ***/
this.save = function(name, value) {
if (typeof name === "string") {
var name = name,
value = value;
DATASPACE[name] = value;
return DATASPACE[name];
} else if (typeof name === "object")
for (var i in name)
DATASPACE[i] = name[i];
return this;
}
this.read = function(name) {
var name = name;
if (typeof name === "string")
return DATASPACE[name];
}
this.dataLength = function() {
var len = 0;
for (var i in DATASPACE)
len++;
return len;
}
this.deleteData = function(name) {
var del;
if (typeof name === "string")
return delete DATASPACE[name];
else if (typeof name === "number") {
var n = 0;
for (var i in DATASPACE)
if (n++ === name)
return delete DATASPACE[i]
}
}
this.getData = function() {
return DATASPACE;
}
this.setDS = function(data) {
if (typeof data === "object")
DATASPACE = data;
return this;
}
this.findData = function(name, proxy) {
var n = name,
p = proxy || DATASPACE;
p.forEach(function(v, i, a) {
if (typeof v === "object")
this.findData(n, p.v);
if (v === n)
return v;
else
return false;
})
}
/*** mod ***/
this.mod = function(name, obj, obj1, obj2) {
//这里需要一个alert作为mod调试,在注释中
// if (DATASPACE.mod[name])
// alert("mod已存在,赋值将覆盖")
var mod = DATASPACE.mod[name] = {};
mod["body"] = obj || {};
mod["sizzle"] = obj2 || {};
mod["monitor"] = obj1 || {};
return mod;
}
this.setMod = function(name, obj) {
var mod = DATASPACE.mod[name],
modB = mod["body"],
modS = mod["sizzle"],
modM = mod["monitor"];
var arr = [];
for (var q in obj) {
if ((modB[q] !== undefined) || modM[q]) {
arr.push(q);
modB[q] = obj[q];
} else
modB[q] = obj[q];
}
if (arr)
this.triggerMod(mod, arr);
return modB;
}
this.getMod = function(name, bool) {
if (!bool)
return DATASPACE.mod[name]["body"];
return DATASPACE.mod[name];
}
this.setModS = function(name, obj) {
var modS = this.getMod(name, true)["sizzle"];
for (var q in obj)
modS[q] = obj[q];
return modS;
}
this.setModM = function(name, obj) {
var modM = this.getMod(name, true)["monitor"];
for (var q in obj)
modM[q] = obj[q];
return modM;
}
this.triggerMod = function(mod, arr) {
var mod = mod,
arr = arr,
modB = mod["body"],
modS = mod["sizzle"],
modM = mod["monitor"];
arr.forEach(function(v, i, a) {
for (var q in modM) {
if (q == v)
modM[q](mod, arr);
}
})
}
this.deleteMod = function(name) {
return delete DATASPACE.mod[name];
}
/*** position ***/
this.position = function(obj, bool) {
var scrollTop = $(document).scrollTop(),
scrollLeft = $(document).scrollLeft();
var pageHeight = $(document.body).height(), // 其实这里,目前并没有什么卵用,这叫高瞻远瞩,2333333333
pageWidth = $(document.body).width(); // 其实这里,目前并没有什么卵用,这叫高瞻远瞩,2333333333
var clientHeight = $(window).height(),
clientWidth = $(window).width();
var appear, //无obj参数时为undefined作为识别
belong,
disappear;
if (obj) {
appear = belong = disappear = false;
var t = $(obj).offset().top,
l = $(obj).offset().left,
h = $(obj).height(),
w = $(obj).width();
//只判断上下,左右现在是鸡肋
if (t > scrollTop + clientHeight || t < scrollTop)
disappear = true;
if ((t >= scrollTop && t <= scrollTop + clientHeight) || t + h > scrollTop)
appear = true, disappear = false;
if (t >= scrollTop && t + h <= scrollTop + clientHeight)
belong = true, disappear = false;
//左右修正,然而并没有什么卵用
if (bool && (l < screenLeft || l + w < screenLeft + clientWidth))
appear = false, belong = false, disappear = true;
}
return {
"scrollTop": scrollTop,
"scrollLeft": scrollLeft,
"pageHeight": pageHeight,
"pageWidth": pageWidth,
"appear": appear,
"belong": belong,
"disappear": disappear,
}
}
/*** ispc ***/
this.ispc = function() {
var userAgentInfo = navigator.userAgent;
var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");
for (var v = 0; v < Agents.length; v++)
if (userAgentInfo.indexOf(Agents[v]) > 0)
return false;
}
// proto.attr
/***
RR.ini.prototype = {
// maybe nothing, perhaps need more
}
***/
}
window.RRSPACE = RRSPACE; //对外暴露RRSPACE,用于存放各种怪异的RR供选择
window.RR = new RR(new RR.ini()); //对外暴露RR.ini的实例->作为RR存在
})($, window, document, undefined);