CsdnDialog.js
上传用户:andy18
上传日期:2022-05-22
资源大小:83k
文件大小:17k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*//
  2. 标题:对话框
  3. 设计:ZswangY37
  4. 版权:CSDN.NET
  5. 版本:2008103101
  6. //*/
  7. var globalZIndex = 1001; // 对话框层次
  8. var nonepng = (/(msie)s*(d+(.d+)?)/i).exec(navigator.userAgent) && parseFloat(RegExp.$2) < 7; // 不能显示png
  9. //var imagePath = "images/"; // 图片路径
  10. var imagePath = "http://blog.csdn.net/images/logindialog/"; // 图片路径
  11. /// <summary>
  12. /// 查询IE元素
  13. /// </summary>
  14. /// <param name="id">元素id</param>
  15. /// <returns>返回id对应的网页元素</returns>
  16. /// function $(id) { return document.getElementById(id); }
  17. /// <summary>
  18. /// 添加事件
  19. /// </summary>
  20. /// <param name="target">载体</param>
  21. /// <param name="type">事件类型</param>
  22. /// <param name="func">事件函数</param>
  23. function addEventHandler(target, type, func) {
  24.     if (target.addEventListener)
  25.         target.addEventListener(type, func, false);
  26.     else if (target.attachEvent)
  27.         target.attachEvent("on" + type, func);
  28.     else target["on" + type] = func;
  29. }
  30. /// <summary>
  31. /// 获得HTML元素当前的样式
  32. /// </summary>
  33. /// <param name="element">HTML元素</param>
  34. function currentStyle(element) {
  35.     return element.currentStyle || document.defaultView.getComputedStyle(element, null);
  36. }
  37. /// <summary>
  38. /// 移除事件
  39. /// </summary>
  40. /// <param name="target">载体</param>
  41. /// <param name="type">事件类型</param>
  42. /// <param name="func">事件函数</param>
  43. function removeEventHandler(target, type, func) {
  44.     if (target.removeEventListener)
  45.         target.removeEventListener(type, func, false);
  46.     else if (target.detachEvent)
  47.         target.detachEvent("on" + type, func);
  48.     else delete target["on" + type];
  49. }
  50. /// <summary>
  51. /// 构造拖拽引擎
  52. /// </summary>
  53. /// <param name="target">拖拽元素</param>
  54. /// <param name="drag">移动元素</param>
  55. /// <parem name="bounds">拖拽范围 left: 左边界 right: 右边界 top: 上边界 bottom: 下边界 (-1 表示不限制)
  56. function DragEngine(target, drag, bounds) {
  57.     this.target = target;
  58.     this.drag = typeof drag != "undefined" ? drag : traget;
  59.     this.drag.style.position = "absolute";
  60.     this.downPoint = {}; // 鼠标按下的坐标
  61.     this.offset = {
  62.         x: parseInt(currentStyle(this.drag).marginLeft) || 0,
  63.         y: parseInt(currentStyle(this.drag).marginTop) || 0
  64.     };
  65.     this.bounds = typeof bounds != "undefined" ? bounds : { left: 0, top: 0, right: -1, bottom: -1 };
  66.     var dragEngine = this;
  67.     this.documentMousemove = function(e) {
  68.         if (window.getSelection)
  69.             getSelection().removeAllRanges();
  70.         else if (document.selection && document.selection.empty)
  71.             document.selection.empty();
  72.         var left = e.clientX - dragEngine.downPoint.x + dragEngine.offset.x;
  73.         var top = e.clientY - dragEngine.downPoint.y + dragEngine.offset.y;
  74.         if (parseInt(dragEngine.bounds.right) >= 0)
  75.             left = Math.min(left, dragEngine.bounds.right - dragEngine.drag.offsetWidth);
  76.         if (parseInt(dragEngine.bounds.bottom) >= 0)
  77.             top = Math.min(top, dragEngine.bounds.bottom - dragEngine.drag.offsetHeight);
  78.         if (parseInt(dragEngine.bounds.left) >= 0)
  79.             left = Math.max(left, dragEngine.bounds.left);
  80.         if (parseInt(dragEngine.bounds.top) >= 0)
  81.             top = Math.max(top, dragEngine.bounds.top);
  82.         dragEngine.drag.style.left = left + "px";
  83.         dragEngine.drag.style.top = top + "px";
  84.         if (typeof dragEngine.onmove == "function") dragEngine.onmove(dragEngine);
  85.     };
  86.     this.documentMouseup = function(e) {
  87.         var iframes = dragEngine.drag.getElementsByTagName("iframe");
  88.         for (var i = 0; i < iframes.length; i++) {
  89.             iframes[i].style.display = "";
  90.             //removeEventHandler(iframes[i], "mouseover", dragEngine.documentMouseup);
  91.         }
  92.         removeEventHandler(document, "mousemove", dragEngine.documentMousemove);
  93.         removeEventHandler(document, "mouseup", dragEngine.documentMouseup);
  94.         removeEventHandler(dragEngine.target, "losecapture", dragEngine.documentMouseup);
  95.         if (dragEngine.target.releaseCapture) dragEngine.target.releaseCapture();
  96.         removeEventHandler(window, "blur", dragEngine.documentMouseup);
  97.         if (typeof dragEngine.onstop == "function") dragEngine.onstop(dragEngine);
  98.     };
  99.     this.targetMousedown = function(e) {
  100.         // which: 1 == left; 2 == middle; 3 == right
  101.         if (!e.which && e.button)
  102.             e.which = e.button & 1 ? 1 : (e.button & 2 ? 3 : (e.button & 4 ? 2 : 0));
  103.         if (e.which != 1) return;
  104.         var iframes = dragEngine.drag.getElementsByTagName("iframe");
  105.         for (var i = 0; i < iframes.length; i++) {
  106.             iframes[i].style.display = "none";
  107.             //addEventHandler(iframes[i], "mouseover", dragEngine.documentMouseup);
  108.         }
  109.         dragEngine.downPoint.x = e.clientX - dragEngine.drag.offsetLeft;
  110.         dragEngine.downPoint.y = e.clientY - dragEngine.drag.offsetTop;
  111.         addEventHandler(document, "mousemove", dragEngine.documentMousemove);
  112.         addEventHandler(document, "mouseup", dragEngine.documentMouseup);
  113.         addEventHandler(dragEngine.target, "losecapture", dragEngine.documentMouseup);
  114.         if (dragEngine.target.setCapture) dragEngine.target.setCapture();
  115.         addEventHandler(window, "blur", dragEngine.documentMouseup);
  116.         if (e.preventDefault) e.preventDefault();
  117.         if (typeof dragEngine.onstart == "function") dragEngine.onstart(dragEngine);
  118.     };
  119.     addEventHandler(this.target, "mousedown", this.targetMousedown);
  120. }
  121. /// <summary>
  122. /// 释放拖拽引擎
  123. /// </summary>
  124. DragEngine.prototype.dispose = function() {
  125.     removeEventHandler(this.target, "mousedown", this.targetMousedown);
  126. }
  127. /// <summary>
  128. /// 获取文档的边界信息
  129. /// </summary>
  130. function getDocumentBounds() {
  131.     if (document.documentElement && document.compatMode == "CSS1Compat") {
  132.         return {
  133.             st: document.documentElement.scrollTop,
  134.             sl: document.documentElement.scrollLeft,
  135.             sw: document.documentElement.scrollWidth,
  136.             sh: document.documentElement.scrollHeight,
  137.             cw: document.documentElement.clientWidth,
  138.             ch: document.documentElement.clientHeight
  139.         }
  140.     } else if (document.body) {
  141.         return {
  142.             st: document.body.scrollTop,
  143.             sl: document.body.scrollLeft,
  144.             sw: document.body.scrollWidth,
  145.             sh: document.body.scrollHeight,
  146.             cw: document.body.clientWidth,
  147.             ch: document.body.clientHeight
  148.         }
  149.     }
  150. }
  151. function CsdnDialog(title, left, top, width, height, closefree, fixup) {
  152. this.closefree = closefree;
  153. this.div_dialog = document.createElement("div");
  154. this.div_dialog.style.textAlign = "left";
  155. this.div_dialog.style.display = "none";
  156. this.div_dialog.style.position = "absolute";
  157. this.div_dialog.style.borderStyle = "none";
  158. this.div_dialog.style.borderWidth = "0px";
  159. this.div_dialog.style.zIndex = globalZIndex;
  160. this.edgeWidth = 25; // 边宽
  161. document.body.appendChild(this.div_dialog);
  162. this.table_dialog = document.createElement("table");
  163. this.table_dialog.cellPadding = "0px";
  164. this.table_dialog.cellSpacing = "0px";
  165. this.div_dialog.appendChild(this.table_dialog);
  166. this.tr_top = this.table_dialog.insertRow(-1);
  167. this.tr_top.style.height = "40px";
  168. this.tr_middle = this.table_dialog.insertRow(-1);
  169. this.tr_bottom = this.table_dialog.insertRow(-1);
  170. this.tr_bottom.style.height = "25px";
  171. this.td_top_left = this.tr_top.insertCell(-1);
  172. this.td_top_left.style.backgroundImage = "url(" + imagePath + "corner.png)";
  173. this.td_top_left.style.backgroundPosition = "0px -100px";
  174. this.td_top_left.style.backgroundRepeat = "no-repeat";
  175. this.td_top_left.style.width = this.edgeWidth + "px";
  176. /*
  177. this.img_icon = document.createElement("img");
  178. this.img_icon.src = imagePath + "icon.gif";
  179. this.img_icon.style.marginTop = "10px";
  180. this.img_icon.style.marginLeft = "17px";
  181. this.td_top_left.appendChild(this.img_icon);
  182. */
  183. this.td_top_center = this.tr_top.insertCell(-1);
  184. this.td_top_center.style.backgroundImage = "url(" + imagePath + "vertical.png)";
  185. this.td_top_center.style.width = (width - this.edgeWidth * 2) + "px";
  186. this.td_top_center.style.backgroundPosition = "0px -100px";
  187. this.td_top_center.style.backgroundRepeat = "repeat-x";
  188. this.div_title = document.createElement("div");
  189. this.div_title.style.styleFloat = "left";
  190. this.div_title.style.cssFloat = "left";
  191. this.div_title.style.fontWeight = "bold";
  192. if (!fixup) {
  193. this.div_title.style.cursor = "move";
  194. this.dragEngine = new DragEngine(this.div_title, this.div_dialog);
  195. }
  196. this.div_title.style.height = "29px";
  197. this.div_title.style.verticalAlign = "middle";
  198. this.div_title.style.lineHeight = "29px";
  199. this.div_title.style.marginTop = "11px";
  200. this.div_title.dialog = this;
  201. this.td_top_center.appendChild(this.div_title);
  202. this.div_close = document.createElement("div");
  203. this.div_close.style.styleFloat = "right";
  204. this.div_close.style.cssFloat = "right";
  205. this.td_top_center.appendChild(this.div_close);
  206. this.img_close = document.createElement("img");
  207. this.img_close.src = imagePath + "blank.gif";
  208. this.img_close.style.backgroundImage = "url(" + imagePath + "closebtn.gif)";
  209. this.img_close.style.width = "44px";
  210. this.img_close.style.height = "19px";
  211. this.img_close.style.backgroundRepeat = "no-repeat";
  212. this.img_close.style.marginTop = "12px";
  213. try {
  214. this.img_close.style.cursor = "pointer";
  215. }
  216. catch (e) {
  217. this.img_close.style.cursor = "hand";
  218. }
  219. this.img_close.onmouseover = function() {
  220. this.hot = true;
  221. this.doChange();
  222. }
  223. this.img_close.onmouseout = function() {
  224. this.down = false;
  225. this.hot = false;
  226. this.doChange();
  227. }
  228. this.img_close.onmousedown = function() {
  229. this.down = true;
  230. this.doChange();
  231. }
  232. this.img_close.onmouseup = function() {
  233. this.down = false;
  234. this.doChange();
  235. }
  236. this.img_close.onclick = function() {
  237. if (!this.dialog) return;
  238. this.dialog.close();
  239. }
  240. this.img_close.dialog = this;
  241. this.img_close.hot = false;
  242. this.img_close.down = false;
  243. this.img_close.doChange = function() {
  244. if (this.disabled)
  245. this.style.backgroundPosition = "-132px 0px";
  246. else if (this.down)
  247. this.style.backgroundPosition = "-88px 0px";
  248. else if (this.hot)
  249. this.style.backgroundPosition = "-44px 0px";
  250. else this.style.backgroundPosition = "0px 0px";
  251. }
  252. this.div_close.appendChild(this.img_close)
  253. this.td_top_right = this.tr_top.insertCell(-1);
  254. this.td_top_right.style.backgroundImage = "url(" + imagePath + "corner.png)";
  255. this.td_top_right.style.backgroundRepeat = "no-repeat";
  256. this.td_top_right.style.backgroundPosition = "-75px -100px";
  257. this.td_top_right.style.width = this.edgeWidth + "px";
  258. this.td_middle_left = this.tr_middle.insertCell(-1);
  259. this.td_middle_left.style.backgroundImage = "url(" + imagePath + "horizontal.png)";
  260. this.td_middle_left.style.backgroundPosition = "-100px 0px";
  261. this.td_middle_left.style.backgroundRepeat = "repeat-y";
  262. this.td_middle_center = this.tr_middle.insertCell(-1);
  263. this.td_middle_center.style.backgroundColor = "White";
  264. this.div_html = document.createElement("div");
  265. this.td_middle_center.appendChild(this.div_html);
  266. this.td_middle_right = this.tr_middle.insertCell(-1);
  267. this.td_middle_right.style.backgroundImage = "url(" + imagePath + "horizontal.png)";
  268. this.td_middle_right.style.backgroundPosition = "-175px 0px";
  269. this.td_middle_right.style.backgroundRepeat = "repeat-y";
  270. this.td_bottom_left = this.tr_bottom.insertCell(-1);
  271. this.td_bottom_left.style.backgroundImage = "url(" + imagePath + "corner.png)";
  272. this.td_bottom_left.style.backgroundRepeat = "no-repeat";
  273. this.td_bottom_left.style.backgroundPosition = "0px -175px";
  274. this.td_bottom_center = this.tr_bottom.insertCell(-1);
  275. this.td_bottom_center.style.backgroundImage = "url(" + imagePath + "vertical.png)";
  276. this.td_bottom_center.style.backgroundPosition = "0px -175px";
  277. this.td_bottom_center.style.backgroundRepeat = "repeat-x";
  278. this.td_bottom_right = this.tr_bottom.insertCell(-1);
  279. this.td_bottom_right.style.backgroundImage = "url(" + imagePath + "corner.png)";
  280. this.td_bottom_right.style.backgroundRepeat = "no-repeat";
  281. this.td_bottom_right.style.backgroundPosition = "-75px -175px";
  282. this.resize(left, top, width, height);
  283. this.setTitle(title);
  284. }
  285. CsdnDialog.prototype.hide = function() {
  286. this.div_dialog.style.display = "none";
  287. }
  288. CsdnDialog.prototype.resize = function(left, top, width, height) {
  289. var bounds = getDocumentBounds();
  290. if (typeof top == "undefined" || top < 0) top = Math.max((bounds.ch - height) / 2 + bounds.st, 10);
  291. if (typeof left == "undefined" || left < 0) left = Math.max((bounds.cw - width) / 2 + bounds.sl, 10);
  292. this.left = left;
  293. this.top = top;
  294. this.width = width;
  295. this.height = height;
  296. this.div_dialog.style.left = this.left + "px";
  297. this.div_dialog.style.top = this.top + "px";
  298. this.div_dialog.style.width = width + "px";
  299. this.div_dialog.style.height = height + "px";
  300. this.table_dialog.style.width = width + "px";
  301. this.table_dialog.style.height = height + "px";
  302. this.tr_middle.style.height = (height - 40 - 25) + "px";
  303. this.td_top_center.style.width = (width - this.edgeWidth * 2) + "px";
  304. this.div_title.style.width = (width - this.edgeWidth * 2 - 50) + "px";
  305. this.td_middle_center.style.height = (height - 40 - 25) + "px";
  306. this.div_html.style.width = (width - this.edgeWidth * 2) + "px";
  307. this.div_html.style.height = (height - 40 - 25) + "px";
  308. if (typeof this.onresize == "function") this.onresize(this);
  309. }
  310. CsdnDialog.prototype.setTitle = function(title) {
  311. if (this.title == title) return;
  312. this.title = title;
  313. this.div_title.innerHTML = title;
  314. }
  315. CsdnDialog.prototype.show = function() {
  316. this.div_dialog.style.left = this.left + "px";
  317. this.div_dialog.style.top = this.top + "px";
  318. this.div_dialog.style.display = "";
  319. this.bodyStyleOverflow = document.body.style.overflowX;
  320. document.body.style.overflowX = "hidden";
  321. if ((/(msie)s*(d+(.d+)?)/i).exec(navigator.userAgent)) {
  322. document.createStyleSheet("javascript:'html{overflow-x:hidden;}'");
  323. document.createStyleSheet().addRule("html", "overflow-x:hidden;");
  324. document.createStyleSheet().addRule("body", "overflow-x:hidden;");
  325. }
  326. }
  327. CsdnDialog.prototype.close = function() {
  328. if (typeof this.onclose == "function") this.onclose(this);
  329. if (typeof (this.closefree) != "undefined" && this.closefree)
  330. this.dispose();
  331. else this.hide();
  332. document.body.style.overflowX = this.bodyStyleOverflow;
  333. if ((/(msie)s*(d+(.d+)?)/i).exec(navigator.userAgent)) {
  334. document.createStyleSheet("javascript:'html{overflow-x:auto;}'");
  335. document.createStyleSheet().addRule("html", "overflow-x:auto;");
  336. document.createStyleSheet().addRule("body", "overflow-x:auto;");
  337. }
  338. }
  339. CsdnDialog.prototype.dispose = function() {
  340. if (typeof this.ondispose == "function") this.ondispose(this);
  341. if (!fixup) this.dragEngine.dispose();
  342. this.td_middle_center.removeChild(this.div_html);
  343. this.div_close.removeChild(this.img_close)
  344. this.td_top_center.removeChild(this.div_close);
  345. this.td_top_center.removeChild(this.div_title);
  346. //this.td_top_left.removeChild(this.img_icon);
  347. this.div_dialog.removeChild(this.table_dialog);
  348. document.body.removeChild(this.div_dialog);
  349. }
  350. // 调用封装
  351. var currentDialog = null;
  352. function showWindow(opts) {
  353. if (!currentDialog) {
  354. if (typeof opts.width == "undefined")
  355. opts.width = 200;
  356. if (typeof opts.height == "undefined")
  357. opts.height = 200;
  358. var bounds = getDocumentBounds();
  359. currentDialog = new CsdnDialog(opts.title, opts.left, opts.top,
  360. opts.width, opts.height, false, false);
  361. currentDialog.div_black = document.createElement("div");
  362. currentDialog.div_black.style.position = "absolute";
  363. currentDialog.div_black.style.borderStyle = "none";
  364. currentDialog.div_black.style.zIndex = "1000";
  365. currentDialog.div_black.style.left = "0";
  366. currentDialog.div_black.style.top = "0";
  367. currentDialog.div_black.style.height = "3000px";
  368. currentDialog.div_black.style.width = "3000px";
  369. currentDialog.div_black.style.filter = "alpha(opacity = 40)";
  370. currentDialog.div_black.style.opacity = "0.55857";
  371. currentDialog.div_black.style.backgroundColor = "#999999";
  372. document.body.appendChild(currentDialog.div_black);
  373. currentDialog.onresize = function() {
  374. var bounds = getDocumentBounds();
  375. currentDialog.div_black.style.top = bounds.st - 1000 + "px";
  376. }
  377. currentDialog.onclose = function() {
  378. currentDialog.div_black.style.display = "none";
  379. }
  380. currentDialog.ondispose = function() {
  381. document.body.removeChild(currentDialog.div_black);
  382. currentDialog = null;
  383. }
  384. } else {
  385. currentDialog.div_black.style.display = "block";
  386. currentDialog.resize(opts.left, opts.top, opts.width, opts.height);
  387. currentDialog.setTitle(opts.title);
  388. }
  389. if (typeof opts.url == "string") {
  390. currentDialog.div_html.innerHTML = "";
  391. currentDialog.iframe = document.createElement("iframe");
  392. currentDialog.iframe.width = (opts.width - currentDialog.edgeWidth * 2) + "px";
  393. currentDialog.iframe.height = (opts.height - 40 - 25) + "px";
  394. currentDialog.iframe.style.margin = "0";
  395. currentDialog.iframe.frameBorder = "0";
  396. currentDialog.iframe.src = opts.url;
  397. currentDialog.div_html.appendChild(currentDialog.iframe);
  398. } else if (typeof opts.html == "string") {
  399. currentDialog.div_html.innerHTML = opts.html;
  400. }
  401. currentDialog.show();
  402. }
  403. function closeWindow() {
  404. if (currentDialog) currentDialog.close();
  405. }