PhotoSwipe源码解读系列(二)

作者: 铁锚

日期: 2013年12月19日

说明: 本系列文章为草稿,等待后期完善。源码是jQuery版本的,code.photoswipe-3.0.5.js


1. 代码开头,就是一些版权申明,没什么好说的,MIT授权。

// Copyright (c) 2012 by Code Computerlove (http://www.codecomputerlove.com)
// Licensed under the MIT license
// version: 3.0.5

2. 接下来的代码段,是一种闭包形式,匿名空间式的代码段,第一段代码的格式如下:

(function (window) {
  // 代码区域
  // Function.prototype.bind 函数绑定 部分
  // window.Code.Util 部分
}(window));
说明: 如果要在自己的代码环境中将 window 指代为全局window,只有采用函数参数的形式,如果 直接 var window = xxx;将会报语法错误。

3. 对 Function 函数定义类的hack,如果没有bind原型方法则用自定义的方法来实现:

// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
if (!Function.prototype.bind ) {
  Function.prototype.bind = function( obj ) {
    var slice = [].slice,
        args = slice.call(arguments, 1), 
        self = this, 
        nop = function () {}, 
        bound = function () {
          return self.apply( this instanceof nop ? this : ( obj || {} ), 
                              args.concat( slice.call(arguments) ) );
        };

    nop.prototype = self.prototype;
    bound.prototype = new nop();
    return bound;
  };
}
4. 接下来是window.Code工具类的封装:

  if (typeof window.Code === "undefined") {
    window.Code = {};
  }
  
  window.Code.Util = {
    /*
     * Function: registerNamespace
     */      
    registerNamespace: function () {
      var 
        args = arguments, obj = null, i, j, ns, nsParts, root, argsLen, nsPartsLens;
      for (i=0, argsLen=args.length; i<argsLen; i++) {
        ns = args[i];
        nsParts = ns.split(".");
        root = nsParts[0];
        if (typeof window[root] === "undefined"){
          window[root] = {};
        }
        obj = window[root];
        //eval('if (typeof ' + root + ' == "undefined"){' + root + ' = {};} obj = ' + root + ';');
        for (j=1, nsPartsLens=nsParts.length; j<nsPartsLens; ++j) {
          obj[nsParts[j]] = obj[nsParts[j]] || {};
          obj = obj[nsParts[j]];
        }
      }
    },
    /*
     * Function: coalesce
     * Takes any number of arguments and returns the first non Null / Undefined argument.
     */
    coalesce: function () {
      var i, j;
      for (i=0, j=arguments.length; i<j; i++) {
        if (!this.isNothing(arguments[i])) {
          return arguments[i];
        }
      }
      return null;
    },
    /*
     * Function: extend
     */
    extend: function(destination, source, overwriteProperties){
      var prop;
      if (this.isNothing(overwriteProperties)){
        overwriteProperties = true;
      }
      if (destination && source && this.isObject(source)){
        for(prop in source){
          if (this.objectHasProperty(source, prop)) {
            if (overwriteProperties){
              destination[prop] = source[prop];
            }
            else{
              if(typeof destination[prop] === "undefined"){ 
                destination[prop] = source[prop]; 
              }
            }
          }
        }
      }
    },
    /*
     * Function: clone
     */
    clone: function(obj) {
      var retval = {};
      this.extend(retval, obj);
      return retval;
    },
    /*
     * Function: isObject
     */
    isObject: function(obj){
      return obj instanceof Object;
    },
    /*
     * Function: isFunction
     */
    isFunction: function(obj){
      return ({}).toString.call(obj) === "[object Function]";
    },
    /*
     * Function: isArray
     */
    isArray: function(obj){
      return obj instanceof Array;
    },
    /*
     * Function: isLikeArray
     */
    isLikeArray: function(obj) { 
      return typeof obj.length === 'number';
    },
    /*
     * Function: isNumber
     */
    isNumber: function(obj){
      return typeof obj === "number";
    },
    /*
     * Function: isString
     */
    isString: function(obj){
      return typeof obj === "string";
    },
    /*
     * Function: isNothing
     */
    isNothing: function (obj) {
    
      if (typeof obj === "undefined" || obj === null) {
        return true;
      }  
      return false;
      
    },
    /*
     * Function: swapArrayElements
     */
    swapArrayElements: function(arr, i, j){
      
      var temp = arr[i]; 
      arr[i] = arr[j];
      arr[j] = temp;
    
    },
    /*
     * Function: trim
     */
    trim: function(val) {
      return val.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    },
    /*
     * Function: toCamelCase
     */
    toCamelCase: function(val){
      return val.replace(/(\-[a-z])/g, function($1){return $1.toUpperCase().replace('-','');});
    },
    /*
     * Function: toDashedCase
     */
    toDashedCase: function(val){
      return val.replace(/([A-Z])/g, function($1){return "-"+$1.toLowerCase();});
    },
    /*
     * Function: indexOf
     */
    arrayIndexOf: function(obj, array, prop){
      var i, j, retval, arrayItem;
      retval = -1;
      for (i=0, j=array.length; i<j; i++){
        arrayItem = array[i];
        
        if (!this.isNothing(prop)){
          if (this.objectHasProperty(arrayItem, prop)) {
            if (arrayItem[prop] === obj){
              retval = i;
              break;
            }
          }
        }
        else{
          if (arrayItem === obj){
            retval = i;
            break;
          }
        }
      }
      return retval;
    },
    /*
     * Function: objectHasProperty
     */
    objectHasProperty: function(obj, propName){
      
      if (obj.hasOwnProperty){
        return obj.hasOwnProperty(propName);
      }
      else{
        return ('undefined' !== typeof obj[propName]);
      }
    }
  };
内部是一些需要使用到的工具函数.



你可能感兴趣的:(photoswipe)