esri_core.js

来自「esri的ArcGIS Server超级学习模板程序(for java)」· JavaScript 代码 · 共 1,599 行 · 第 1/4 页

JS
1,599
字号
/*
COPYRIGHT 1995-2005 ESRI

TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
Unpublished material - all rights reserved under the
Copyright Laws of the United States.

For additional information, contact:
Environmental Systems Research Institute, Inc.
Attn: Contracts Dept
380 New York Street
Redlands, California, USA 92373

email: contracts@esri.com
*/

if (! Object.prototype.inheritsFrom) { Object.prototype.inheritsFrom = function(oSuper) { for (sProperty in oSuper) this[sProperty] = oSuper[sProperty]; } }

if (! Array.prototype.indexOf) {
  Array.prototype.indexOf = function(item) {
    for (var i=0;i<this.length;i++) if (this[i] == item) return i;
    return -1;
  }
}

if (! String.prototype.trim) { String.prototype.trim = function() { return this.replace(/^\s+/g, '').replace(/\s+$/g, ''); } }

if (! String.prototype.endsWith) {
  String.prototype.endsWith = function(str) {
    if (str.length > this.length) return false;
    if (this.lastIndexOf(str) + str.length == this.length) return true;
    return false;
  }
}

function EsriPoint(x, y) {
  this.x = this.y = null;
  this.reshape(x, y);
}

EsriPoint.prototype.reshape = function(x, y) {
  this.x = x;
  this.y = y;
}

EsriPoint.prototype.offset = function(oX, oY) { return new EsriPoint(this.x + oX, this.y + oY); }
EsriPoint.prototype.equals = function(pt) { return this.x == pt.x && this.y == pt.y; }
EsriPoint.prototype.toString = function() { return "EsriPoint [x = " + this.x + ", y = " + this.y + "]"; }

function EsriRectangle(left, top, width, height) {
  this.left = this.top = this.width = this.height = 0;
  this.center = null;
  this.reshape(left, top, width, height);
}

EsriRectangle.prototype.offset = function(oX, oY) { return new EsriRectangle(this.left + oX, this.top + oY, this.width, this.height); }

EsriRectangle.prototype.reshape = function(left, top, width, height) {
  this.left = left;
  this.top = top;
  this.width = width;
  this.height = height;
  this.center = new EsriPoint(this.left + (this.width / 2), this.top + (this.height / 2));
}

EsriRectangle.prototype.scale = function(factor, scaleCenter) {
  var newWd = this.width * factor;
  var newHt = this.height * factor;
  var rect = new EsriRectangle(this.center.x - (newWd / 2), this.center.y - (newHt / 2), newWd, newHt);

  if (scaleCenter) {
    var x = this.center.x - scaleCenter.x;
    var y = this.center.y - scaleCenter.y;
    var shiftX = x * factor;
    var shiftY = y * factor;
    var moveX = shiftX - x;
    var moveY = shiftY - y;
    rect = rect.offset(moveX, moveY);
  }
  return rect;
}

EsriRectangle.prototype.parseStyle = function(style) {
  var a = style.split(";");
  var l = t = w = h = 0;
  for (var i=0;i<a.length;i++) {
    if (a[i] == "") continue;
    var p = a[i].trim().split(":");
    if (p[0] == "" || p[1] == "") continue;
    switch (p[0]) {
      case "left": l = EsriUtils.getStyleValue(p[1]); break;
      case "top" : t = EsriUtils.getStyleValue(p[1]); break;
      case "width" : w = EsriUtils.getStyleValue(p[1]); break;
      case "height" : h = EsriUtils.getStyleValue(p[1]); break;
    }
  }
  this.reshape(l, t, w, h);
  return this;
}

EsriRectangle.prototype.equals = function(rect) { return this.left == rect.left && this.top == rect.top && this.width == rect.width && this.height == rect.height; }
EsriRectangle.prototype.toStyle = function() { return "left:" + this.left + "px; top:" + this.top + "px; width:" + this.width + "px; height:" + this.height + "px;"; }
EsriRectangle.prototype.toString = function() { return "EsriRectangle [left = " + this.left + ", top = " + this.top + ", width = " + this.width + ", height = " + this.height + "]"; }

