jquery.dialog.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /*
  2. TODO: can be dragged, DOM-Drag
  3. */
  4. ;(function($) {
  5. // if already load dialog, return directly
  6. if ($.dialog) {
  7. return;
  8. }
  9. var $window = $(window), $document = $(document);
  10. var ieBug = $.browser.msie && parseFloat($.browser.version) < 7;
  11. // jQuery doesn't support a is string judgement, so I made it by myself.
  12. function isString(obj) {
  13. return typeof obj == "string" || Object.prototype.toString.call(obj) === "[object String]";
  14. }
  15. // dialog list to manage the dialogs.
  16. var dialogList = [];
  17. dialogList.add = function($dialog) {
  18. this.push($dialog);
  19. return $dialog;
  20. };
  21. dialogList.remove = function($dialog) {
  22. var flag;
  23. for (var i = 0; i < this.length; i++) {
  24. if (this[i] == $dialog) {
  25. flag = true;
  26. }
  27. if (flag) {
  28. this[i] = this[i + 1];
  29. }
  30. }
  31. if (flag) {
  32. this.length--;
  33. }
  34. return $dialog;
  35. };
  36. // manage the window resize event.
  37. var resizeTimer;
  38. $window.resize(function() {
  39. window.clearTimeout(resizeTimer);
  40. resizeTimer = window.setTimeout(function() {
  41. for (var i = 0; i < dialogList.length; i++) {
  42. dialogList[i].adjust(false);
  43. }
  44. }, 100);
  45. });
  46. // manage the window scroll event for ie6.
  47. if (ieBug) {
  48. var scrollTimer;
  49. $window.scroll(function () {
  50. for (var i = 0; i < dialogList.length; i++) {
  51. dialogList[i].ieFixedHide();
  52. }
  53. window.clearTimeout(scrollTimer);
  54. scrollTimer = window.setTimeout(function() {
  55. for (var i = 0; i < dialogList.length; i++) {
  56. dialogList[i].ieFixedPos();
  57. }
  58. }, 400);
  59. });
  60. }
  61. // handle escape key to close dialog one by one.
  62. $document.keydown(function(event) {
  63. if (dialogList.length && event.keyCode == 27) {
  64. dialogList[dialogList.length - 1].close("cancel", event);
  65. }
  66. });
  67. // the basic dialog plugin
  68. $.dialog = function(settings) {
  69. var initializing = true;
  70. var defaults = {
  71. id: "",
  72. className: "",
  73. tip: false,
  74. direction: "top",
  75. title: "",
  76. content: "",
  77. labClose: null,
  78. titleClose: "关闭",
  79. btns: [],
  80. defaultBtn: "",
  81. labAccept: "纭畾",
  82. labCancel: "鍙栨秷",
  83. top: null,
  84. left: null,
  85. refer: null,
  86. fixed: true,
  87. scrollIntoView: true,
  88. contentWidth: null,
  89. contentHeight: null,
  90. contentBtnHelp: false,
  91. modal: true,
  92. onLoad: null,
  93. onBeforeAccept: null,
  94. onAccept: null,
  95. onBeforeCancel: null,
  96. onCancel: null,
  97. onBeforeClose: null,
  98. onClose: null
  99. };
  100. // give settings to UI elements
  101. var opts = $.extend(defaults, settings);
  102. opts.top = parseInt(opts.top);
  103. opts.left = parseInt(opts.left);
  104. opts.contentWidth = parseInt(opts.contentWidth);
  105. opts.contentHeight = parseInt(opts.contentHeight);
  106. // build button html template.
  107. var mapBtns = {
  108. "accept": '<button class="dialog-button-accept">' + opts.labAccept + '</button>',
  109. "cancel": '<button class="dialog-button-cancel">' + opts.labCancel + '</button>'
  110. };
  111. var templateBtns = "";
  112. if (opts.btns.length) {
  113. templateBtns += '<div class="dialog-button-container">';
  114. for (var i = 0 ; i < opts.btns.length; i++) {
  115. templateBtns += mapBtns[opts.btns[i]];
  116. }
  117. templateBtns += '</div>';
  118. }
  119. // build mask html template.
  120. var templateMask =
  121. '<div' + (opts.id ? (' id="' + opts.id + '-mask"') : '') + ' class="jquery-dialog-mask ' +
  122. (!$("div.jquery-dialog-mask").length ? "jquery-dialog-mask-color" : "jquery-dialog-mask-transparent") +
  123. (opts.className ? (' ' + opts.className + '-mask"') : '"') + '></div>';
  124. // build dialog html template.
  125. var templateDialog =
  126. '<div style="top: -10000px; left: -10000px;"' + (opts.id ? (' id="' + opts.id + '"') : '') + ' class="jquery-dialog ' +
  127. (!opts.fixed || ieBug ? 'dialog-outer-absolute' : 'dialog-outer-fixed') +
  128. (opts.tip ? ' jquery-tip' : '') + (opts.className ? (' ' + opts.className + '"') : '"') + '>' +
  129. (opts.tip ? '<div class="dialog-tip-arrow dialog-tip-arrow-' + opts.direction.toLowerCase() + '"></div>' : '') +
  130. '<div class="dialog-top-container">' +
  131. '<div class="dialog-top-left-corner"></div>' +
  132. '<div class="dialog-top-border"></div>' +
  133. '<div class="dialog-top-right-corner"></div>' +
  134. '</div>' +
  135. '<div class="dialog-middle-container">' +
  136. '<div class="dialog-left-border"></div>' +
  137. '<div class="dialog-inner-container">' +
  138. '<div class="dialog-title-container">' +
  139. '<div class="dialog-title">' + opts.title + '</div>' +
  140. '<div tabindex="0" class="dialog-button-close" title="' + opts.titleClose + '">' +
  141. (opts.labClose || '') +
  142. '</div>' +
  143. '</div>' +
  144. '<div class="dialog-content-container"></div>' + templateBtns +
  145. '</div>' +
  146. '<div class="dialog-right-border"></div>' +
  147. '</div>' +
  148. '<div class="dialog-bottom-container">' +
  149. '<div class="dialog-bottom-left-corner"></div>' +
  150. '<div class="dialog-bottom-border"></div>' +
  151. '<div class="dialog-bottom-right-corner"></div>' +
  152. '</div>' +
  153. '</div>';
  154. // append mask and dialog into document.
  155. var $body = $(document.body), $dialogInserter = $("#jquery-dialog-inserter");
  156. if (!$dialogInserter.length) {
  157. $dialogInserter = $('<div id="jquery-dialog-inserter"></div>');
  158. $dialogInserter.prependTo($body);
  159. }
  160. var $dialog = dialogList.add($(templateDialog));
  161. $dialog.data("jquery-dialog", $dialog);
  162. if (opts.modal) {
  163. $dialog.data("jquery-dialog-mask", $(templateMask).insertBefore($dialogInserter));
  164. }
  165. // set dom content into dialog
  166. var isNode = opts.content && !isString(opts.content) && (opts.content.parentNode || opts.content.jquery);
  167. if (isNode) {
  168. var $node = $(opts.content);
  169. var data = {
  170. el: $node[0],
  171. html: $node.html(),
  172. parent: $node.parent()[0],
  173. display: $node.css("display"),
  174. position: $node.css("position")
  175. };
  176. if (data.parent) {
  177. $node.remove();
  178. }
  179. $dialog.data("dialog.history", data);
  180. }
  181. $("div.dialog-content-container", $dialog).append(isNode ? $(opts.content).eq(0) : opts.content.toString());
  182. $dialog.insertBefore($dialogInserter);
  183. if (isNode) {
  184. $(opts.content).show();
  185. }
  186. // this method can remove dialog without any callback.
  187. $dialog.destroy = function() {
  188. // remove mask from dom
  189. dialogList.remove(this);
  190. if (opts.modal) {
  191. this.data("jquery-dialog-mask").removeShadow().remove();
  192. }
  193. // restore content node.
  194. var data = this.data("dialog.history");
  195. if (data && data.el) {
  196. var $node = $(data.el).css({"display": data.display, "position": data.position});
  197. if (data.parent) {
  198. $node.html(data.html).appendTo(data.parent);
  199. }
  200. this.removeData("dialog.history");
  201. }
  202. // remove dialog from dom.
  203. this.remove();
  204. };
  205. // add dialog close method.
  206. $dialog.close = function(state, event) {
  207. event = $.extend(event, {"state": state});
  208. if ($.isFunction(opts.onBeforeClose) && opts.onBeforeClose.call($dialog, event) === false) {
  209. return false;
  210. }
  211. // call destroy method
  212. this.destroy();
  213. if ($.isFunction(opts.onClose)) {
  214. opts.onClose(event);
  215. }
  216. if (state == "accept") {
  217. if ($.isFunction(opts.onAccept)) {
  218. opts.onAccept(event);
  219. }
  220. }
  221. else if (state == "cancel") {
  222. if ($.isFunction(opts.onCancel)) {
  223. opts.onCancel(event);
  224. }
  225. }
  226. return true;
  227. };
  228. // add adjust dialog size method.
  229. $dialog.adjust = function() {
  230. // adjust mask size
  231. var $mask = this.data("jquery-dialog-mask");
  232. if ($mask) {
  233. if (ieBug) {
  234. $mask.css("position", "absolute")
  235. .height(Math.max($body.boxHeight(), $window.height()))
  236. .width(Math.max($body.boxWidth(), $window.width()))
  237. .iShadow({position: "absolute", referPoint: "topleft"});
  238. }
  239. else {
  240. $mask
  241. .iShadow({position: "fixed", referPoint: "topleft"});
  242. }
  243. }
  244. if ((typeof arguments[0] == "undefined") || initializing) {
  245. var $contentContainer = $("div.dialog-content-container", this);
  246. if (!initializing) {
  247. $contentContainer.css({height: "auto"});
  248. }
  249. var $leftBorder = $("div.dialog-left-border", this);
  250. var $rightBorder = $("div.dialog-right-border", this);
  251. var $topBorder = $("div.dialog-top-border", this);
  252. var $bottomBorder = $("div.dialog-bottom-border", this);
  253. var $topLeftCorner = $("div.dialog-top-left-corner", this);
  254. var $topRightCorner = $("div.dialog-top-right-corner", this);
  255. var $bottomLeftCorner = $("div.dialog-bottom-left-corner", this);
  256. var $bottomRightCorner = $("div.dialog-bottom-right-corner", this);
  257. var $topContainer = $("div.dialog-top-container", this);
  258. var $midderContainer = $("div.dialog-middle-container", this);
  259. var $bottomContainer = $("div.dialog-bottom-container", this);
  260. var $innerContainer = $("div.dialog-inner-container", this);
  261. var $titleContainer = $("div.dialog-title-container", this);
  262. var $title = $("div.dialog-title", this);
  263. var $buttonClose = $("div.dialog-button-close", this);
  264. var $buttonContainer = $("div.dialog-button-container", this);
  265. // give the content a width or height define.
  266. var contentWidth = Math.max(
  267. (opts.contentWidth > 0 ? opts.contentWidth : Math.min($contentContainer.boxWidth(), $window.width() - $.scrollbarWidth())),
  268. opts.btns.length * 150
  269. );
  270. $contentContainer.boxWidth(contentWidth);
  271. var contentHeight = opts.contentHeight > 0 ? opts.contentHeight : $contentContainer.boxHeight();
  272. $contentContainer.boxHeight(contentHeight);
  273. // translate buttons inside content to iButton default style.
  274. if (opts.contentBtnHelp && $.fn.iButton) {
  275. $("input[type='button'], button", $contentContainer).iButton();
  276. }
  277. // set the title-container and button-container are sync with content width
  278. var contentBoxWidth = $contentContainer.boxWidth();
  279. $titleContainer.boxWidth(contentBoxWidth);
  280. $buttonContainer.boxWidth(contentBoxWidth);
  281. // adjust title and button layout.
  282. $title.boxWidth($titleContainer.width() - $buttonClose.boxWidth());
  283. if (initializing && $.fn.iButton) {
  284. $("input[type='button'], button", $buttonContainer).iButton();
  285. }
  286. // set the top-border and bottom-border width.
  287. var innerContainerBoxWidth = contentBoxWidth + $innerContainer.box("lr");
  288. $innerContainer.boxWidth(innerContainerBoxWidth);
  289. $topBorder.boxWidth(innerContainerBoxWidth);
  290. $bottomBorder.boxWidth(innerContainerBoxWidth);
  291. // set the left-border and right-border height.
  292. var innerContainerBoxHeight = $innerContainer.box("tb") + $contentContainer.boxHeight() +
  293. $titleContainer.boxHeight() + $buttonContainer.boxHeight();
  294. $leftBorder.boxHeight(innerContainerBoxHeight);
  295. $rightBorder.boxHeight(innerContainerBoxHeight);
  296. // give the top-c, middle-c and bottom-c a fixed width and height.
  297. $topContainer.width($topLeftCorner.boxWidth() + $topBorder.boxWidth() + $topRightCorner.boxWidth());
  298. $bottomContainer.width($bottomLeftCorner.boxWidth() + $bottomBorder.boxWidth() + $bottomRightCorner.boxWidth());
  299. $midderContainer
  300. .height(innerContainerBoxHeight)
  301. .width($leftBorder.boxWidth() + $innerContainer.boxWidth() + $rightBorder.boxWidth());
  302. // give the dialog a fixed width
  303. this.width($topContainer.boxWidth());
  304. }
  305. // calculate the center top and center left position
  306. if (initializing || opts.fixed) {
  307. var centerTop = Math.round(($window.height() - this.boxHeight()) / 2),
  308. centerLeft = Math.round(($window.width() - this.boxWidth()) / 2);
  309. }
  310. // calculate the dialog top and left position.
  311. var top = null, left = null;
  312. // ie6 fixed position.
  313. if (ieBug && opts.fixed) {
  314. top = $window.scrollTop() + (isNaN(opts.top) ? centerTop : opts.top );
  315. left = $window.scrollLeft() + (isNaN(opts.left) ? centerLeft : opts.left);
  316. }
  317. // for unfixed refer position.
  318. else if (!opts.fixed && opts.refer) {
  319. var offset = $(opts.refer).offset();
  320. top = offset.top + (opts.top || 0);
  321. left = offset.left + (opts.left || 0);
  322. }
  323. // for initialized unfixed dialog and always for fixed dialog
  324. else if (initializing || opts.fixed) {
  325. if (!opts.fixed) {
  326. centerTop = $window.scrollTop() + centerTop;
  327. centerLeft = $window.scrollLeft() + centerLeft;
  328. }
  329. top = isNaN(opts.top) ? centerTop : opts.top;
  330. left = isNaN(opts.left) ? centerLeft : opts.left;
  331. }
  332. if (top !== null) {
  333. this.css({"top": top + "px", "left": left + "px"});
  334. }
  335. // scroll into view control.
  336. if (initializing && !opts.fixed && opts.scrollIntoView) {
  337. this.scrollIntoView();
  338. }
  339. // give default button focus
  340. if (initializing) {
  341. if (opts.defaultBtn == "accept") {
  342. $(".dialog-button-accept", $buttonContainer).eq(0).focus();
  343. }
  344. else if (opts.defaultBtn == "cancel") {
  345. $(".dialog-button-cancel", $buttonContainer).eq(0).focus();
  346. }
  347. }
  348. return this;
  349. };
  350. // scroll dialog into view method.
  351. $dialog.scrollIntoView = function() {
  352. var pos = this.position(), scrollTop = $window.scrollTop(), scrollLeft = $window.scrollLeft();
  353. if (((pos.top < scrollTop) || (pos.top > $window.height() + scrollTop)) ||
  354. ((pos.left < scrollLeft) || (pos.left > $window.width() + scrollLeft))) {
  355. this[0].scrollIntoView();
  356. }
  357. };
  358. // ie fixed top method.
  359. $dialog.ieFixedPos = function() {
  360. if (ieBug && opts.fixed) {
  361. var top = isNaN(opts.top) ? Math.round(($window.height() - this.boxHeight()) / 2) : opts.top;
  362. var left = isNaN(opts.left) ? Math.round(($window.width() - this.boxWidth()) / 2) : opts.left;
  363. this.css({"top": top + $window.scrollTop() + "px",
  364. "left": left + $window.scrollLeft() + "px"});
  365. }
  366. return this;
  367. };
  368. // ie fixed hide method
  369. $dialog.ieFixedHide = function() {
  370. if (ieBug && opts.fixed) {
  371. this.css({"top": "-10000px", "left": "-10000px"});
  372. }
  373. return this;
  374. }
  375. /*
  376. init position and size for dialog.
  377. */
  378. $dialog.adjust(false);
  379. /*
  380. add event handlers for dialog.
  381. */
  382. $dialog
  383. .mouseover(function(event) {
  384. var $target = $(event.target);
  385. var fromElement = event.fromElement || event.relatedTarget;
  386. if ($target.is("div.dialog-button-close")) {
  387. $target.addClass("dialog-button-close-hover");
  388. }
  389. })
  390. .mouseout(function(event) {
  391. var $target = $(event.target);
  392. var toElement = event.toElement || event.relatedTarget;
  393. if ($target.is("div.dialog-button-close")) {
  394. $target.removeClass("dialog-button-close-hover");
  395. }
  396. })
  397. .click(function(event) {
  398. var $target = $(event.target);
  399. if ($target.is("div.dialog-button-close"))
  400. {
  401. $dialog.close("cancel");
  402. }else
  403. {
  404. var $btnAccept = $target.closest(".dialog-button-accept");
  405. var $btnCancel = $target.closest(".dialog-button-cancel");
  406. if ($btnAccept.length && $btnAccept.attr("data-disabled") != "true") {
  407. if ($.isFunction(opts.onBeforeAccept) &&
  408. opts.onBeforeAccept.call($dialog, $.extend(event, {"state": "accept"})) === false) {
  409. return;
  410. }
  411. $dialog.trigger("accept");
  412. }
  413. else if ($btnCancel.length && $btnCancel.attr("data-disabled") != "true") {
  414. if ($.isFunction(opts.onBeforeCancel) &&
  415. opts.onBeforeCancel.call($dialog, $.extend(event, {"state": "cancel"})) === false) {
  416. return;
  417. }
  418. $dialog.trigger("cancel");
  419. }
  420. else if ($target.is("div.dialog-button-close")) {
  421. $dialog.close("cancel");
  422. }
  423. }
  424. })
  425. .bind("accept", function() {
  426. $dialog.close("accept");
  427. })
  428. .bind("cancel", function() {
  429. $dialog.close("cancel");
  430. });
  431. if ($.isFunction(opts.onLoad)) {
  432. opts.onLoad.call($dialog);
  433. }
  434. initializing = false;
  435. return $dialog;
  436. };
  437. /* the jquery inform dialog */
  438. $.inform = function(settings) {
  439. var defaults = {
  440. icon: "",
  441. title: "",
  442. content: "",
  443. delay: 2000,
  444. easyClose: true,
  445. onClose: null
  446. };
  447. // give settings to UI elements
  448. var opts = $.extend(defaults, settings);
  449. // for icon class define.
  450. var content = $.isPlainObject(settings) ? opts.content : settings;
  451. if (opts.icon) {
  452. content = '<div class="' + opts.icon + '"></div><div class="dialog-content">' + content + '</div>';
  453. }
  454. var $inform = $.dialog({
  455. className: "jquery-inform",
  456. title: opts.title,
  457. content: content,
  458. onClose: opts.onClose
  459. });
  460. // bind close handler.
  461. var timer;
  462. if (opts.delay > 0) {
  463. timer = window.setTimeout(close, opts.delay);
  464. }
  465. if (opts.easyClose) {
  466. $document.bind("mouseup", close);
  467. }
  468. // close by timeout or click event.
  469. function close() {
  470. try{ $inform.close(); }catch(e){};
  471. window.clearTimeout(timer);
  472. if (opts.easyClose) {
  473. $document.unbind("mouseup", close);
  474. }
  475. };
  476. return $inform;
  477. };
  478. /* the jquery confirm dialog */
  479. $.alert = function(settings) {
  480. var defaults = {
  481. icon: "",
  482. title: "",
  483. content: "",
  484. labClose: "纭畾",
  485. onClose: null
  486. };
  487. // give settings to UI elements
  488. var opts = $.extend(defaults, settings);
  489. // for icon class define.
  490. var content = $.isPlainObject(settings) ? opts.content : settings;
  491. if (opts.icon) {
  492. content = '<div class="' + opts.icon + '"></div><div class="dialog-content">' + content + '</div>';
  493. }
  494. return $.dialog({
  495. className: "jquery-alert",
  496. btns: ["accept"],
  497. defaultBtn: "accept",
  498. labAccept: opts.labClose,
  499. title: opts.title,
  500. content: content,
  501. onClose: opts.onClose
  502. });
  503. };
  504. /* the jquery confirm dialog */
  505. $.confirm = function(settings) {
  506. var defaults = {
  507. icon: "",
  508. title: "",
  509. content: "",
  510. labConfirm: "纭畾",
  511. labCancel: "鍙栨秷",
  512. defaultBtn: "accept",
  513. onConfirm: null,
  514. onCancel: null
  515. };
  516. // give settings to UI elements
  517. var opts = $.extend(defaults, settings);
  518. // for icon class define.
  519. var content = $.isPlainObject(settings) ? opts.content : settings;
  520. if (opts.icon) {
  521. content = '<div class="' + opts.icon + '"></div><div class="dialog-content">' + content + '</div>';
  522. }
  523. return $.dialog({
  524. className: "jquery-confirm",
  525. btns: ["accept", "cancel"],
  526. defaultBtn: opts.defaultBtn,
  527. labAccept: opts.labConfirm,
  528. labCancel: opts.labCancel,
  529. title: opts.title,
  530. content: content,
  531. onAccept: opts.onConfirm,
  532. onCancel: opts.onCancel
  533. });
  534. };
  535. /*
  536. * jQuery tip plugins
  537. */
  538. // wrap the browser default getClientRects method to cross browser function.
  539. function getClientRects(target) {
  540. // create new wrapped rect objects.
  541. var rects = target.getClientRects();
  542. var newRects = [{"top": rects[0].top, "right": rects[0].right,
  543. "bottom": rects[0].bottom, "left": rects[0].left}];
  544. for (var i = 1, j = 0; i < rects.length; i++) {
  545. if (rects[i]) {
  546. // unit rects for IE6/IE7
  547. if ((newRects[j].right == rects[i].left &&
  548. newRects[j].bottom - newRects[j].top > Math.abs(newRects[j].top - rects[i].top)) ||
  549. (newRects[j].left == newRects[j].right)) {
  550. newRects[j].top = Math.min(newRects[j].top, rects[i].top);
  551. newRects[j].right = Math.max(newRects[j].right, rects[i].right);
  552. newRects[j].bottom = Math.max(newRects[j].bottom, rects[i].bottom);
  553. newRects[j].left = Math.min(newRects[j].left, rects[i].left);
  554. }
  555. else {
  556. newRects.push({"top": rects[i].top, "right": rects[i].right,
  557. "bottom": rects[i].bottom, "left": rects[i].left});
  558. j++;
  559. }
  560. }
  561. }
  562. // convert float position value to integer value for Firefox
  563. for (var i = 0; i < newRects.length; i++) {
  564. newRects[i].top = Math.round(newRects[i].top);
  565. newRects[i].right = Math.round(newRects[i].right);
  566. newRects[i].bottom = Math.round(newRects[i].bottom);
  567. newRects[i].left = Math.round(newRects[i].left);
  568. }
  569. return newRects;
  570. }
  571. /*
  572. * Extend a custom hover rect event plugin.
  573. * Because jQuery hover event doesn't work well for jQuery tip.
  574. */
  575. $.fn.hoverrect = function(mouseenter, mouseleave) {
  576. var pos = {x: 0, y: 0}, lastRect = {top: null, right: null, bottom: null, left: null};
  577. return this
  578. .mousemove(function(event) {
  579. if (pos.x == event.clientX && pos.y == event.clientY) {
  580. return;
  581. }
  582. pos.x = event.clientX;
  583. pos.y = event.clientY;
  584. // check rect by current mouse position.
  585. var rects = getClientRects(this), rect;
  586. for (var i = 0; i < rects.length; i++) {
  587. if (rects[i].left <= pos.x && rects[i].right >= pos.x &&
  588. rects[i].top <= pos.y && rects[i].bottom >= pos.y) {
  589. rect = rects[i];
  590. break;
  591. }
  592. }
  593. // rect which unders current mouse position has changed.
  594. if (rect && (lastRect.top !== rect.top || lastRect.right !== rect.right ||
  595. lastRect.bottom !== rect.bottom || lastRect.left !== rect.left)) {
  596. // mouse leave the last rect.
  597. if (lastRect.top !== null) {
  598. event.clientRect = lastRect;
  599. event.type = "mouseleaverect";
  600. if ($.isFunction(mouseleave)) {
  601. mouseleave.call(this, event);
  602. }
  603. }
  604. // mouse enter a new rect.
  605. lastRect = rect;
  606. event.clientRect = rect;
  607. event.type = "mouseenterrect";
  608. if ($.isFunction(mouseenter)) {
  609. mouseenter.call(this, event);
  610. }
  611. }
  612. })
  613. .mouseleave(function(event) {
  614. // mouse leave the last rect.
  615. event.clientRect = lastRect;
  616. event.type = "mouseleaverect";
  617. lastRect = {top: null, right: null, bottom: null, left: null};
  618. if ($.isFunction(mouseleave)) {
  619. mouseleave.call(this, event);
  620. }
  621. });
  622. };
  623. // build the tip arrow class string
  624. function buildArrowClass(direction) {
  625. return "dialog-tip-arrow dialog-tip-arrow-" + direction;
  626. }
  627. // calculate the tip location
  628. function calcTipPos(align, referRect, tipRect, offset) {
  629. var top = 0, left = 0;
  630. if (align == "top") {
  631. top = Math.round(referRect.top - tipRect.height - offset.y);
  632. left = Math.round(referRect.left + (referRect.width - tipRect.width) / 2 + offset.x);
  633. }
  634. else if (align == "bottom") {
  635. top = Math.round(referRect.top + referRect.height + offset.y);
  636. left = Math.round(referRect.left + (referRect.width - tipRect.width) / 2 + offset.x);
  637. }
  638. else if (align == "left") {
  639. top = Math.round(referRect.top + (referRect.height - tipRect.height) / 2 + offset.y);
  640. left = Math.round(referRect.left - tipRect.width - offset.x);
  641. }
  642. else if (align == "right") {
  643. top = Math.round(referRect.top + (referRect.height - tipRect.height) / 2 + offset.y);
  644. left = Math.round(referRect.left + referRect.width + offset.x);
  645. }
  646. return {top: top, left: left};
  647. }
  648. // alignTipToRect shared with $.tip() and $.fn.tip().
  649. function alignTipToRect($tip, rect, opts) {
  650. // get below help data info.
  651. var docElem = document.documentElement, body = document.body, $body = $(body);
  652. var winHeight = $window.height();
  653. var winWidth = $window.width();
  654. var scrollTop = window.pageYOffset || docElem.scrollTop;
  655. var scrollLeft = window.pageXOffset || docElem.scrollLeft;
  656. var clientTop = docElem.clientTop || body.clientTop || 0;
  657. var clientLeft = docElem.clientLeft || body.clientLeft || 0;
  658. // fill the referRect object.
  659. var referRect = {
  660. top: Math.round(rect.top + scrollTop - clientTop),
  661. left: Math.round(rect.left + scrollLeft - clientLeft),
  662. width: Math.round(rect.right - rect.left),
  663. height: Math.round(rect.bottom - rect.top)
  664. };
  665. // fill the tipRect object.
  666. var tipRect = {
  667. width: Math.round($tip.width()),
  668. height: Math.round($tip.height())
  669. };
  670. // fill the offset object.
  671. var offset = {x: opts.offsetX, y: opts.offsetY};
  672. // calculate the tip position by rectRect, tipRect and offset.
  673. var position = calcTipPos(opts.align, referRect, tipRect, offset);
  674. // get the scope top, left, bottom, right
  675. var scopeTop = (opts.scope === "viewport" ? scrollTop : 0) + 1;
  676. var scopeLeft = (opts.scope === "viewport" ? scrollLeft : 0) + 1;
  677. var scopeBottom = (opts.scope === "viewport" ? winHeight + scrollTop : Math.max($body.boxHeight(), winHeight)) - 1;
  678. var scopeRight = (opts.scope === "viewport" ? winWidth + scrollLeft : Math.max($body.boxWidth(), winWidth)) - 1;
  679. // reset arrow class by align option.
  680. var $arrow = $tip.find(".dialog-tip-arrow"), arrow = $arrow[0];
  681. arrow.className = buildArrowClass(alignMap[opts.align]);
  682. // the tip outside scope top, align to bottom
  683. if (opts.align == "top" && position.top < scopeTop) {
  684. position = calcTipPos("bottom", referRect, tipRect, offset);
  685. arrow.className = buildArrowClass("top");
  686. }
  687. // the tip outside scope bottom, align to top
  688. else if (opts.align == "bottom" && (position.top + tipRect.height) > scopeBottom) {
  689. position = calcTipPos("top", referRect, tipRect, offset);
  690. arrow.className = buildArrowClass("bottom");
  691. }
  692. // the tip outside scope left, align to right
  693. else if (opts.align == "left" && position.left < scopeLeft) {
  694. position = calcTipPos("right", referRect, tipRect, offset);
  695. arrow.className = buildArrowClass("left");
  696. }
  697. // the tip outside scope right, align to left
  698. else if (opts.align == "right" && (position.left + tipRect.width) > scopeRight) {
  699. position = calcTipPos("left", referRect, tipRect, offset);
  700. arrow.className = buildArrowClass("right");
  701. }
  702. // limit the tip and tip-arrow position.
  703. var minTop = scrollTop + 1,
  704. minLeft = scrollLeft + 1,
  705. maxTop = winHeight + scrollTop - tipRect.height - 1,
  706. maxLeft = winWidth + scrollLeft - tipRect.width - 1,
  707. tipTop = position.top,
  708. tipLeft = position.left;
  709. // set the tip arrow position.
  710. if (opts.align == "top" || opts.align == "bottom") {
  711. tipLeft = Math.max(Math.min(tipLeft, maxLeft), minLeft);
  712. $arrow.css({"left": Math.round((tipRect.width - $arrow.width()) / 2) - (tipLeft - position.left) + "px"});
  713. }
  714. else if (opts.align == "left" || opts.align == "right") {
  715. tipTop = Math.max(Math.min(tipTop, maxTop), minTop);
  716. $arrow.css({"top": Math.round((tipRect.height - $arrow.height()) / 2) - (tipTop - position.top) + "px"});
  717. }
  718. // set the tip dialog position.
  719. $tip.css({"top": tipTop + "px", "left": tipLeft + "px"});
  720. }
  721. // setTipContent shared with $.tip() and $.fn.tip().
  722. function setTipContent($tip, content) {
  723. var top = $tip.css("top");
  724. var left = $tip.css("left");
  725. $tip.css({"top": "-10000px", "left": "-10000px"}).show();
  726. $tip.find(".dialog-content-container").html(content).css("width", "auto");
  727. $tip.find(".dialog-inner-container").css("width", "auto");
  728. $tip.find(".dialog-middle-container").css("width", "auto");
  729. $tip.css("width", "auto");
  730. $tip.adjust();
  731. $tip.css({"top": top, "left": left});
  732. }
  733. // $.tip(): jQuery tip plugin.
  734. var alignMap = {"top": "bottom", "right": "left", "bottom": "top", "left": "right"};
  735. $.tip = function(settings) {
  736. var defaults = {
  737. id: "",
  738. className: "",
  739. title: "",
  740. content: "",
  741. labClose: null,
  742. titleClose: "鍏抽棴",
  743. btns: [],
  744. defaultBtn: "",
  745. labAccept: "纭畾",
  746. labCancel: "鍙栨秷",
  747. contentWidth: null,
  748. contentHeight: null,
  749. contentBtnHelp: false,
  750. modal: false,
  751. onLoad: null,
  752. onBeforeAccept: null,
  753. onAccept: null,
  754. onBeforeCancel: null,
  755. onCancel: null,
  756. onBeforeClose: null,
  757. onClose: null
  758. };
  759. var opts = $.extend(defaults, settings);
  760. // create the tip widget.
  761. var $tip = $.dialog({
  762. tip: true,
  763. fixed: false,
  764. scrollIntoView: false,
  765. top: -10000,
  766. left: -10000,
  767. id: opts.id,
  768. className: opts.className,
  769. title: opts.title,
  770. content: opts.content,
  771. labClose: opts.labClose,
  772. titleClose: opts.titleClose,
  773. btns: opts.btns,
  774. defaultBtn: opts.defaultBtn,
  775. labAccept: opts.labAccept,
  776. labCancel: opts.labCancel,
  777. contentWidth: opts.contentWidth,
  778. contentHeight: opts.contentHeight,
  779. contentBtnHelp: opts.contentBtnHelp,
  780. modal: opts.modal,
  781. onLoad: opts.onLoad,
  782. onBeforeAccept: opts.onBeforeAccept,
  783. onAccept: opts.onAccept,
  784. onBeforeCancel: opts.onBeforeCancel,
  785. onCancel: opts.onCancel,
  786. onBeforeClose: opts.onBeforeClose,
  787. onClose: opts.onClose
  788. })
  789. .hide();
  790. /*
  791. * tip.align(): adjust tip position.
  792. */
  793. $tip.align = function(settings) {
  794. if (!this.is(":visible")) {
  795. return this;
  796. }
  797. // fill the options object.
  798. var opts;
  799. if (typeof settings === "undefined") {
  800. opts = this.data("tip-settings");
  801. }
  802. else {
  803. var defaults = {
  804. scope: "viewport",
  805. align: "bottom",
  806. refer: null,
  807. referRect: null,
  808. offsetX: 0,
  809. offsetY: 0
  810. };
  811. // align setting.
  812. opts = $.extend(defaults, settings);
  813. // tip align map object.
  814. opts.align = opts.align.toLowerCase();
  815. if (!alignMap[opts.align]) {
  816. opts.align = defaults.align;
  817. }
  818. // save current setting to tip's data.
  819. this.data("tip-settings", opts);
  820. }
  821. // align the tip.
  822. alignTipToRect(this, opts.referRect ? opts.referRect : getClientRects($(opts.refer)[0])[0], opts);
  823. return this;
  824. };
  825. /*
  826. * tip.content(): get or set content of tip widget.
  827. */
  828. $tip.content = function(content) {
  829. if (typeof content == "undefined") {
  830. return this.find(".dialog-content-container").html();
  831. }
  832. else {
  833. var isVisible = this.is(":visible");
  834. setTipContent(this, content);
  835. // align tip position or restore hide states.
  836. if (!isVisible) {
  837. this.hide();
  838. }
  839. return this;
  840. }
  841. };
  842. return $tip;
  843. };
  844. /*
  845. * $(selector).tip: jQuery tip plugin.
  846. */
  847. $.fn.tip = function(settings) {
  848. var defaults = {
  849. hover: true,
  850. enterable: false,
  851. enterableDelay: 100,
  852. show: null,
  853. hide: null,
  854. scope: "viewport",
  855. align: "bottom",
  856. offsetX: 0,
  857. offsetY: 0,
  858. id: "",
  859. className: "",
  860. content: "",
  861. contentWidth: null,
  862. contentHeight: null
  863. };
  864. // give settings to UI elements
  865. var opts = $.extend(defaults, settings);
  866. // tip align map object.
  867. opts.align = opts.align.toLowerCase();
  868. if (!alignMap[opts.align]) {
  869. opts.align = defaults.align;
  870. }
  871. // handler code.
  872. return this.each(function() {
  873. // initialize the tip widget.
  874. var $refer = $(this),
  875. $tip = $refer.data("tip");
  876. if ($tip) {
  877. return;
  878. }
  879. // create the tip widget and save it to its data "tip".
  880. $tip = $.dialog({
  881. tip: true,
  882. modal: false,
  883. fixed: false,
  884. top: -10000,
  885. left: -10000,
  886. id: opts.id,
  887. className: opts.className,
  888. content: opts.content,
  889. contentWidth: opts.contentWidth,
  890. contentHeight: opts.contentHeight
  891. })
  892. .hide();
  893. $refer.data("tip", $tip);
  894. // auto hover show up.
  895. if (opts.hover) {
  896. // tip hide and show event handler.
  897. function hideTip(event) {
  898. if ($.isFunction(opts.hide)) {
  899. opts.hide.call($tip, event);
  900. }
  901. else {
  902. $tip.hide();
  903. }
  904. }
  905. function showTip(event) {
  906. $tip.data("tip-refer-rect", event.clientRect);
  907. if ($.isFunction(opts.show)) {
  908. opts.show.call($tip, event);
  909. }
  910. else {
  911. $tip.show();
  912. }
  913. $tip.data("tip-refer-rect", null);
  914. }
  915. // timer for tip enterable feature.
  916. var timer = null;
  917. function cancelledHideTip() {
  918. if (timer) {
  919. window.clearTimeout(timer);
  920. timer = null;
  921. return true;
  922. }
  923. else {
  924. return false;
  925. }
  926. }
  927. function delayedHideTip(event) {
  928. timer = window.setTimeout(function() {
  929. timer = null;
  930. hideTip(event);
  931. }, opts.enterableDelay);
  932. }
  933. // hover to show the tip.
  934. if (opts.enterable) {
  935. $tip.hover(cancelledHideTip, delayedHideTip);
  936. }
  937. var lastRect;
  938. $refer.hoverrect(
  939. function(event) {
  940. var rect = event.clientRect;
  941. // Igonre hidden only for the same rect.
  942. if (!cancelledHideTip() && lastRect &&
  943. lastRect.top == rect.top && lastRect.right == rect.right &&
  944. lastRect.bottom == rect.bottom && lastRect.left == rect.left) {
  945. showTip(event);
  946. }
  947. else {
  948. cancelledHideTip();
  949. showTip(event);
  950. }
  951. },
  952. function(event) {
  953. lastRect = event.clientRect;
  954. if (opts.enterable) {
  955. delayedHideTip(event);
  956. }
  957. else {
  958. hideTip(event);
  959. }
  960. }
  961. );
  962. }
  963. /*
  964. * fn.tip.algin(): adjust tip position.
  965. */
  966. $tip.align = function () {
  967. if (!this.is(":visible")) {
  968. return this;
  969. }
  970. var rect = this.data("tip-refer-rect");
  971. if (!rect) {
  972. rect = getClientRects($refer[0])[0];
  973. }
  974. // align the tip.
  975. alignTipToRect(this, rect, opts);
  976. return this;
  977. };
  978. /*
  979. * fn.tip.show(): inject code to the default jQuery show method.
  980. */
  981. var funShow = $tip.show;
  982. $tip.show = function(speed) {
  983. funShow.call(this);
  984. this.align();
  985. // for speed animation effect.
  986. if (speed || speed === 0) {
  987. this.hide();
  988. funShow.apply(this, arguments);
  989. }
  990. return this;
  991. };
  992. /*
  993. * fn.tip.fadeIn(): inject code to the default jQuery fadeIn method.
  994. */
  995. var funFadeIn = $tip.fadeIn;
  996. $tip.fadeIn = function() {
  997. funShow.call(this);
  998. this.align().hide();
  999. funFadeIn.apply(this, arguments);
  1000. return this;
  1001. };
  1002. /*
  1003. * fn.tip.content(): get or set content of tip widget.
  1004. */
  1005. $tip.content = function(content) {
  1006. if (typeof content == "undefined") {
  1007. return this.find(".dialog-content-container").html();
  1008. }
  1009. else {
  1010. var isVisible = this.is(":visible");
  1011. setTipContent(this, content);
  1012. // align tip position or restore hide states.
  1013. if (isVisible) {
  1014. this.align();
  1015. }
  1016. else {
  1017. this.hide();
  1018. }
  1019. return this;
  1020. }
  1021. };
  1022. });
  1023. };
  1024. })(jQuery);