RoadFlow2.1 临时演示

roadui.window.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //弹出层
  2. ;RoadUI.Window = function ()
  3. {
  4. this.opts = {};
  5. var instance = this;
  6. this.open = function (options)
  7. {
  8. var defaults = {
  9. id: "",
  10. title: "",
  11. url: "",
  12. ico: "",
  13. showico: true,
  14. showclose: true,
  15. left: 0,
  16. top: 0,
  17. height: 300,
  18. width: 600,
  19. ismodal: true, //是否模态窗口
  20. zindex: 9999,
  21. resize: true,
  22. opener: null,
  23. openerid: ""
  24. };
  25. this.opts = $.extend(defaults, options);
  26. this.opts.opener = this.getOpener();
  27. this.opts.id = this.getID();
  28. var $opener = $(this.opts.opener);
  29. if ($opener == null || $opener.size() == 0)
  30. {
  31. throw "父窗口为空!"; return false;
  32. }
  33. var $openerBody = $(this.opts.opener.document.body);
  34. var $openerDocument = $(this.opts.opener.document);
  35. if ($openerBody == null || $openerBody.size() == 0)
  36. {
  37. throw "父窗口body为空!"; return false;
  38. }
  39. if (this.opts.left == 0)
  40. {
  41. this.opts.left = ($opener.width() - this.opts.width) / 2;
  42. }
  43. if (this.opts.top == 0)
  44. {
  45. this.opts.top = ($opener.height() - this.opts.height) / 2 + $(this.opts.opener.document).scrollTop();
  46. }
  47. if (this.opts.ismodal)//如果是模态窗口,则添加遮罩层
  48. {
  49. var $maskdiv = $('<div id="' + this.opts.id + '_maskdiv" class="window_maskdiv" style="z-index:' + (this.opts.zindex - 1) + ';"></div>', $openerDocument);
  50. $maskdiv.width($opener.width()).height(Math.max($openerBody.get(0).clientHeight, $opener.height()));
  51. $openerBody.append($maskdiv);
  52. }
  53. var $maindiv = $('<div id="' + this.opts.id + '" class="window_maindiv" style="left:' + this.opts.left + 'px;top:' + this.opts.top + 'px;width:' + this.opts.width + 'px;height:' + this.opts.height + 'px;z-index:' + this.opts.zindex + ';"></div>', $openerDocument);
  54. var $titlediv = $('<div id="' + this.opts.id + '_titlediv" class="window_title"></div>', $openerDocument);
  55. var $bodydiv = $('<div class="window_body"></div>', $openerDocument);
  56. var $resizediv = null;
  57. var $titlediv_title = $('<div class="' + (this.opts.showico ? 'window_title_title_ico' : 'window_title_title') + '">' + this.opts.title + '</div>', $openerDocument);
  58. if (this.opts.showico && this.opts.ico && $.trim(this.opts.ico).length > 0)
  59. {
  60. $titlediv_title.css({ 'background-image': 'url(' + this.opts.ico + ')' });
  61. }
  62. //双击关闭窗口
  63. $titlediv_title.bind("dblclick", function () { instance.close($(this).parent().parent().attr("id")); });
  64. var $titlediv_button = $();
  65. if (this.opts.showclose)
  66. {
  67. $titlediv_button = $('<div class="window_title_button">&nbsp;</div>', $openerDocument);
  68. $titlediv_button.bind("mouseover", function ()
  69. {
  70. $(this).removeClass().addClass("window_title_button1");
  71. }).bind("mouseout", function ()
  72. {
  73. $(this).removeClass().addClass("window_title_button");
  74. }).bind("click", function ()
  75. {
  76. instance.close($(this).parent().parent().attr("id"));
  77. });
  78. }
  79. $titlediv.append($titlediv_title, $titlediv_button, '<div style="clear:both;"></div>');
  80. var bodydivHeight = this.opts.height - (this.opts.resize ? 39 : 26);
  81. $bodydiv.css({ "height": bodydivHeight + "px" });
  82. if (this.opts.url && $.trim(this.opts.url).length > 0)
  83. {
  84. var url = this.opts.url;
  85. if (url.indexOf('?') >= 0)
  86. {
  87. url += "&iframeid=" + this.opts.id + "&openerid=" + this.opts.openerid;
  88. }
  89. else
  90. {
  91. url += "?iframeid=" + this.opts.id + "&openerid=" + this.opts.openerid;
  92. }
  93. var $iframe = $('<iframe id="' + this.opts.id + '_iframe" name="' + this.opts.id + '_iframe" src="' + url + '" frameborder="0" marginheight="0" marginwidth="0" border="0" style="border:none 0;margin:0;padding:0;width:100%;height:' + bodydivHeight + 'px;"></iframe>', $openerDocument);
  94. if (this.opts.title.isNullOrEmpty())
  95. {
  96. $iframe.bind("load", function ()
  97. {
  98. if (!instance.opts.title || $.trim(instance.opts.title).length == 0)
  99. {
  100. var title = "";
  101. try
  102. {
  103. title = $("head title", $(this).contents()).html();
  104. } catch (e) { }
  105. instance.setTitle(title);
  106. }
  107. });
  108. }
  109. $bodydiv.append($iframe);
  110. }
  111. $maindiv.append($titlediv, $bodydiv);
  112. if (this.opts.resize)
  113. {
  114. $resizediv = $('<div class="window_resize"><div class="window_resize_img">&nbsp;</div></div>', $openerDocument);
  115. $maindiv.append($resizediv);
  116. }
  117. $openerBody.append($maindiv);
  118. var maindiv = $maindiv.get(0);
  119. var titlediv = $titlediv.get(0);
  120. var resizediv = $resizediv ? $resizediv.get(0) : null;
  121. new draging
  122. (
  123. titlediv,
  124. function ()
  125. {
  126. return { x: maindiv.offsetLeft, y: maindiv.offsetTop };
  127. },
  128. function (x, y)
  129. {
  130. win = instance.opts.opener || top;
  131. var iSTop = win.document.body.scrollTop || win.document.documentElement.scrollTop;
  132. if (x < 0)
  133. {
  134. x = 0;
  135. }
  136. else if (x + maindiv.offsetWidth > win.document.documentElement.clientWidth)
  137. {
  138. x = win.document.body.clientWidth - maindiv.offsetWidth;
  139. }
  140. if (y < iSTop)
  141. {
  142. y = iSTop;
  143. }
  144. else if (y + maindiv.offsetHeight > win.document.documentElement.clientHeight + iSTop)
  145. {
  146. y = win.document.documentElement.clientHeight - maindiv.offsetHeight + iSTop;
  147. }
  148. maindiv.style.left = x + 'px';
  149. maindiv.style.top = y + 'px';
  150. },
  151. instance.opts.opener
  152. );
  153. if (this.opts.resize)
  154. {
  155. var resizediv = $resizediv.get(0);
  156. new draging
  157. (
  158. resizediv,
  159. function ()
  160. {
  161. g_orgTop = maindiv.offsetTop;
  162. g_orgHeight = maindiv.offsetHeight;
  163. g_orgLeft = maindiv.offsetLeft;
  164. g_orgWidth = maindiv.offsetWidth;
  165. return { x: maindiv.offsetWidth, y: maindiv.offsetHeight };
  166. },
  167. function (x, y)
  168. {
  169. doBothDrag(x, y, maindiv);
  170. },
  171. instance.opts.opener
  172. );
  173. };
  174. return this.opts.id;
  175. };
  176. this.getOpener = function ()
  177. {
  178. if (this.opts.opener)
  179. {
  180. return this.opts.opener;
  181. }
  182. if (this.opts.openerid)
  183. {
  184. var iframes = top.frames;
  185. for (var i = 0; i < iframes.length; i++)
  186. {
  187. if ($(iframes[i]).attr("name") == this.opts.openerid)
  188. {
  189. return iframes[i];
  190. }
  191. }
  192. }
  193. return window.top;
  194. };
  195. this.getID = function ()
  196. {
  197. var id = this.opts.id != null && this.opts.id != undefined && $.trim(this.opts.id).length > 0 ? this.opts.id : "roadui_window_" + Math.random().toString();
  198. return id.replaceAll('.', '');
  199. };
  200. this.setTitle = function (title)
  201. {
  202. this.opts.title = title;
  203. var mainid = this.opts.id;
  204. var $titlediv = $(">div:first", $("#" + mainid + "_titlediv", $(this.opts.opener.document)));
  205. if ($titlediv == null || $titlediv.size() == 0)
  206. {
  207. return false;
  208. }
  209. $titlediv.text(title);
  210. return true;
  211. };
  212. this.close = function (id)
  213. {
  214. if (!id || id.trim().length == 0)
  215. {
  216. id = RoadUI.Core.query("iframeid");
  217. }
  218. return closeWindow(id);
  219. };
  220. this.closeAll = function ()
  221. {
  222. return closeWindow();
  223. };
  224. function closeWindow(id)
  225. {
  226. var amount = 0;
  227. var $maindiv = !id || id.trim().length == 0 ? $("div[id^='roadui_window_']", top.document) : $("#" + id, top.document);
  228. for (var x = 0; x < $maindiv.size(); x++)
  229. {
  230. try
  231. {
  232. $maindiv.eq(x).find("iframe").attr("src", "about:blank");
  233. CollectGarbage();
  234. } catch (e) { }
  235. try
  236. {
  237. $("#" + $maindiv.eq(x).attr("id") + "_maskdiv", top.document).remove();
  238. $maindiv.eq(x).find("iframe").remove();
  239. $maindiv.eq(x).remove();
  240. amount++;
  241. } catch (e) { }
  242. }
  243. for (var i = 0; i < top.frames.length; i++)
  244. {
  245. var $maindiv1 = !id || id.trim().length == 0 ? $("div[id^='roadui_window_']", top.frames[i].document) : $("#" + id, top.frames[i].document);
  246. for (var j = 0; j < $maindiv1.size(); j++)
  247. {
  248. try
  249. {
  250. $maindiv1.eq(j).find("iframe").attr("src", "about:blank");
  251. CollectGarbage();
  252. } catch (e) { }
  253. try
  254. {
  255. $("#" + $maindiv1.eq(j).attr("id") + "_maskdiv", top.frames[i].document).remove();
  256. $maindiv1.eq(j).find("iframe").remove();
  257. $maindiv1.eq(j).remove();
  258. amount++;
  259. } catch (e) { }
  260. }
  261. }
  262. return amount;
  263. };
  264. var iframesArray = new Array();
  265. this.getOpenerElement = function (id)
  266. {
  267. iframesArray = new Array();
  268. var openerid = RoadUI.Core.query("openerid") || "";
  269. if (openerid && openerid.length > 0)
  270. {
  271. openerid += "_iframe";
  272. }
  273. var ele = $();
  274. var iframes = $(top.document).find("iframe");
  275. if (openerid && openerid.length > 0)
  276. {
  277. for (var i = iframes.size() - 1; i >= 0; i--)
  278. {
  279. if (openerid && openerid.length > 0 && openerid == iframes.eq(i).attr("id"))
  280. {
  281. var obj = iframes.eq(i).get(0).contentWindow.document.getElementById(id);
  282. if (obj)
  283. {
  284. return $(obj);
  285. }
  286. }
  287. }
  288. }
  289. if (ele.size() == 0)
  290. {
  291. addIframe(top.document);
  292. for (var i = iframesArray.length - 1; i >= 0; i--)
  293. {
  294. var obj = iframesArray[i].contentWindow.document.getElementById(id);
  295. if (obj)
  296. {
  297. iframesArray = [];
  298. return $(obj);
  299. }
  300. }
  301. }
  302. return ele;
  303. };
  304. var addIframe = function (doc)
  305. {
  306. var iframes = $(doc).find("iframe");
  307. for (var i = 0; i < iframes.size(); i++)
  308. {
  309. iframesArray.push(iframes.eq(i).get(0));
  310. addIframe(iframes.eq(i).get(0).contentWindow.document);
  311. }
  312. };
  313. this.reloadOpener = function (url, id)
  314. {
  315. if (!id || id.trim().length == 0)
  316. {
  317. id = RoadUI.Core.query("openerid");
  318. }
  319. id += "_iframe";
  320. $("iframe", top.document).each(function ()
  321. {
  322. if (id == $(this).attr("id"))
  323. {
  324. var win = $(this).get(0).contentWindow;
  325. win.location = !url || $.trim(url).length == 0 ? win.location : url;
  326. }
  327. });
  328. for (var i = 0; i < top.frames.length; i++)
  329. {
  330. $("iframe", top.frames[i].document).each(function ()
  331. {
  332. if (id == $(this).attr("id"))
  333. {
  334. var win = $(this).get(0).contentWindow;
  335. win.location = !url || $.trim(url).length == 0 ? win.location : url;
  336. }
  337. });
  338. }
  339. };
  340. this.resize = function (width, height)
  341. {
  342. if (!width || !height)
  343. {
  344. return;
  345. }
  346. var $maindiv = $("#" + this.opts.id, $(this.opts.opener.document));
  347. if ($maindiv == null || $maindiv.size() == 0)
  348. {
  349. return;
  350. }
  351. $maindiv.css({ "height": height + "px", "width": width + "px" });
  352. var $bodydiv = $(".window_body", $maindiv);
  353. if ($bodydiv == null || $bodydiv.size() == 0)
  354. {
  355. return;
  356. }
  357. var bodydivHeight = height - (this.opts.resize ? 39 : 26);
  358. $bodydiv.css({ "height": bodydivHeight + "px" });
  359. var $iframe = $bodydiv.children().first();
  360. if ($iframe && $iframe.size() > 0 && $iframe.get(0).nodeName.toLowerCase() == "iframe")
  361. {
  362. $iframe.css({ "height": bodydivHeight + "px" });
  363. }
  364. }
  365. var doBothDrag = function (x, y, maindiv)
  366. {
  367. if (x < 110)
  368. {
  369. x = 110;
  370. }
  371. maindiv.style.width = (x - 8) + 'px';
  372. if (y < 35)
  373. {
  374. y = 35;
  375. }
  376. maindiv.style.height = (y - 8) + 'px';
  377. instance.resize(x - 8, y - 8);
  378. };
  379. var draging = function (oElement, fnGetPos, fnOnDrag, win)
  380. {
  381. win = win || window;
  382. var obj = this;
  383. this.oElement = oElement;
  384. this.fnGetPos = fnGetPos;
  385. this.fnOnDrag = fnOnDrag;
  386. this.__oStartOffset__ = { x: 0, y: 0 };
  387. this.fnOnMouseUp = function (ev)
  388. {
  389. obj.stopDrag(win.event || ev);
  390. };
  391. this.fnOnMouseMove = function (ev)
  392. {
  393. obj.doDrag(win.event || ev);
  394. };
  395. this.oElement.onmousedown = function (ev)
  396. {
  397. obj.startDrag(win.event || ev);
  398. };
  399. }
  400. draging.prototype.startDrag = function (oEvent)
  401. {
  402. var oPos = this.fnGetPos();
  403. var x = oEvent.clientX;
  404. var y = oEvent.clientY;
  405. this.__oStartOffset__.x = x - oPos.x;
  406. this.__oStartOffset__.y = y - oPos.y;
  407. if (this.oElement.setCapture)
  408. {
  409. this.oElement.setCapture();
  410. this.oElement.onmouseup = this.fnOnMouseUp;
  411. this.oElement.onmousemove = this.fnOnMouseMove;
  412. }
  413. else
  414. {
  415. document.addEventListener("mouseup", this.fnOnMouseUp, true);
  416. document.addEventListener("mousemove", this.fnOnMouseMove, true);
  417. window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
  418. }
  419. };
  420. draging.prototype.stopDrag = function (oEvent)
  421. {
  422. if (this.oElement.releaseCapture)
  423. {
  424. this.oElement.releaseCapture();
  425. this.oElement.onmouseup = null;
  426. this.oElement.onmousemove = null;
  427. }
  428. else
  429. {
  430. document.removeEventListener("mouseup", this.fnOnMouseUp, true);
  431. document.removeEventListener("mousemove", this.fnOnMouseMove, true);
  432. window.releaseEvents(Event.MOUSE_MOVE | Event.MOUSE_UP);
  433. }
  434. };
  435. draging.prototype.doDrag = function (oEvent)
  436. {
  437. var x = oEvent.clientX;
  438. var y = oEvent.clientY;
  439. this.fnOnDrag(x - this.__oStartOffset__.x, y - this.__oStartOffset__.y);
  440. };
  441. }