function EsriColor(r, g, b) {
  this.red = r;
  this.green = g;
  this.blue = b;
}

EsriColor.prototype.fromHex = function(hex) {
  this.red = EsriUtils.fromHex(((hex.charAt(0)=="#") ? hex.substring(1, 7) : hex).substring(0, 2));
  this.green = EsriUtils.fromHex(((hex.charAt(0)=="#") ? hex.substring(1, 7) : hex).substring(2, 4));
  this.blue = EsriUtils.fromHex(((hex.charAt(0)=="#") ? hex.substring(1, 7) : hex).substring(4, 6));
  return this;
}

EsriColor.prototype.fromString = function(s) {
  var cs = s.split(",");
  this.red = parseInt(cs[0]);
  this.green = parseInt(cs[1]);
  this.blue = parseInt(cs[2]);
  return this;
}

EsriColor.prototype.toHex = function() { return "#" + EsriUtils.toHex(this.red).substring(4) + EsriUtils.toHex(this.green).substring(4) + EsriUtils.toHex(this.blue).substring(4); }
EsriColor.prototype.toString = function() { return "EsriColor [red = " + this.red + ", green = " + this.green + ", blue = " + this.blue + "]"; }

function EsriGraphicsElement(id) {
  this.id = id;
  this.gc = null;

  this.lineColor = "#000";
  this.lineOpacity = 1;
  this.lineWidth = 1;
  this.fillColor = "#000";
  this.fillOpacity = 0;

  this.show = function() { EsriUtils.showElement(this.gc); }
  this.hide = function() { EsriUtils.hideElement(this.gc); }
  this.destroy = function() {}
  this.clip = function(rect) {}
  this.clearClip = function() {}
  this.remove = function(element) {}
  this.clear = function() {}
  this.drawPoint = function(point, symbol) {}
  this.drawLine = function(point1, point2, symbol) {}
  this.drawRectangle = function(rect, symbol) {}
  this.drawOval = function(bounds, symbol) {}
  this.drawCircle = function(center, radius, symbol) {}
  this.drawPolyline = function(points, symbol) {}
  this.drawPolygon = function(points, symbol) {}
  this.drawImage = function(src, left, top, width, height) {}
  this.drawText = function(txt, bounds, style, symbol) {}
  this.updateSymbol = function(el, symbol) {}
}

function EsriGraphicsSymbol(lc, lo, lw, fc, fo) {
  this.lineColor = lc||"#000";
  this.lineOpacity = lo||1;
  this.lineWidth = lw||1;
  this.fillColor = fc||"#000";
  this.fillOpacity = fo||0;
}

