dhtmlx5源码解析(二)全局方法分析date&&ajax

dhtmlx5将日期处理的相应公用函数是放在window.dh4对象中:

if (typeof(window.dhx4.dateLang) == "undefined") {
  //申明语音
  window.dhx4.dateLang = "en";
  //日期用到的常用字
  window.dhx4.dateStrings = {
    en: {
      monthFullName: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
      monthShortName: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
      dayFullName: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
      dayShortName: ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
    }
  };
  //日期默认格式yyyy-mm-dd
  window.dhx4.dateFormat = {en: "%Y-%m-%d"};
  //日期转换成字符串
  window.dhx4.date2str = function (l, g, a) {
    if (g == null || typeof(g) == "undefined") {
      g = window.dhx4.dateFormat[window.dhx4.dateLang]
    }
    if (a == null || typeof(a) == "undefined") {
      a = window.dhx4.dateStrings[window.dhx4.dateLang]
    }
    if (l instanceof Date) {
      var h = function (m) {
        return (String(m).length == 1 ? "0" + String(m) : m)
      };
      var c = function (o) {
        switch (o) {
          case"%d":
            return h(l.getDate());
          case"%j":
            return l.getDate();
          case"%D":
            return a.dayShortName[l.getDay()];
          case"%l":
            return a.dayFullName[l.getDay()];
          case"%m":
            return h(l.getMonth() + 1);
          case"%n":
            return l.getMonth() + 1;
          case"%M":
            return a.monthShortName[l.getMonth()];
          case"%F":
            return a.monthFullName[l.getMonth()];
          case"%y":
            return h(l.getYear() % 100);
          case"%Y":
            return l.getFullYear();
          case"%g":
            return (l.getHours() + 11) % 12 + 1;
          case"%h":
            return h((l.getHours() + 11) % 12 + 1);
          case"%G":
            return l.getHours();
          case"%H":
            return h(l.getHours());
          case"%i":
            return h(l.getMinutes());
          case"%s":
            return h(l.getSeconds());
          case"%a":
            return (l.getHours() > 11 ? "pm" : "am");
          case"%A":
            return (l.getHours() > 11 ? "PM" : "AM");
          case"%%":
            return "%";
          case"%u":
            return l.getMilliseconds();
          case"%P":
            if (window.dhx4.temp_calendar != null && window.dhx4.temp_calendar.tz != null) {
              return window.dhx4.temp_calendar.tz
            }
            var s = l.getTimezoneOffset();
            var r = Math.abs(Math.floor(s / 60));
            var n = Math.abs(s) - r * 60;
            return (s > 0 ? "-" : "+") + h(r) + ":" + h(n);
          default:
            return o
        }
      };
      var e = String(g || window.dhx4.dateFormat).replace(/%[a-zA-Z]/g, c)
    }
    return (e || String(l))
  };
  //字符串转日期
  window.dhx4.str2date = function (l, A, E) {
    if (A == null || typeof(A) == "undefined") {
      A = window.dhx4.dateFormat[window.dhx4.dateLang]
    }
    if (E == null || typeof(E) == "undefined") {
      E = window.dhx4.dateStrings[window.dhx4.dateLang]
    }
    A = A.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\\:|]/g, "\\$&");
    var D = [];
    var o = [];
    A = A.replace(/%[a-z]/gi, function (e) {
      switch (e) {
        case"%d":
        case"%m":
        case"%y":
        case"%h":
        case"%H":
        case"%i":
        case"%s":
          o.push(e);
          return "(\\d{2})";
        case"%D":
        case"%l":
        case"%M":
        case"%F":
          o.push(e);
          return "([a-zéûä\u0430-\u044F\u0451]{1,})";
        case"%j":
        case"%n":
        case"%g":
        case"%G":
          o.push(e);
          return "(\\d{1,2})";
        case"%Y":
          o.push(e);
          return "(\\d{4})";
        case"%a":
          o.push(e);
          return "([a|p]m)";
        case"%A":
          o.push(e);
          return "([A|P]M)";
        case"%u":
          o.push(e);
          return "(\\d{1,6})";
        case"%P":
          o.push(e);
          return "([+-]\\d{1,2}:\\d{1,2})"
      }
      return e
    });
    var F = new RegExp(A, "i");
    var s = l.match(F);
    if (s == null || s.length - 1 != o.length) {
      return "Invalid Date"
    }
    for (var c = 1; c < s.length; c++) {
      D.push(s[c])
    }
    var g = {
      "%y": 1,
      "%Y": 1,
      "%n": 2,
      "%m": 2,
      "%M": 2,
      "%F": 2,
      "%d": 3,
      "%j": 3,
      "%a": 4,
      "%A": 4,
      "%H": 5,
      "%G": 5,
      "%h": 5,
      "%g": 5,
      "%i": 6,
      "%s": 7,
      "%u": 7,
      "%P": 7
    };
    var u = {};
    var n = {};
    for (var c = 0; c < o.length; c++) {
      if (typeof(g[o[c]]) != "undefined") {
        var h = g[o[c]];
        if (!u[h]) {
          u[h] = [];
          n[h] = []
        }
        u[h].push(D[c]);
        n[h].push(o[c])
      }
    }
    D = [];
    o = [];
    for (var c = 1; c <= 7; c++) {
      if (u[c] != null) {
        for (var y = 0; y < u[c].length; y++) {
          D.push(u[c][y]);
          o.push(n[c][y])
        }
      }
    }
    var a = new Date();
    a.setDate(1);
    a.setHours(0);
    a.setMinutes(0);
    a.setSeconds(0);
    a.setMilliseconds(0);
    var x = function (v, e) {
      for (var r = 0; r < e.length; r++) {
        if (e[r].toLowerCase() == v) {
          return r
        }
      }
      return -1
    };
    for (var c = 0; c < D.length; c++) {
      switch (o[c]) {
        case"%d":
        case"%j":
        case"%n":
        case"%m":
        case"%Y":
        case"%H":
        case"%G":
        case"%i":
        case"%s":
        case"%u":
          if (!isNaN(D[c])) {
            a[{
              "%d": "setDate",
              "%j": "setDate",
              "%n": "setMonth",
              "%m": "setMonth",
              "%Y": "setFullYear",
              "%H": "setHours",
              "%G": "setHours",
              "%i": "setMinutes",
              "%s": "setSeconds",
              "%u": "setMilliseconds"
            }[o[c]]](Number(D[c]) + (o[c] == "%m" || o[c] == "%n" ? -1 : 0))
          }
          break;
        case"%M":
        case"%F":
          var m = x(D[c].toLowerCase(), E[{"%M": "monthShortName", "%F": "monthFullName"}[o[c]]]);
          if (m >= 0) {
            a.setMonth(m)
          }
          break;
        case"%y":
          if (!isNaN(D[c])) {
            var C = Number(D[c]);
            a.setFullYear(C + (C > 50 ? 1900 : 2000))
          }
          break;
        case"%g":
        case"%h":
          if (!isNaN(D[c])) {
            var C = Number(D[c]);
            if (C <= 12 && C >= 0) {
              a.setHours(C + (x("pm", D) >= 0 ? (C == 12 ? 0 : 12) : (C == 12 ? -12 : 0)))
            }
          }
          break;
        case"%P":
          if (window.dhx4.temp_calendar != null) {
            window.dhx4.temp_calendar.tz = D[c]
          }
          break
      }
    }
    return a
  }
}

