mcwindows.js
上传用户:dlqqsh
上传日期:2021-11-13
资源大小:7840k
文件大小:14k
源码类别:

OA系统

开发平台:

Java

  1. /**
  2.  * $Id: mcwindows.js 18 2006-06-29 14:11:23Z spocke $
  3.  *
  4.  * Moxiecode DHTML Windows script.
  5.  *
  6.  * @author Moxiecode
  7.  * @copyright Copyright  2004, Moxiecode Systems AB, All rights reserved.
  8.  */
  9. // Windows handler
  10. function MCWindows() {
  11. this.settings = new Array();
  12. this.windows = new Array();
  13. this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
  14. this.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
  15. this.isSafari = navigator.userAgent.indexOf('Safari') != -1;
  16. this.isMac = navigator.userAgent.indexOf('Mac') != -1;
  17. this.isMSIE5_0 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
  18. this.action = "none";
  19. this.selectedWindow = null;
  20. this.zindex = 100;
  21. this.mouseDownScreenX = 0;
  22. this.mouseDownScreenY = 0;
  23. this.mouseDownLayerX = 0;
  24. this.mouseDownLayerY = 0;
  25. this.mouseDownWidth = 0;
  26. this.mouseDownHeight = 0;
  27. };
  28. MCWindows.prototype.init = function(settings) {
  29. this.settings = settings;
  30. if (this.isMSIE)
  31. this.addEvent(document, "mousemove", mcWindows.eventDispatcher);
  32. else
  33. this.addEvent(window, "mousemove", mcWindows.eventDispatcher);
  34. this.addEvent(document, "mouseup", mcWindows.eventDispatcher);
  35. };
  36. MCWindows.prototype.getParam = function(name, default_value) {
  37. var value = null;
  38. value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name];
  39. // Fix bool values
  40. if (value == "true" || value == "false")
  41. return (value == "true");
  42. return value;
  43. };
  44. MCWindows.prototype.eventDispatcher = function(e) {
  45. e = typeof(e) == "undefined" ? window.event : e;
  46. if (mcWindows.selectedWindow == null)
  47. return;
  48. // Switch focus
  49. if (mcWindows.isGecko && e.type == "mousedown") {
  50. var elm = e.currentTarget;
  51. for (var n in mcWindows.windows) {
  52. var win = mcWindows.windows[n];
  53. if (typeof(win) == 'function')
  54. continue;
  55. if (win.headElement == elm || win.resizeElement == elm) {
  56. win.focus();
  57. break;
  58. }
  59. }
  60. }
  61. switch (e.type) {
  62. case "mousemove":
  63. mcWindows.selectedWindow.onMouseMove(e);
  64. break;
  65. case "mouseup":
  66. mcWindows.selectedWindow.onMouseUp(e);
  67. break;
  68. case "mousedown":
  69. mcWindows.selectedWindow.onMouseDown(e);
  70. break;
  71. case "focus":
  72. mcWindows.selectedWindow.onFocus(e);
  73. break;
  74. }
  75. }
  76. MCWindows.prototype.addEvent = function(obj, name, handler) {
  77. if (this.isMSIE)
  78. obj.attachEvent("on" + name, handler);
  79. else
  80. obj.addEventListener(name, handler, true);
  81. };
  82. MCWindows.prototype.cancelEvent = function(e) {
  83. if (this.isMSIE) {
  84. e.returnValue = false;
  85. e.cancelBubble = true;
  86. } else
  87. e.preventDefault();
  88. };
  89. MCWindows.prototype.parseFeatures = function(opts) {
  90. // Cleanup the options
  91. opts = opts.toLowerCase();
  92. opts = opts.replace(/;/g, ",");
  93. opts = opts.replace(/[^0-9a-z=,]/g, "");
  94. var optionChunks = opts.split(',');
  95. var options = new Array();
  96. options['left'] = 10;
  97. options['top'] = 10;
  98. options['width'] = 300;
  99. options['height'] = 300;
  100. options['resizable'] = true;
  101. options['minimizable'] = true;
  102. options['maximizable'] = true;
  103. options['close'] = true;
  104. options['movable'] = true;
  105. if (opts == "")
  106. return options;
  107. for (var i=0; i<optionChunks.length; i++) {
  108. var parts = optionChunks[i].split('=');
  109. if (parts.length == 2)
  110. options[parts[0]] = parts[1];
  111. }
  112. return options;
  113. };
  114. MCWindows.prototype.open = function(url, name, features) {
  115. var win = new MCWindow();
  116. var winDiv, html = "", id;
  117. features = this.parseFeatures(features);
  118. // Create div
  119. id = "mcWindow_" + name;
  120. width = parseInt(features['width']);
  121. height = parseInt(features['height'])-12-19;
  122. if (this.isMSIE)
  123. width -= 2;
  124. // Setup first part of window
  125. win.id = id;
  126. win.url = url;
  127. win.name = name;
  128. win.features = features;
  129. this.windows[name] = win;
  130. iframeWidth = width;
  131. iframeHeight = height;
  132. // Create inner content
  133. html += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
  134. html += '<html>';
  135. html += '<head>';
  136. html += '<title>Wrapper iframe</title>';
  137. html += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
  138. html += '<link href="../iXs_Editor/themes/advanced/css/editor_ui.css" rel="stylesheet" type="text/css" />';
  139. html += '</head>';
  140. html += '<body onload="parent.mcWindows.onLoad('' + name + '');">';
  141. html += '<div id="' + id + '_container" class="mceWindow">';
  142. html += '<div id="' + id + '_head" class="mceWindowHead" onmousedown="parent.mcWindows.windows['' + name + ''].focus();">';
  143. html += '  <div id="' + id + '_title" class="mceWindowTitle"';
  144. html += '  onselectstart="return false;" unselectable="on" style="-moz-user-select: none !important;">No name window</div>';
  145. html += '    <div class="mceWindowHeadTools">';
  146. html += '      <a href="javascript:parent.mcWindows.windows['' + name + ''].close();" onmousedown="return false;" class="mceWindowClose"><img border="0" src="../iXs_Editor/themes/advanced/images/window_close.gif" /></a>';
  147. // html += '      <a href="javascript:mcWindows.windows['' + name + ''].maximize();" onmousedown="return false;" class="mceWindowMaximize"></a>';
  148. // html += '      <a href="javascript:mcWindows.windows['' + name + ''].minimize();" onmousedown="return false;" class="mceWindowMinimize"></a>';
  149. html += '    </div>';
  150. html += '</div><div id="' + id + '_body" class="mceWindowBody" style="width: ' + width + 'px; height: ' + height + 'px;">';
  151. html += '<iframe id="' + id + '_iframe" name="' + id + '_iframe" onfocus="parent.mcWindows.windows['' + name + ''].focus();" frameborder="0" width="' + iframeWidth + '" height="' + iframeHeight + '" src="' + url + '" class="mceWindowBodyIframe"></iframe></div>';
  152. html += '<div id="' + id + '_statusbar" class="mceWindowStatusbar" onmousedown="parent.mcWindows.windows['' + name + ''].focus();">';
  153. html += '<div id="' + id + '_resize" class="mceWindowResize"><img onmousedown="parent.mcWindows.windows['' + name + ''].focus();" border="0" src="../iXs_Editor/themes/advanced/images/window_resize.gif" /></div>';
  154. html += '</div>';
  155. html += '</div>';
  156. html += '</body>';
  157. html += '</html>';
  158. // Create iframe
  159. this.createFloatingIFrame(id, features['left'], features['top'], features['width'], features['height'], html);
  160. };
  161. // Gets called when wrapper iframe is initialized
  162. MCWindows.prototype.onLoad = function(name) {
  163. var win = mcWindows.windows[name];
  164. var id = "mcWindow_" + name;
  165. var wrapperIframe = window.frames[id + "_iframe"].frames[0];
  166. var wrapperDoc = window.frames[id + "_iframe"].document;
  167. var doc = window.frames[id + "_iframe"].document;
  168. var winDiv = document.getElementById("mcWindow_" + name + "_div");
  169. var realIframe = window.frames[id + "_iframe"].frames[0];
  170. // Set window data
  171. win.id = "mcWindow_" + name + "_iframe";
  172. win.winElement = winDiv;
  173. win.bodyElement = doc.getElementById(id + '_body');
  174. win.iframeElement = doc.getElementById(id + '_iframe');
  175. win.headElement = doc.getElementById(id + '_head');
  176. win.titleElement = doc.getElementById(id + '_title');
  177. win.resizeElement = doc.getElementById(id + '_resize');
  178. win.containerElement = doc.getElementById(id + '_container');
  179. win.left = win.features['left'];
  180. win.top = win.features['top'];
  181. win.frame = window.frames[id + '_iframe'].frames[0];
  182. win.wrapperFrame = window.frames[id + '_iframe'];
  183. win.wrapperIFrameElement = document.getElementById(id + "_iframe");
  184. // Add event handlers
  185. mcWindows.addEvent(win.headElement, "mousedown", mcWindows.eventDispatcher);
  186. mcWindows.addEvent(win.resizeElement, "mousedown", mcWindows.eventDispatcher);
  187. if (mcWindows.isMSIE) {
  188. mcWindows.addEvent(realIframe.document, "mousemove", mcWindows.eventDispatcher);
  189. mcWindows.addEvent(realIframe.document, "mouseup", mcWindows.eventDispatcher);
  190. } else {
  191. mcWindows.addEvent(realIframe, "mousemove", mcWindows.eventDispatcher);
  192. mcWindows.addEvent(realIframe, "mouseup", mcWindows.eventDispatcher);
  193. mcWindows.addEvent(realIframe, "focus", mcWindows.eventDispatcher);
  194. }
  195. for (var i=0; i<window.frames.length; i++) {
  196. if (!window.frames[i]._hasMouseHandlers) {
  197. if (mcWindows.isMSIE) {
  198. mcWindows.addEvent(window.frames[i].document, "mousemove", mcWindows.eventDispatcher);
  199. mcWindows.addEvent(window.frames[i].document, "mouseup", mcWindows.eventDispatcher);
  200. } else {
  201. mcWindows.addEvent(window.frames[i], "mousemove", mcWindows.eventDispatcher);
  202. mcWindows.addEvent(window.frames[i], "mouseup", mcWindows.eventDispatcher);
  203. }
  204. window.frames[i]._hasMouseHandlers = true;
  205. }
  206. }
  207. if (mcWindows.isMSIE) {
  208. mcWindows.addEvent(win.frame.document, "mousemove", mcWindows.eventDispatcher);
  209. mcWindows.addEvent(win.frame.document, "mouseup", mcWindows.eventDispatcher);
  210. } else {
  211. mcWindows.addEvent(win.frame, "mousemove", mcWindows.eventDispatcher);
  212. mcWindows.addEvent(win.frame, "mouseup", mcWindows.eventDispatcher);
  213. mcWindows.addEvent(win.frame, "focus", mcWindows.eventDispatcher);
  214. }
  215. this.selectedWindow = win;
  216. };
  217. MCWindows.prototype.createFloatingIFrame = function(id_prefix, left, top, width, height, html) {
  218. var iframe = document.createElement("iframe");
  219. var div = document.createElement("div");
  220. width = parseInt(width);
  221. height = parseInt(height)+1;
  222. // Create wrapper div
  223. div.setAttribute("id", id_prefix + "_div");
  224. div.setAttribute("width", width);
  225. div.setAttribute("height", (height));
  226. div.style.position = "absolute";
  227. div.style.left = left + "px";
  228. div.style.top = top + "px";
  229. div.style.width = width + "px";
  230. div.style.height = (height) + "px";
  231. div.style.backgroundColor = "white";
  232. div.style.display = "none";
  233. if (this.isGecko) {
  234. iframeWidth = width + 2;
  235. iframeHeight = height + 2;
  236. } else {
  237. iframeWidth = width;
  238. iframeHeight = height + 1;
  239. }
  240. // Create iframe
  241. iframe.setAttribute("id", id_prefix + "_iframe");
  242. iframe.setAttribute("name", id_prefix + "_iframe");
  243. iframe.setAttribute("border", "0");
  244. iframe.setAttribute("frameBorder", "0");
  245. iframe.setAttribute("marginWidth", "0");
  246. iframe.setAttribute("marginHeight", "0");
  247. iframe.setAttribute("leftMargin", "0");
  248. iframe.setAttribute("topMargin", "0");
  249. iframe.setAttribute("width", iframeWidth);
  250. iframe.setAttribute("height", iframeHeight);
  251. // iframe.setAttribute("src", "../iXs_Editor/blank.htm");
  252. // iframe.setAttribute("allowtransparency", "false");
  253. iframe.setAttribute("scrolling", "no");
  254. iframe.style.width = iframeWidth + "px";
  255. iframe.style.height = iframeHeight + "px";
  256. iframe.style.backgroundColor = "white";
  257. div.appendChild(iframe);
  258. document.body.appendChild(div);
  259. // Fixed MSIE 5.0 issue
  260. div.innerHTML = div.innerHTML;
  261. if (this.isSafari) {
  262. // Give Safari some time to setup
  263. window.setTimeout(function() {
  264. doc = window.frames[id_prefix + '_iframe'].document;
  265. doc.open();
  266. doc.write(html);
  267. doc.close();
  268. }, 10);
  269. } else {
  270. doc = window.frames[id_prefix + '_iframe'].window.document
  271. doc.open();
  272. doc.write(html);
  273. doc.close();
  274. }
  275. div.style.display = "block";
  276. return div;
  277. };
  278. // Window instance
  279. function MCWindow() {
  280. };
  281. MCWindow.prototype.focus = function() {
  282. this.winElement.style.zIndex = mcWindows.zindex++;
  283. mcWindows.selectedWindow = this;
  284. };
  285. MCWindow.prototype.minimize = function() {
  286. };
  287. MCWindow.prototype.maximize = function() {
  288. };
  289. MCWindow.prototype.startResize = function() {
  290. mcWindows.action = "resize";
  291. };
  292. MCWindow.prototype.startMove = function(e) {
  293. mcWindows.action = "move";
  294. };
  295. MCWindow.prototype.close = function() {
  296. document.body.removeChild(this.winElement);
  297. mcWindows.windows[this.name] = null;
  298. };
  299. MCWindow.prototype.onMouseMove = function(e) {
  300. var scrollX = 0;//this.doc.body.scrollLeft;
  301. var scrollY = 0;//this.doc.body.scrollTop;
  302. // Calculate real X, Y
  303. var dx = e.screenX - mcWindows.mouseDownScreenX;
  304. var dy = e.screenY - mcWindows.mouseDownScreenY;
  305. switch (mcWindows.action) {
  306. case "resize":
  307. width = mcWindows.mouseDownWidth + (e.screenX - mcWindows.mouseDownScreenX);
  308. height = mcWindows.mouseDownHeight + (e.screenY - mcWindows.mouseDownScreenY);
  309. width = width < 100 ? 100 : width;
  310. height = height < 100 ? 100 : height;
  311. this.wrapperIFrameElement.style.width = width+2;
  312. this.wrapperIFrameElement.style.height = height+2;
  313. this.wrapperIFrameElement.width = width+2;
  314. this.wrapperIFrameElement.height = height+2;
  315. this.winElement.style.width = width;
  316. this.winElement.style.height = height;
  317. height = height-12-19;
  318. this.containerElement.style.width = width;
  319. this.iframeElement.style.width = width;
  320. this.iframeElement.style.height = height;
  321. this.bodyElement.style.width = width;
  322. this.bodyElement.style.height = height;
  323. this.headElement.style.width = width;
  324. //this.statusElement.style.width = width;
  325. mcWindows.cancelEvent(e);
  326. break;
  327. case "move":
  328. this.left = mcWindows.mouseDownLayerX + (e.screenX - mcWindows.mouseDownScreenX);
  329. this.top = mcWindows.mouseDownLayerY + (e.screenY - mcWindows.mouseDownScreenY);
  330. this.winElement.style.left = this.left + "px";
  331. this.winElement.style.top = this.top + "px";
  332. mcWindows.cancelEvent(e);
  333. break;
  334. }
  335. };
  336. MCWindow.prototype.onMouseUp = function(e) {
  337. mcWindows.action = "none";
  338. };
  339. MCWindow.prototype.onFocus = function(e) {
  340. // Gecko only handler
  341. var winRef = e.currentTarget;
  342. for (var n in mcWindows.windows) {
  343. var win = mcWindows.windows[n];
  344. if (typeof(win) == 'function')
  345. continue;
  346. if (winRef.name == win.id) {
  347. win.focus();
  348. return;
  349. }
  350. }
  351. };
  352. MCWindow.prototype.onMouseDown = function(e) {
  353. var elm = mcWindows.isMSIE ? this.wrapperFrame.event.srcElement : e.target;
  354. var scrollX = 0;//this.doc.body.scrollLeft;
  355. var scrollY = 0;//this.doc.body.scrollTop;
  356. mcWindows.mouseDownScreenX = e.screenX;
  357. mcWindows.mouseDownScreenY = e.screenY;
  358. mcWindows.mouseDownLayerX = this.left;
  359. mcWindows.mouseDownLayerY = this.top;
  360. mcWindows.mouseDownWidth = parseInt(this.winElement.style.width);
  361. mcWindows.mouseDownHeight = parseInt(this.winElement.style.height);
  362. if (elm == this.resizeElement.firstChild)
  363. this.startResize(e);
  364. else
  365. this.startMove(e);
  366. mcWindows.cancelEvent(e);
  367. };
  368. // Global instance
  369. var mcWindows = new MCWindows();