msgbox.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. window.ZENG=window.ZENG || {};
  2. ZENG.dom = {getById: function(id) {
  3. return document.getElementById(id);
  4. },get: function(e) {
  5. return (typeof (e) == "string") ? document.getElementById(e) : e;
  6. },createElementIn: function(tagName, elem, insertFirst, attrs) {
  7. var _e = (elem = ZENG.dom.get(elem) || document.body).ownerDocument.createElement(tagName || "div"), k;
  8. if (typeof (attrs) == 'object') {
  9. for (k in attrs) {
  10. if (k == "class") {
  11. _e.className = attrs[k];
  12. } else if (k == "style") {
  13. _e.style.cssText = attrs[k];
  14. } else {
  15. _e[k] = attrs[k];
  16. }
  17. }
  18. }
  19. insertFirst ? elem.insertBefore(_e, elem.firstChild) : elem.appendChild(_e);
  20. return _e;
  21. },getStyle: function(el, property) {
  22. el = ZENG.dom.get(el);
  23. if (!el || el.nodeType == 9) {
  24. return null;
  25. }
  26. var w3cMode = document.defaultView && document.defaultView.getComputedStyle, computed = !w3cMode ? null : document.defaultView.getComputedStyle(el, ''), value = "";
  27. switch (property) {
  28. case "float":
  29. property = w3cMode ? "cssFloat" : "styleFloat";
  30. break;
  31. case "opacity":
  32. if (!w3cMode) {
  33. var val = 100;
  34. try {
  35. val = el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
  36. } catch (e) {
  37. try {
  38. val = el.filters('alpha').opacity;
  39. } catch (e) {
  40. }
  41. }
  42. return val / 100;
  43. } else {
  44. return parseFloat((computed || el.style)[property]);
  45. }
  46. break;
  47. case "backgroundPositionX":
  48. if (w3cMode) {
  49. property = "backgroundPosition";
  50. return ((computed || el.style)[property]).split(" ")[0];
  51. }
  52. break;
  53. case "backgroundPositionY":
  54. if (w3cMode) {
  55. property = "backgroundPosition";
  56. return ((computed || el.style)[property]).split(" ")[1];
  57. }
  58. break;
  59. }
  60. if (w3cMode) {
  61. return (computed || el.style)[property];
  62. } else {
  63. return (el.currentStyle[property] || el.style[property]);
  64. }
  65. },setStyle: function(el, properties, value) {
  66. if (!(el = ZENG.dom.get(el)) || el.nodeType != 1) {
  67. return false;
  68. }
  69. var tmp, bRtn = true, w3cMode = (tmp = document.defaultView) && tmp.getComputedStyle, rexclude = /z-?index|font-?weight|opacity|zoom|line-?height/i;
  70. if (typeof (properties) == 'string') {
  71. tmp = properties;
  72. properties = {};
  73. properties[tmp] = value;
  74. }
  75. for (var prop in properties) {
  76. value = properties[prop];
  77. if (prop == 'float') {
  78. prop = w3cMode ? "cssFloat" : "styleFloat";
  79. } else if (prop == 'opacity') {
  80. if (!w3cMode) {
  81. prop = 'filter';
  82. value = value >= 1 ? '' : ('alpha(opacity=' + Math.round(value * 100) + ')');
  83. }
  84. } else if (prop == 'backgroundPositionX' || prop == 'backgroundPositionY') {
  85. tmp = prop.slice(-1) == 'X' ? 'Y' : 'X';
  86. if (w3cMode) {
  87. var v = ZENG.dom.getStyle(el, "backgroundPosition" + tmp);
  88. prop = 'backgroundPosition';
  89. typeof (value) == 'number' && (value = value + 'px');
  90. value = tmp == 'Y' ? (value + " " + (v || "top")) : ((v || 'left') + " " + value);
  91. }
  92. }
  93. if (typeof el.style[prop] != "undefined") {
  94. el.style[prop] = value + (typeof value === "number" && !rexclude.test(prop) ? 'px' : '');
  95. bRtn = bRtn && true;
  96. } else {
  97. bRtn = bRtn && false;
  98. }
  99. }
  100. return bRtn;
  101. },getScrollTop: function(doc) {
  102. var _doc = doc || document;
  103. return Math.max(_doc.documentElement.scrollTop, _doc.body.scrollTop);
  104. },getClientHeight: function(doc) {
  105. var _doc = doc || document;
  106. return _doc.compatMode == "CSS1Compat" ? _doc.documentElement.clientHeight : _doc.body.clientHeight;
  107. }
  108. };
  109. ZENG.string = {RegExps: {trim: /^\s+|\s+$/g,ltrim: /^\s+/,rtrim: /\s+$/,nl2br: /\n/g,s2nb: /[\x20]{2}/g,URIencode: /[\x09\x0A\x0D\x20\x21-\x29\x2B\x2C\x2F\x3A-\x3F\x5B-\x5E\x60\x7B-\x7E]/g,escHTML: {re_amp: /&/g,re_lt: /</g,re_gt: />/g,re_apos: /\x27/g,re_quot: /\x22/g},escString: {bsls: /\\/g,sls: /\//g,nl: /\n/g,rt: /\r/g,tab: /\t/g},restXHTML: {re_amp: /&amp;/g,re_lt: /&lt;/g,re_gt: /&gt;/g,re_apos: /&(?:apos|#0?39);/g,re_quot: /&quot;/g},write: /\{(\d{1,2})(?:\:([xodQqb]))?\}/g,isURL: /^(?:ht|f)tp(?:s)?\:\/\/(?:[\w\-\.]+)\.\w+/i,cut: /[\x00-\xFF]/,getRealLen: {r0: /[^\x00-\xFF]/g,r1: /[\x00-\xFF]/g},format: /\{([\d\w\.]+)\}/g},commonReplace: function(s, p, r) {
  110. return s.replace(p, r);
  111. },format: function(str) {
  112. var args = Array.prototype.slice.call(arguments), v;
  113. str = String(args.shift());
  114. if (args.length == 1 && typeof (args[0]) == 'object') {
  115. args = args[0];
  116. }
  117. ZENG.string.RegExps.format.lastIndex = 0;
  118. return str.replace(ZENG.string.RegExps.format, function(m, n) {
  119. v = ZENG.object.route(args, n);
  120. return v === undefined ? m : v;
  121. });
  122. }};
  123. ZENG.object = {
  124. routeRE: /([\d\w_]+)/g,
  125. route: function(obj, path) {
  126. obj = obj || {};
  127. path = String(path);
  128. var r = ZENG.object.routeRE, m;
  129. r.lastIndex = 0;
  130. while ((m = r.exec(path)) !== null) {
  131. obj = obj[m[0]];
  132. if (obj === undefined || obj === null) {
  133. break;
  134. }
  135. }
  136. return obj;
  137. }};
  138. var ua = ZENG.userAgent = {}, agent = navigator.userAgent;
  139. ua.ie = 9 - ((agent.indexOf('Trident/5.0') > -1) ? 0 : 1) - (window.XDomainRequest ? 0 : 1) - (window.XMLHttpRequest ? 0 : 1);
  140. if (typeof (ZENG.msgbox) == 'undefined') {
  141. ZENG.msgbox = {};
  142. }
  143. ZENG.msgbox._timer = null;
  144. ZENG.msgbox.loadingAnimationPath = ZENG.msgbox.loadingAnimationPath || ("loading.gif");
  145. ZENG.msgbox.show = function(msgHtml, type, timeout, opts) {
  146. if (typeof (opts) == 'number') {
  147. opts = {topPosition: opts};
  148. }
  149. opts = opts || {};
  150. var _s = ZENG.msgbox,
  151. template = '<span class="zeng_msgbox_layer" style="display:none; z-index:10000;" id="mode_tips_v2"><span class="gtl_ico_{type}"></span>{loadIcon}{msgHtml}<span class="gtl_end"></span></span>',
  152. loading = '<span class="gtl_ico_loading"></span>',
  153. typeClass = [0, 0, 0, 0, "succ", "fail", "clear"],
  154. mBox,
  155. tips;
  156. _s._loadCss && _s._loadCss(opts.cssPath);
  157. mBox = ZENG.dom.get("q_Msgbox") || ZENG.dom.createElementIn("div", document.body, false, {className: "zeng_msgbox_layer_wrap"});
  158. mBox.id = "q_Msgbox";
  159. mBox.style.display = "";
  160. mBox.innerHTML = ZENG.string.format(template, {type: typeClass[type] || "hits",msgHtml: msgHtml || "",loadIcon: type == 6 ? loading : ""});
  161. _s._setPosition(mBox, timeout, opts.topPosition);
  162. };
  163. ZENG.msgbox._setPosition = function(tips, timeout, topPosition) {
  164. timeout = timeout || 5000;
  165. var _s = ZENG.msgbox, bt = ZENG.dom.getScrollTop(), ch = ZENG.dom.getClientHeight(), t = Math.floor(ch / 2) - 40;
  166. ZENG.dom.setStyle(tips, "top", ((document.compatMode == "BackCompat" || ZENG.userAgent.ie < 7) ? bt : 0) + ((typeof (topPosition) == "number") ? topPosition : t) + "px");
  167. clearTimeout(_s._timer);
  168. tips.firstChild.style.display = "";
  169. timeout && (_s._timer = setTimeout(_s.hide, timeout));
  170. };
  171. ZENG.msgbox.hide = function(timeout) {
  172. var _s = ZENG.msgbox;
  173. if (timeout) {
  174. clearTimeout(_s._timer);
  175. _s._timer = setTimeout(_s._hide, timeout);
  176. } else {
  177. _s._hide();
  178. }
  179. };
  180. ZENG.msgbox._hide = function() {
  181. var _mBox = ZENG.dom.get("q_Msgbox"), _s = ZENG.msgbox;
  182. clearTimeout(_s._timer);
  183. if (_mBox) {
  184. var _tips = _mBox.firstChild;
  185. ZENG.dom.setStyle(_mBox, "display", "none");
  186. }
  187. };