base.js 18.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
/**
* @fileoverview 核心文件,包含按需载入管理器和基础函数的封装
* @author yuyang <yuyangvi@gmail.com>
* @version 1.0
*/
(function () {
    if (!window.Breeze) {
        /**
        * Function 扩展
        */
        Function.prototype.bind = function () {
            var fn = this, args = Array.prototype.slice.call(arguments, 0), object = args.shift();
            return function () {
                return fn.apply(object,
                          args.concat(Array.prototype.slice.call(arguments, 0)));
            };
        };
        /**
        * 使IE兼容数组的indexOf方法.
        * @memberOf Array
        * @returns 数字,表示元素在数组中的索引地址,如果不在数组中就返回-1
        * @type int
        */
        if (!Array.prototype.indexOf) {
            Array.prototype.indexOf = function (elt /*, from*/) {
                var len = this.length >>> 0;
 
                var from = Number(arguments[1]) || 0;
                from = (from < 0)
                                 ? Math.ceil(from)
                                 : Math.floor(from);
                if (from < 0)
                    from += len;
 
                for (; from < len; from++) {
                    if (from in this &&
                                  this[from] === elt)
                        return from;
                }
                return -1;
            };
        }
        window.isQueue = false;
        var loadQueue = [],
                modQueue = [],
 
                isLoading = false,
                loadingIndex = 0,
                runVerson = '1.0.0912',
 
                startQueue = function () {
                    if (loadQueue.length === 0) {
                        isQueue = false;
                        return;
                    }
                    isQueue = true;
                    var mod = loadQueue.shift();
                    (loadingIndex > 0) && (loadingIndex--);
                    switch (typeof mod) {
                        case 'string': //模组
                            if (modQueue.indexOf(mod) == -1) {
                                var script = document.createElement('script');
                                script.id = mod.replace('.', '-');
 
                                var i = mod.indexOf('.');
                                if (i < 0) {
                                    mod = 'core.' + mod;
                                } else {
                                    //补充上层命名空间
                                    var basemod = mod.slice(0, i);
                                    Breeze[basemod] || (Breeze[basemod] = {});
                                }
                                script.src = Breeze.path + mod.replace('.', '/') + '.js?v='+runVerson;
                                isQueue = false;
                                isLoading = true;
                                                //document.body.appendChild(script);
                                document.getElementsByTagName('head')[0].appendChild(script);
                            } else {
                                startQueue();
                            }
                            break;
                        case 'function': //回执
                            mod(Breeze);
                            startQueue();
                        default:
                    }
                },
                word,
        win = window,
        doc = win.document,
        // Is the DOM ready to be used? Set to true once it occurs.
            isReady = false,
 
        // The functions to execute on DOM ready.
            readyList = [],
 
        // Has the ready events already been bound?
            readyBound = false;
        /**
        * @construct
        */
        Breeze = {
            version: '1.0.0',
            path: breeze_base_path,/*function () {
                return document.getElementById('B_script_base').src.replace('core/base.js', '');
            } (),*/
            /**
            * @description 将参数中的js文件和函数加入管理队列<br />
            * 参数可以是字符串或者函数本身
            * @exports require as Breeze.require
            * @params {String|Function} fun
            */
            require: function () {
                var args = Array.prototype.slice.call(arguments),
                                prequeue = loadQueue.slice(0, loadingIndex),
                                afterqueue = loadQueue.slice(loadingIndex);
                loadQueue = prequeue.concat(args, afterqueue);
                loadingIndex += args.length;
                isQueue || isLoading || startQueue();
            },
            /**
            * @description 命名空间, 用在各模块文件下面,以注明自己所代表的模块。
            * @param {String} modName 对应的名称
            *
            */
            namespace: function (modName, fn) {
                modQueue.push(modName);
                loadingIndex = 0;
                fn(Breeze);
                isLoading = false;
                isQueue || startQueue();
            },
 
            /**
            * @description 检测对象是否是字面量对象
            * @param {Object} o 对象
            * @returns {Boolean}
            */
            isPlainObject: function (o) {
                return o && o.toString() === '[object Object]' && !o['nodeType'] && !o['setInterval'];
            },
            /**
            * @description 当dom加载完成后执行
            * @params {Function} DOM加载完成后要执行的函数
            * @example B.ready(function(){
                                do some thing...
                                });
            */
            ready: function (fn) {
                // Attach the listeners
                if (!readyBound) this._bindReady();
 
                // If the DOM is already ready
                if (isReady) {
                    // Execute the function immediately
                    fn.call(win, this);
                } else {
                    // Remember the function for later
                    readyList.push(fn);
                }
            },
            /**
            * Binds ready events.
            */
            _bindReady: function () {
                var self = this,
                                        doScroll = doc.documentElement.doScroll,
                                        eventType = doScroll ? 'onreadystatechange' : 'DOMContentLoaded',
                                        COMPLETE = 'complete',
                                        fire = function () {
                                            self._fireReady();
                                        };
 
                // Set to true once it runs
                readyBound = true;
 
                // Catch cases where ready() is called after the
                // browser event has already occurred.
                if (doc.readyState === COMPLETE) {
                    return fire();
                }
 
                // w3c mode
                if (doc.addEventListener) {
                    function domReady() {
                        doc.removeEventListener(eventType, domReady, false);
                        fire();
                    }
 
                    doc.addEventListener(eventType, domReady, false);
 
                    // A fallback to window.onload, that will always work
                    win.addEventListener('load', fire, false);
                }
                // IE event model is used
                else {
                    function stateChange() {
                        if (doc.readyState === COMPLETE) {
                            doc.detachEvent(eventType, stateChange);
                            fire();
                        }
                    }
 
                    // ensure firing before onload, maybe late but safe also for iframes
                    doc.attachEvent(eventType, stateChange);
 
                    // A fallback to window.onload, that will always work.
                    win.attachEvent('onload', fire);
 
                    if (win == win.top) { // not an iframe
                        function readyScroll() {
                            try {
                                // Ref: http://javascript.nwbox.com/IEContentLoaded/
                                doScroll('left');
                                fire();
                            } catch (ex) {
                                setTimeout(readyScroll, 1);
                            }
                        }
                        readyScroll();
                    }
                }
            },
 
            /**
            * Executes functions bound to ready event.
            */
            _fireReady: function () {
                if (isReady) return;
 
                // Remember that the DOM is ready
                isReady = true;
 
                // If there are functions bound, to execute
                if (readyList) {
                    // Execute all of them
                    var fn, i = 0;
                    while (fn = readyList[i++]) {
                        fn.call(win, this);
                    }
 
                    // Reset the list of functions
                    readyList = null;
                }
            },
            /**
            * @description 浏览器判断<br/>
            * 可以判断浏览器的核心和javascript的版本号
            * 有ie,webkit,opera和gecko四种
            * 值为数字表示的版本号,如果不是该核心,值为0
            * @example Breeze.UA.ie
            */
            UA: function () {
                var o = {
                    ie: 0,
                    gecko: 0,
                    webkit: 0,
                    opera: 0
                },
                                ua = navigator.userAgent,
                                m,
                                numberify = function (s) {
                                    var c = 0;
                                    return parseFloat(s.replace(/\./g, function () {
                                        return (c++ == 1) ? '' : '.';
                                    }));
                                };
                if (ua) {
                    m = ua.match(/AppleWebKit\/([^\s]*)/);
                    if (m && m[1]) {
                        o.webkit = numberify(m[1]);
                    }
 
                };
 
                if (!o.webkit) { // not webkit
                    // @todo check Opera/8.01 (J2ME/MIDP; Opera Mini/2.0.4509/1316; fi; U; ssr)
                    m = ua.match(/Opera[\s\/]([^\s]*)/);
                    if (m && m[1]) {
                        o.opera = numberify(m[1]);
                    } else { // not opera or webkit
                        m = ua.match(/MSIE\s([^;]*)/);
                        if (m && m[1]) {
                            o.ie = numberify(m[1]);
                        } else { // not opera, webkit, or ie
                            m = ua.match(/Gecko\/([^\s]*)/);
                            if (m) {
                                o.gecko = 1; // Gecko detected, look for revision
                                m = ua.match(/rv:([^\s\)]*)/);
                                if (m && m[1]) {
                                    o.gecko = numberify(m[1]);
                                }
                            }
                        }
                    }
                }
                return o;
            } (),
            /***
            * @description 用于属性合并
            * @param a 被加工的对象
            * @param b 将其参数的属性赋给第一个对象,
            * @param overWrite 确定是否对已经有的属性直接覆盖
            * @returns 加工后的对象
            */
            mix: function (/**Object*/a, /**Object*/b, /**Boolean*/overWrite) {
                for (var p in b) {
                    if (overWrite || (typeof a[p] == 'undefined')) {
                        a[p] = b[p];
                    }
                }
                return a;
            },
            /***
            * @description 多对象覆盖
            * @param {Object} object 需要覆盖的对象
            * @return 加工后的对象
            */
            merge: function (o) {
                var a = arguments, i, l = a.length;
                for (i = 1; i < l; i = i + 1) {
                    Breeze.mix(o, a[i], true);
                }
                return o;
            },
            /**
            * @description 将类数组转换为数组
            * @param {ArrayLike} array 类数组
            * @returns Array 加工后的数组
            */
            makeArray: function (o/*=====, results=====*/) {
                if (B.isArray(o)) return o;
                if (typeof o.length !== 'number' || B.isString(o) || B.isFunction(o)) {
                    return [o];
                }
                return Array.prototype.slice.call(o, 0);
                /*=====
                if ( results ) {
                results.push.apply( results, array);
                return results;
                }
                               
                return array;
                =====*/
            },
            /**
            * @description 将字符串左右两侧的空白去掉
            * @param {String} str 字符串
            * @return 加工后的字符串
            */
            trim: function (str) {
                if (typeof str === 'string') {
                    return str.trim ? str.trim() : str.replace(/\s+$/, '');
                }
                return '';
            },
            /**
            * @description 检查对象是否是函数
            * @param {Object} obj 任意对象
            * @returns Boolean
            */
            isFunction: function (obj) {
                return Object.prototype.toString.call(obj) === "[object Function]";
            },
            /**
            * @description 检查对象是否为数组
            * @param {Object} obj任意对象
            * @returns Boolean
            */
            isArray: function (obj) {
                return Object.prototype.toString.call(obj) === '[object Array]';
            },
            /**
            * @description 检查对象是否为字符串
            * @param {Object} obj任意对象
            * @returns Boolean
            */
            isString: function (o) {
                return Object.prototype.toString.call(o) === '[object String]';
            },
            /**
            * @description 设定缩写
            */
            shortcut: function (s) {
                word && (window[word] = undefined);
                window[word = s] = Breeze;
            },
            /**
            * @description 接受CSS
            * @param {String} url 地址
            */
            loadCSS: function (url, id) {
                var css = document.createElement('link');
                css.type = 'text/css';
                                css.rel = 'stylesheet';
                                css.href = url;
                                if (id){
                                        css.id = id;
                                }
                document.body.appendChild(css);
                                return css;
            }
        };
 
        /**
        * Set Shortcut
        */
        Breeze.shortcut('B');
 
 
        var bindNative = function () {
            ['every', 'forEach', 'filter', 'map', 'some'].forEach(function (n) {
                B[n] = function () {
                    var arg = arguments, l = arg.length;
                    if (l == 0) {
                        return null;
                    }
                    var newarg = Breeze.makeArray(arg);
                    return Array.prototype[n].apply(newarg.shift(), newarg);
                }
            });
        };
        if (Array.prototype.some) {
            bindNative();
        } else {
           B.require('native', bindNative);
        }
        /**
        * @name Breeze.$
        * @function
        */
        var bindDom = function (B) {
            if (typeof Sizzle !== 'undefined') {//载入,整合sizzle;
                /**
                * @lends Breeze
                * @description XX函数
                */
                B.$$ = Sizzle;
                /**
                * @lends Breeze
                * @description XX函数
                */
                B.$ = function (selector, parentNode) {
                    var results = Sizzle(selector, parentNode);
                    return results.length ? results[0] : null;
                }
            } else {//不载入,整合querySelector
                B.$ = function (selector, parentNode) {
                    parentNode = parentNode || document;
                    return parentNode.querySelector(selector);
                };
                B.$$ = function (selector, parentNode) {
                    parentNode = parentNode || document;
                    var ar = parentNode.querySelectorAll(selector), l = ar.length, res = [];
                    for (var i = 0; i < l; i++) {
                        res.push(ar[i]);
                    }
                    return res;
                };
            }
        };
        if (document.querySelectorAll) {
            bindDom(B);
        } else {
            B.require('sizzle', bindDom);
        }
 
 
 
 
        /*
        * 链式核心代码
        */
        Function.prototype.method = function (name, fn) {
            this.prototype[name] = fn;
            return this;
        };
        function _$(el) {
            if (B.isString(el)) {
                this.nodes = B.$$(el);
            } else {
                if (el && el.nodeType && el.nodeType === 1) {
                    this.nodes = B.makeArray(el);
                } else {
                    this.nodes = [];
                }
            }
            this[0] = this.nodes[0];
            this.length = this.nodes.length;
            //return this.nodes;
        };
        B.query = function (el) { return new _$(el); }
        B.extend = function (name, fn) {
            _$.method(name, fn);
            return this;
        };
    }
})();