-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbright.min.js
1 lines (1 loc) · 8.67 KB
/
bright.min.js
1
(function () { var Bright = function (selector, parent) { return new Bright.core.init(selector, parent) }, toString = Object.prototype.toString, hasOwnProperty = Object.prototype.hasOwnProperty, push = Array.prototype.push, slice = Array.prototype.slice, indexOf = Array.prototype.indexOf; var rclass = /[\n\t]/g, rspace = /\s+/, rreturn = /\r/g, rspecialurl = /href|src|style/, rtype = /(button|input)/i, rfocusable = /(button|input|object|select|textarea)/i, rclickable = /^(a|area)$/i, rradiocheck = /radio|checkbox/; if (!window.$) { window.Bright = window.$ = Bright } var browserUserAgent = (navigator.userAgent || navigator.vendor || window.opera); if (browserUserAgent != null) { browserUserAgent = browserUserAgent.toLowerCase() } var Browser = { ie: /(msie)|(windows (ce|phone))/.test(browserUserAgent), webkit: /webkit/.test(browserUserAgent), moz: /gecko/.test(browserUserAgent), opera: /opera/.test(browserUserAgent), safari: /safari/.test(browserUserAgent) }; Bright.JsLoader = function (sUrl, fCallback, sBianMa) { var _script = document.createElement("script"); _script.setAttribute("charset", sBianMa || "utf-8"); _script.setAttribute("type", "text/javascript"); _script.setAttribute("src", sUrl); document.getElementsByTagName("head")[0].appendChild(_script); if (Browser.ie) { _script.onreadystatechange = function () { if (this.readyState == "loaded" || this.readyState == "complete") { _script.parentNode.removeChild(_script); fCallback() } } } else { if (Browser.moz || Browser.opera || Browser.webkit) { _script.onload = function () { _script.parentNode.removeChild(_script); fCallback() } } else { _script.parentNode.removeChild(_script); fCallback() } } }; Bright.tmpl = function (str, obj) { if (!(Object.prototype.toString.call(str) === "[object String]")) { return "" } if (!(Object.prototype.toString.call(obj) === "[object Object]" && "isPrototypeOf" in obj)) { return str } return str.replace(/\{([^{}]+)\}/g, function (match, key) { var value = obj[key]; return (value !== undefined) ? "" + value : "" }) }; Bright.core = Bright.prototype = { init: function (selector, parent) { var els; selector = selector || window; parent = parent || document; if (typeof selector == "object") { this.context = this[0] = selector; this.length = 1; return this } els = (typeof selector == "string") ? Bright.selector(selector, parent) : els = selector; this.length = 0; push.apply(this, els); return this }, length: 0, get: function (index) { return (typeof index == "undefined") ? this[0] : this[index] }, size: function () { return this.length }, each: function (fn) { for (var i = 0, l = this.length; i < l; i++) { fn.call(this[i], i, this) } return this }, attrAll: function (name, value) { return this.attr(name, value, true) }, attr: function (name, value, isarray) { var _return = this; var _isget = typeof value == "undefined" ? true : false; if (isarray && this.length > 0) { _return = _isget ? [] : this; this.each(function () { if (_isget) { _return.push(this.getAttribute(name)) } else { this.setAttribute(name, value) } }) } else { if (_isget) { _return = this[0].getAttribute(name) } else { this[0].setAttribute(name, value) } } return _return }, removeAttr: function (name) { return this.each(function () { if (this.nodeType === 1) { this.removeAttribute(name) } }) }, css: function (styles) { var _styles = []; if (typeof styles == "string") { push.apply(_styles, styles.split(";")) } else { if (typeof styles == "object") { for (var name in styles) { _styles.push(name + ":" + styles[name]) } } } var _item; this.each(function () { for (var i = 0, l = _styles.length; i < l; i++) { _item = _styles[i].split(":"); this.style[_item[0]] = _item[1] } }); return this }, addClass: function (value) { if (value && typeof value === "string") { var classNames = (value || "").split(rspace); for (var i = 0, l = this.length; i < l; i++) { var elem = this[i]; if (elem.nodeType === 1) { if (!elem.className) { elem.className = value } else { var className = " " + elem.className + " "; for (var c = 0, cl = classNames.length; c < cl; c++) { if (className.indexOf(" " + classNames[c] + " ") < 0) { elem.className += " " + classNames[c] } } } } } } return this }, removeClass: function (value) { if ((value && typeof value === "string") || value === undefined) { var classNames = (value || "").split(rspace); for (var i = 0, l = this.length; i < l; i++) { var elem = this[i]; if (elem.nodeType === 1 && elem.className) { if (value) { var className = (" " + elem.className + " ").replace(rclass, " "); for (var c = 0, cl = classNames.length; c < cl; c++) { className = className.replace(" " + classNames[c] + " ", " ") } elem.className = className.substring(1, className.length - 1) } else { elem.className = "" } } } } return this }, hasClass: function (className) { for (var i = 0; i < this.length; i++) { if (this[i].className.match(new RegExp("\\b" + className + "\\b"))) { return true } } return false }, hide: function () { this.each(function () { this.style.display = "none" }); return this }, show: function (other) { var _show = other ? other : "block"; this.each(function () { this.style.display = _show }); return this }, html: function (value) { if (value == undefined) { return this[0] ? this[0].innerHTML : "" } this.each(function () { this.innerHTML = value }); return this }, text: function (value) { var name = (document.body.textContent != undefined) ? "textContent" : "innerText"; if (value == undefined) { return this[0] ? this[0][name] : "" } this.each(function () { this[name] = value }); return this }, val: function (value) { if (value == undefined) { return this.els[0] ? this.els[0].value : "" } this.each(function (el) { el.value = value }); return this }, find: function (selector) { return new this.init(selector, this.get(0)) }, click: function (fn) { return this.on("click", fn) }, on: function (event, fn) { var addEvent = function (el) { if (window.addEventListener) { el.addEventListener(event, fn, false) } else { if (window.attachEvent) { el.attachEvent("on" + event, function () { fn.call(el, window.event) }) } } }; this.each(function () { addEvent(this) }); return this }, ready: function (fn) { DOMReady.add(fn); return this }, remove: function () { this.each(function () { this.parentNode.removeChild(this) }); return this }, push: Bright.push, sort: [].sort, splice: [].splice, fastclick: function (fn) { this.each(function (el) { new FastButton(el, function () { fn.call(el) }) }); return this }, textStock: function (value, isup) { var _this = this; var name = (document.body.textContent != undefined) ? "textContent" : "innerText"; var red = "red"; var green = "green"; this.each(function () { if (isup == 0) { } else { if (isup > 0) { $(this).removeClass("red,green"); $(this).addClass("red") } else { if (isup < 0) { $(this).removeClass("red,green"); $(this).addClass("green") } } } this[name] = value }); return this } }; var baseSelector = function (selector, context) { var sels = selector.split(","), el, op, s; for (var i = 0; i < sels.length; i++) { var sel = sels[i].replace(/\s/g, ""); if (typeof sel == "string") { op = sel.substr(0, 1); s = sel.substr(1); if (op == "#") { el = document.getElementById(s); el = (isDescendant(el, context)) ? el : null } else { if (op == ".") { el = getElementsByClassName(s, context) } else { el = context.getElementsByTagName(sel) } } } } return el }; var html5Selector = function (selector, context) { return context.querySelectorAll(selector) }; Bright.selector = function (selector, context) { return html5Selector(selector, context) }; Bright.core.init.prototype = Bright.core; var DOMReady = (function () { var fns = [], isReady = false, ready = function () { isReady = true; for (var i = 0; i < fns.length; i++) { fns[i].call() } }; this.add = function (fn) { if (fn.constructor == String) { var strFunc = fn; fn = function () { eval(strFunc) } } if (isReady) { fn.call() } else { fns[fns.length] = fn } }; if (window.addEventListener) { document.addEventListener("DOMContentLoaded", function () { ready() }, false) } (function () { if (!document.uniqueID && document.expando) { return } var tempNode = document.createElement("document:ready"); try { tempNode.doScroll("left"); ready() } catch (err) { setTimeout(arguments.callee, 0) } })(); return this })(); Bright.ready = DOMReady.add; function isDescendant(desc, anc) { return ((desc.parentNode == anc) || (desc.parentNode != document) && isDescendant(desc.parentNode, anc)) } function getElementsByClassName(className, parent) { parent = parent || document.getElementsByTagName("body")[0]; if (typeof (parent.getElementsByClassName) == "function") { return parent.getElementsByClassName(className) } var a = [], re = new RegExp("\\b" + className + "\\b"), els = parent.getElementsByTagName("*"); for (var i = 0; i < els.length; i++) { if (re.test(els[i].className)) { a.push(els[i]) } } return a } })();