ajax的相关操作也放到了 window.dhx4中

if (typeof(window.dhx4.ajax) == "undefined") {
  window.dhx4.ajax = {
    cache: false, method: "get",
    parse: function (a) {
      if (typeof a !== "string") {
        return a
      }
      a = a.replace(/^[\s]+/, "");
      if (window.DOMParser && !dhx4.isIE) {
        var c = (new window.DOMParser()).parseFromString(a, "text/xml")
      } else {
        if (window.ActiveXObject !== window.undefined) {
          var c = new window.ActiveXObject("Microsoft.XMLDOM");
          c.async = "false";
          c.loadXML(a)
        }
      }
      return c
    },
    xmltop: function (a, h, g) {
      if (typeof h.status == "undefined" || h.status < 400) {
        xml = (!h.responseXML) ? dhx4.ajax.parse(h.responseText || h) : (h.responseXML || h);
        if (xml && xml.documentElement !== null) {
          try {
            if (!xml.getElementsByTagName("parsererror").length) {
              return xml.getElementsByTagName(a)[0]
            }
          } catch (c) {
          }
        }
      }
      if (g !== -1) {
        dhx4.callEvent("onLoadXMLError", ["Incorrect XML", arguments[1], g])
      }
      return document.createElement("DIV")
    },
    xpath: function (g, a) {
      if (!a.nodeName) {
        a = a.responseXML || a
      }
      if (dhx4.isIE) {
        try {
          return a.selectNodes(g) || []
        } catch (l) {
          return []
        }
      } else {
        var h = [];
        var m;
        var c = (a.ownerDocument || a).evaluate(g, a, null, XPathResult.ANY_TYPE, null);
        while (m = c.iterateNext()) {
          h.push(m)
        }
        return h
      }
    },
    query: function (a) {
      dhx4.ajax._call((a.method || "GET"), a.url, a.data || "", (a.async || true), a.callback, null, a.headers)
    },
    get: function (a, c) {
      return this._call("GET", a, null, true, c)
    },
    getSync: function (a) {
      return this._call("GET", a, null, false)
    },
    put: function (c, a, e) {
      return this._call("PUT", c, a, true, e)
    },
    del: function (c, a, e) {
      return this._call("DELETE", c, a, true, e)
    },
    post: function (c, a, e) {
      if (arguments.length == 1) {
        a = ""
      } else {
        if (arguments.length == 2 && (typeof(a) == "function" || typeof(window[a]) == "function")) {
          e = a;
          a = ""
        } else {
          a = String(a)
        }
      }
      return this._call("POST", c, a, true, e)
    },
    postSync: function (c, a) {
      a = (a == null ? "" : String(a));
      return this._call("POST", c, a, false)
    },
    getLong: function (a, c) {
      this._call("GET", a, null, true, c, {url: a})
    },
    postLong: function (c, a, e) {
      if (arguments.length == 2 && (typeof(a) == "function" || typeof(window[a]))) {
        e = a;
        a = ""
      }
      this._call("POST", c, a, true, e, {url: c, postData: a})
    },
    _call: function (a, c, e, h, m, r, g) {
      var o = (window.XMLHttpRequest && !dhx4.isIE ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"));
      var l = (navigator.userAgent.match(/AppleWebKit/) != null && navigator.userAgent.match(/Qt/) != null && navigator.userAgent.match(/Safari/) != null);
      if (h == true) {
        o.onreadystatechange = function () {
          if ((o.readyState == 4) || (l == true && o.readyState == 3)) {
            if (o.status != 200 || o.responseText == "") {
              if (!dhx4.callEvent("onAjaxError", [{xmlDoc: o, filePath: c, async: h}])) {
                return
              }
            }
            window.setTimeout(function () {
              if (typeof(m) == "function") {
                m.apply(window, [{xmlDoc: o, filePath: c, async: h}])
              }
              if (r != null) {
                if (typeof(r.postData) != "undefined") {
                  dhx4.ajax.postLong(r.url, r.postData, m)
                } else {
                  dhx4.ajax.getLong(r.url, m)
                }
              }
              m = null;
              o = null
            }, 1)
          }
        }
      }
      if (a == "GET") {
        c += this._dhxr(c)
      }
      o.open(a, c, h);
      if (g != null) {
        for (var n in g) {
          o.setRequestHeader(n, g[n])
        }
      } else {
        if (a == "POST" || a == "PUT" || a == "DELETE") {
          o.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        } else {
          if (a == "GET") {
            e = null
          }
        }
      }
      o.setRequestHeader("X-Requested-With", "XMLHttpRequest");
      o.send(e);
      if (h != true) {
        if ((o.readyState == 4) || (l == true && o.readyState == 3)) {
          if (o.status != 200 || o.responseText == "") {
            dhx4.callEvent("onAjaxError", [{xmlDoc: o, filePath: c, async: h}])
          }
        }
      }
      return {xmlDoc: o, filePath: c, async: h}
    },
    _dhxr: function (a, c) {
      if (this.cache != true) {
        if (a.match(/^[\?\&]$/) == null) {
          a = (a.indexOf("?") >= 0 ? "&" : "?")
        }
        if (typeof(c) == "undefined") {
          c = true
        }
        return a + "dhxr" + new Date().getTime() + (c == true ? "=1" : "")
      }
      return ""
    }
  }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值