var EsriUtils = new function() {
  var appName = window.navigator.appName.toLowerCase();
  var gc;
  var alphaRegExp = new RegExp("progid:DXImageTransform.Microsoft.Alpha\\(opacity=\\d{1,}\\)");
  var alphaImageLoaderRegExp = new RegExp("^.*\\b\\b.*$");

  this.isNav = appName.indexOf("netscape") != -1;
  this.isIE = appName.indexOf("microsoft") != -1;

  this.userAgent = navigator.userAgent;
  this.isIE6 = this.userAgent.indexOf("MSIE 6") != -1;
  this.isIE7 = this.userAgent.indexOf("MSIE 7") != -1;
  this.isFF15 = this.userAgent.indexOf("Firefox/1.5") != -1 || this.userAgent.indexOf("Firefox/2.") != -1;
  this.navType = "IE";
  this.doPostBack = true;

  if (! this.isIE) {
    if (this.userAgent.indexOf("Firefox") != -1) this.navType = "Firefox";
    else if (this.userAgent.indexOf("Opera") != -1) this.navType = "Opera";
    else if (this.userAgent.indexOf("Safari") != -1) this.navType = "Safari";
    else if (this.userAgent.indexOf("Netscape") != -1) this.navType = "Netscape";
    else this.navType = "Mozilla";
  }

  if (this.isIE) {
    this.graphicsType = "VML";
    document.writeln("<xml:namespace ns=\"urn:schemas-microsoft-com:vml\" prefix=\"v\"/>\n");
    document.writeln("<style type=\"text/css\"> v\\:* { behavior: url(#default#VML);} </style>\n");
  }
  else this.graphicsType = "SVG";

  this.leftButton = 1;
  this.rightButton = 2;
  if (this.isNav) this.rightButton = 3;
  this.mouseWheelUnit = 3;
  if (this.isIE) this.mouseWheelUnit = 120;

  this.KEY_LEFT = 37;
  this.KEY_UP = 38;
  this.KEY_RIGHT = 39;
  this.KEY_DOWN = 40;
  this.KEY_ENTER = 13;
  this.KEY_ESCAPE = 27;

  this.hideElement = function(element) { element.style.display = "none"; }
  this.showElement = function(element) { element.style.display = "block"; }
  this.toggleElement = function(element) { (element.style.display.toLowerCase() == "block") ? element.style.display = "none" : element.style.display = "block"; }
  this.moveElement = function(element, left, top) { EsriUtils.setElementStyle(element, "left:" + left + "px; top:" + top + "px;"); }

  this.getXY = function(e) {
    if (this.isIE) return new EsriPoint(window.event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft - 2, window.event.clientY + document.body.scrollTop + document.documentElement.scrollTop - 2);
    else return new EsriPoint(e.pageX, e.pageY);
  }

  this.getEventSource = function(e) {
    if (this.isIE) return window.event.srcElement;
    else return e.target;
  }

  this.stopEvent = function(e) {
    if (this.isIE) {
      window.event.returnValue = false;
      window.event.cancelBubble = true;
    }
    else {
      e.preventDefault();
      e.stopPropagation();
    }
  }

  this.getKeyCode = function(e) {
    if (this.isIE) return window.event.keyCode;
    else return e.keyCode;
  }

  this.getElementBounds = function(e) {
    var es = e.style;
    return new EsriRectangle(this.getStyleValue(es.left), this.getStyleValue(es.top), this.getStyleValue(es.width), this.getStyleValue(es.height));
  }

  this.getElementPageBounds = function(e) {
    var eL = eT = 0;
    var elB, etB;
    var eW = e.offsetWidth;
    var eH = e.offsetHeight;
    while(e) {
      elB = 0;
      etB = 0;
      eL += e.offsetLeft;
      eT += e.offsetTop;

      if (e.style && e.style.borderWidth != "") {
        elB = parseInt(e.style.borderWidth);
        etB = parseInt(e.style.borderWidth);
      }
      else if (e.style && e.style.borderLeftWidth != "") {
        elB = parseInt(e.style.borderLeftWidth);
        etB = parseInt(e.style.borderTopWidth);
      }
      if (isNaN(elB)) elB = 0;
      if (isNaN(etB)) etB = 0;
      eL += elB;
      eT += etB;
      e = e.offsetParent;
    }
    return new EsriRectangle(eL, eT, eW, eH);
  }

  this.getPageBounds = function() {
    if (window.innerHeight) return new EsriRectangle(0, 0, window.innerWidth, window.innerHeight);
    else if (document.documentElement.clientHeight) return new EsriRectangle(0, 0, document.documentElement.clientWidth, document.documentElement.clientHeight);
    else if (document.body.clientHeight) return new EsriRectangle(0, 0, document.body.clientWidth, document.body.clientHeight);
    else return new EsriRectangle(0, 0, 0, 0);
  }

  this.isLeftButton = function(e) { return this.getMouseButton(e) == this.leftButton; }

  this.getMouseButton = function(e) {
    if (this.isIE) return window.event.button;
    else return e.which;
  }

  function camelizeStyle(name) {
    var a = name.split("-");
    var s = a[0];
    for (var c=1;c<a.length;c++) s += a[c].substring(0, 1).toUpperCase() + a[c].substring(1);
    return s;
  }

  this.setElementStyle = function(e, css) {
    var es = e.style;
    var ss = css.split(";");
    for (var i=0;i<ss.length;i++) {
      var s = ss[i].split(":");
      s[0] = s[0].trim();
      if (s[0] == "" || ! s[1]) continue;
      eval("es." + camelizeStyle(s[0]) + " = \"" + s[1].trim() + "\"");
    }
  }

  this.getElementBorders = function(e) {
    var es = e.style;
    return {
      top: parseInt(es.borderTopWidth),
      right: parseInt(es.borderRightWidth),
      bottom: parseInt(es.borderBottomWidth),
      left: parseInt(es.borderLeftWidth)
    }
  }

  this.getStyleByClassName = function(name) {
    var styleSheets = document.styleSheets;
    name = name.toLowerCase();
    for (var s=(styleSheets.length-1);s>=0;s--) {
      var rules;
      if (this.isIE) rules = styleSheets.item(s).rules;
      else rules = styleSheets.item(s).cssRules;
      for (var i=(rules.length-1);i>=0;i--) {
        if (rules.item(i).selectorText && rules.item(i).selectorText.toLowerCase() == name) return rules.item(i).style;
      }
    }
    return null;
  }

  this.removeElementStyle = function(e, css) {
    var es = e.style;
    var ss = css.split(";");
    for (var i=0;i<ss.length;i++) {
      var s = ss[i].split(":");
      s[0] = s[0].trim();
      if (s[0] == "") continue;
      if (this.isIE) es.removeAttribute(camelizeStyle(s[0]));
      else es.removeProperty(s[0]);
    }
  }

  this.setElementOpacity = function(e, o) {
    if (this.isIE) {
      var filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (o * 100) + ")";
      if (e.runtimeStyle.filter) e.runtimeStyle.filter = e.runtimeStyle.filter.replace(alphaRegExp, filter);
      else e.runtimeStyle.filter = filter;
    }
    else this.setElementStyle(e, "-moz-opacity:" + o + "; opacity:" + o + ";");
  }

  this.cloneElementStyle = function(src, target) {
    var ss = src.style;
    var ts = target.style;
    for (var s in ss) try { eval("ts." + s + " = ss." + s + ";"); } catch (e) {};
  }

  this.getElementsByClassName = function(element, className) {
    var cs = element.getElementsByTagName("*");
    var es = [];
    for (var i=0;i<cs.length;i++) {
      var c = cs.item(i);
      if (c.className.match(new RegExp("(^|\\s)" + className + "(\\s|$)"))) es.push(c);
    }
    return es;
  }

  this.removeElement = function(elem) {
    if (this.isIE) {
      if (! gc) {
        gc = document.body.appendChild(document.createElement("div"));
        this.hideElement(gc);
      }
      gc.appendChild(elem);
      gc.innerHTML = "";
    }
    else if (elem && elem.parentNode) {
      elem.parentNode.removeChild(elem);
    }
  }

  this.createImage = function(src, width, height) {
    var img;
    if (this.isIE6) {
      img = document.createElement("div");
      EsriUtils.setElementStyle(img, "width:" + width + "; height:" + height + ";");
      var s = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "')";
      if (img.runtimeStyle.filter) img.runtimeStyle.filter = img.runtimeStyle.filter.replace(alphaImageLoaderRegExp, s);
      else img.runtimeStyle.filter = s;
    }
    else {
      img = document.createElement("img")
      img.src = src;
      EsriUtils.setElementStyle(img, "width:" + width + "; height:" + height + ";");
    }
    return img;
  }

  this.setImageSrc = function(img, src) {
    img.onload = null;
    if (this.isIE6 && !src.toLowerCase().endsWith(".gif") && !src.toLowerCase().endsWith(".jpg")) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?