国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

571 lines
20KB

  1. /**
  2. * Boxy 0.1.4 - Facebook-style dialog, with frills
  3. *
  4. * (c) 2008 Jason Frame
  5. * Licensed under the MIT License (LICENSE)
  6. */
  7. /*
  8. * jQuery plugin
  9. *
  10. * Options:
  11. * message: confirmation message for form submit hook (default: "Please confirm:")
  12. *
  13. * Any other options - e.g. 'clone' - will be passed onto the boxy constructor (or
  14. * Boxy.load for AJAX operations)
  15. */
  16. jQuery.fn.boxy = function(options) {
  17. options = options || {};
  18. return this.each(function() {
  19. var node = this.nodeName.toLowerCase(), self = this;
  20. if (node == 'a') {
  21. jQuery(this).click(function() {
  22. var active = Boxy.linkedTo(this),
  23. href = this.getAttribute('href'),
  24. localOptions = jQuery.extend({actuator: this, title: this.title}, options);
  25. if (active) {
  26. active.show();
  27. } else if (href.indexOf('#') >= 0) {
  28. var content = jQuery(href.substr(href.indexOf('#'))),
  29. newContent = content.clone(true);
  30. content.remove();
  31. localOptions.unloadOnHide = false;
  32. new Boxy(newContent, localOptions);
  33. } else { // fall back to AJAX; could do with a same-origin check
  34. if (!localOptions.cache) localOptions.unloadOnHide = true;
  35. Boxy.load(this.href, localOptions);
  36. }
  37. return false;
  38. });
  39. } else if (node == 'form') {
  40. jQuery(this).bind('submit.boxy', function() {
  41. Boxy.confirm(options.message || 'Please confirm:', function() {
  42. jQuery(self).unbind('submit.boxy').submit();
  43. });
  44. return false;
  45. });
  46. }
  47. });
  48. };
  49. //
  50. // Boxy Class
  51. function Boxy(element, options) {
  52. this.boxy = jQuery(Boxy.WRAPPER);
  53. jQuery.data(this.boxy[0], 'boxy', this);
  54. this.visible = false;
  55. this.options = jQuery.extend({}, Boxy.DEFAULTS, options || {});
  56. if (this.options.modal) {
  57. this.options = jQuery.extend(this.options, {center: true, draggable: false});
  58. }
  59. // options.actuator == DOM element that opened this boxy
  60. // association will be automatically deleted when this boxy is remove()d
  61. if (this.options.actuator) {
  62. jQuery.data(this.options.actuator, 'active.boxy', this);
  63. }
  64. this.setContent(element || "<div></div>");
  65. this._setupTitleBar();
  66. this.boxy.css('display', 'none').appendTo(document.body);
  67. this.toTop();
  68. if (this.options.fixed) {
  69. if (jQuery.browser.msie && jQuery.browser.version < 7) {
  70. this.options.fixed = false; // IE6 doesn't support fixed positioning
  71. } else {
  72. this.boxy.addClass('fixed');
  73. }
  74. }
  75. if (this.options.center && Boxy._u(this.options.x, this.options.y)) {
  76. this.center();
  77. } else {
  78. this.moveTo(
  79. Boxy._u(this.options.x) ? this.options.x : Boxy.DEFAULT_X,
  80. Boxy._u(this.options.y) ? this.options.y : Boxy.DEFAULT_Y
  81. );
  82. }
  83. if (this.options.show) this.show();
  84. };
  85. Boxy.EF = function() {};
  86. jQuery.extend(Boxy, {
  87. WRAPPER: "<table cellspacing='0' cellpadding='0' border='0' class='boxy-wrapper'>" +
  88. "<tr><td class='top-left'></td><td class='top'></td><td class='top-right'></td></tr>" +
  89. "<tr><td class='left'></td><td class='boxy-inner'></td><td class='right'></td></tr>" +
  90. "<tr><td class='bottom-left'></td><td class='bottom'></td><td class='bottom-right'></td></tr>" +
  91. "</table>",
  92. DEFAULTS: {
  93. title: null, // titlebar text. titlebar will not be visible if not set.
  94. closeable: true, // display close link in titlebar?
  95. draggable: true, // can this dialog be dragged?
  96. clone: false, // clone content prior to insertion into dialog?
  97. actuator: null, // element which opened this dialog
  98. center: true, // center dialog in viewport?
  99. show: true, // show dialog immediately?
  100. modal: false, // make dialog modal?
  101. fixed: true, // use fixed positioning, if supported? absolute positioning used otherwise
  102. closeText: '[close]', // text to use for default close link
  103. unloadOnHide: false, // should this dialog be removed from the DOM after being hidden?
  104. clickToFront: false, // bring dialog to foreground on any click (not just titlebar)?
  105. behaviours: Boxy.EF, // function used to apply behaviours to all content embedded in dialog.
  106. afterDrop: Boxy.EF, // callback fired after dialog is dropped. executes in context of Boxy instance.
  107. afterShow: Boxy.EF, // callback fired after dialog becomes visible. executes in context of Boxy instance.
  108. afterHide: Boxy.EF, // callback fired after dialog is hidden. executed in context of Boxy instance.
  109. beforeUnload: Boxy.EF // callback fired after dialog is unloaded. executed in context of Boxy instance.
  110. },
  111. DEFAULT_X: 50,
  112. DEFAULT_Y: 50,
  113. zIndex: 1337,
  114. dragConfigured: false, // only set up one drag handler for all boxys
  115. resizeConfigured: false,
  116. dragging: null,
  117. // load a URL and display in boxy
  118. // url - url to load
  119. // options keys (any not listed below are passed to boxy constructor)
  120. // type: HTTP method, default: GET
  121. // cache: cache retrieved content? default: false
  122. // filter: jQuery selector used to filter remote content
  123. load: function(url, options) {
  124. options = options || {};
  125. var ajax = {
  126. url: url, type: 'GET', dataType: 'html', cache: false, success: function(html) {
  127. html = jQuery(html);
  128. if (options.filter) html = jQuery(options.filter, html);
  129. new Boxy(html, options);
  130. }
  131. };
  132. jQuery.each(['type', 'cache'], function() {
  133. if (this in options) {
  134. ajax[this] = options[this];
  135. delete options[this];
  136. }
  137. });
  138. jQuery.ajax(ajax);
  139. },
  140. // allows you to get a handle to the containing boxy instance of any element
  141. // e.g. <a href='#' onclick='alert(Boxy.get(this));'>inspect!</a>.
  142. // this returns the actual instance of the boxy 'class', not just a DOM element.
  143. // Boxy.get(this).hide() would be valid, for instance.
  144. get: function(ele) {
  145. var p = jQuery(ele).parents('.boxy-wrapper');
  146. return p.length ? jQuery.data(p[0], 'boxy') : null;
  147. },
  148. // returns the boxy instance which has been linked to a given element via the
  149. // 'actuator' constructor option.
  150. linkedTo: function(ele) {
  151. return jQuery.data(ele, 'active.boxy');
  152. },
  153. // displays an alert box with a given message, calling optional callback
  154. // after dismissal.
  155. alert: function(message, callback, options) {
  156. return Boxy.ask(message, ['OK'], callback, options);
  157. },
  158. // displays an alert box with a given message, calling after callback iff
  159. // user selects OK.
  160. confirm: function(message, after, options) {
  161. return Boxy.ask(message, ['OK', 'Cancel'], function(response) {
  162. if (response == 'OK') after();
  163. }, options);
  164. },
  165. // asks a question with multiple responses presented as buttons
  166. // selected item is returned to a callback method.
  167. // answers may be either an array or a hash. if it's an array, the
  168. // the callback will received the selected value. if it's a hash,
  169. // you'll get the corresponding key.
  170. ask: function(question, answers, callback, options) {
  171. options = jQuery.extend({modal: true, closeable: false},
  172. options || {},
  173. {show: true, unloadOnHide: true});
  174. var body = jQuery('<div></div>').append(jQuery('<div class="question"></div>').html(question));
  175. // ick
  176. var map = {}, answerStrings = [];
  177. if (answers instanceof Array) {
  178. for (var i = 0; i < answers.length; i++) {
  179. map[answers[i]] = answers[i];
  180. answerStrings.push(answers[i]);
  181. }
  182. } else {
  183. for (var k in answers) {
  184. map[answers[k]] = k;
  185. answerStrings.push(answers[k]);
  186. }
  187. }
  188. var buttons = jQuery('<form class="answers"></form>');
  189. buttons.html(jQuery.map(answerStrings, function(v) {
  190. return "<input type='button' value='" + v + "' />";
  191. }).join(' '));
  192. jQuery('input[type=button]', buttons).click(function() {
  193. var clicked = this;
  194. Boxy.get(this).hide(function() {
  195. if (callback) callback(map[clicked.value]);
  196. });
  197. });
  198. body.append(buttons);
  199. new Boxy(body, options);
  200. },
  201. // returns true if a modal boxy is visible, false otherwise
  202. isModalVisible: function() {
  203. return jQuery('.boxy-modal-blackout').length > 0;
  204. },
  205. _u: function() {
  206. for (var i = 0; i < arguments.length; i++)
  207. if (typeof arguments[i] != 'undefined') return false;
  208. return true;
  209. },
  210. _handleResize: function(evt) {
  211. var d = jQuery(document);
  212. jQuery('.boxy-modal-blackout').css('display', 'none').css({
  213. width: d.width(), height: d.height()
  214. }).css('display', 'block');
  215. },
  216. _handleDrag: function(evt) {
  217. var d;
  218. if (d = Boxy.dragging) {
  219. d[0].boxy.css({left: evt.pageX - d[1], top: evt.pageY - d[2]});
  220. }
  221. },
  222. _nextZ: function() {
  223. return Boxy.zIndex++;
  224. },
  225. _viewport: function() {
  226. var d = document.documentElement, b = document.body, w = window;
  227. return jQuery.extend(
  228. jQuery.browser.msie ?
  229. { left: b.scrollLeft || d.scrollLeft, top: b.scrollTop || d.scrollTop } :
  230. { left: w.pageXOffset, top: w.pageYOffset },
  231. !Boxy._u(w.innerWidth) ?
  232. { width: w.innerWidth, height: w.innerHeight } :
  233. (!Boxy._u(d) && !Boxy._u(d.clientWidth) && d.clientWidth != 0 ?
  234. { width: d.clientWidth, height: d.clientHeight } :
  235. { width: b.clientWidth, height: b.clientHeight }) );
  236. }
  237. });
  238. Boxy.prototype = {
  239. // Returns the size of this boxy instance without displaying it.
  240. // Do not use this method if boxy is already visible, use getSize() instead.
  241. estimateSize: function() {
  242. this.boxy.css({visibility: 'hidden', display: 'block'});
  243. var dims = this.getSize();
  244. this.boxy.css('display', 'none').css('visibility', 'visible');
  245. return dims;
  246. },
  247. // Returns the dimensions of the entire boxy dialog as [width,height]
  248. getSize: function() {
  249. return [this.boxy.width(), this.boxy.height()];
  250. },
  251. // Returns the dimensions of the content region as [width,height]
  252. getContentSize: function() {
  253. var c = this.getContent();
  254. return [c.width(), c.height()];
  255. },
  256. // Returns the position of this dialog as [x,y]
  257. getPosition: function() {
  258. var b = this.boxy[0];
  259. return [b.offsetLeft, b.offsetTop];
  260. },
  261. // Returns the center point of this dialog as [x,y]
  262. getCenter: function() {
  263. var p = this.getPosition();
  264. var s = this.getSize();
  265. return [Math.floor(p[0] + s[0] / 2), Math.floor(p[1] + s[1] / 2)];
  266. },
  267. // Returns a jQuery object wrapping the inner boxy region.
  268. // Not much reason to use this, you're probably more interested in getContent()
  269. getInner: function() {
  270. return jQuery('.boxy-inner', this.boxy);
  271. },
  272. // Returns a jQuery object wrapping the boxy content region.
  273. // This is the user-editable content area (i.e. excludes titlebar)
  274. getContent: function() {
  275. return jQuery('.boxy-content', this.boxy);
  276. },
  277. // Replace dialog content
  278. setContent: function(newContent) {
  279. newContent = jQuery(newContent).css({display: 'block'}).addClass('boxy-content');
  280. if (this.options.clone) newContent = newContent.clone(true);
  281. this.getContent().remove();
  282. this.getInner().append(newContent);
  283. this._setupDefaultBehaviours(newContent);
  284. this.options.behaviours.call(this, newContent);
  285. return this;
  286. },
  287. // Move this dialog to some position, funnily enough
  288. moveTo: function(x, y) {
  289. this.moveToX(x).moveToY(y);
  290. return this;
  291. },
  292. // Move this dialog (x-coord only)
  293. moveToX: function(x) {
  294. if (typeof x == 'number') this.boxy.css({left: x});
  295. else this.centerX();
  296. return this;
  297. },
  298. // Move this dialog (y-coord only)
  299. moveToY: function(y) {
  300. if (typeof y == 'number') this.boxy.css({top: y});
  301. else this.centerY();
  302. return this;
  303. },
  304. // Move this dialog so that it is centered at (x,y)
  305. centerAt: function(x, y) {
  306. var s = this[this.visible ? 'getSize' : 'estimateSize']();
  307. if (typeof x == 'number') this.moveToX(x - s[0] / 2);
  308. if (typeof y == 'number') this.moveToY(y - s[1] / 2);
  309. return this;
  310. },
  311. centerAtX: function(x) {
  312. return this.centerAt(x, null);
  313. },
  314. centerAtY: function(y) {
  315. return this.centerAt(null, y);
  316. },
  317. // Center this dialog in the viewport
  318. // axis is optional, can be 'x', 'y'.
  319. center: function(axis) {
  320. var v = Boxy._viewport();
  321. var o = this.options.fixed ? [0, 0] : [v.left, v.top];
  322. if (!axis || axis == 'x') this.centerAt(o[0] + v.width / 2, null);
  323. if (!axis || axis == 'y') this.centerAt(null, o[1] + v.height / 2);
  324. return this;
  325. },
  326. // Center this dialog in the viewport (x-coord only)
  327. centerX: function() {
  328. return this.center('x');
  329. },
  330. // Center this dialog in the viewport (y-coord only)
  331. centerY: function() {
  332. return this.center('y');
  333. },
  334. // Resize the content region to a specific size
  335. resize: function(width, height, after) {
  336. if (!this.visible) return;
  337. var bounds = this._getBoundsForResize(width, height);
  338. this.boxy.css({left: bounds[0], top: bounds[1]});
  339. this.getContent().css({width: bounds[2], height: bounds[3]});
  340. if (after) after(this);
  341. return this;
  342. },
  343. // Tween the content region to a specific size
  344. tween: function(width, height, after) {
  345. if (!this.visible) return;
  346. var bounds = this._getBoundsForResize(width, height);
  347. var self = this;
  348. this.boxy.stop().animate({left: bounds[0], top: bounds[1]});
  349. this.getContent().stop().animate({width: bounds[2], height: bounds[3]}, function() {
  350. if (after) after(self);
  351. });
  352. return this;
  353. },
  354. // Returns true if this dialog is visible, false otherwise
  355. isVisible: function() {
  356. return this.visible;
  357. },
  358. // Make this boxy instance visible
  359. show: function() {
  360. if (this.visible) return;
  361. if (this.options.modal) {
  362. var self = this;
  363. if (!Boxy.resizeConfigured) {
  364. Boxy.resizeConfigured = true;
  365. jQuery(window).resize(function() { Boxy._handleResize(); });
  366. }
  367. this.modalBlackout = jQuery('<div class="boxy-modal-blackout"></div>')
  368. .css({zIndex: Boxy._nextZ(),
  369. opacity: 0.7,
  370. width: jQuery(document).width(),
  371. height: jQuery(document).height()})
  372. .appendTo(document.body);
  373. this.toTop();
  374. if (this.options.closeable) {
  375. jQuery(document.body).bind('keypress.boxy', function(evt) {
  376. var key = evt.which || evt.keyCode;
  377. if (key == 27) {
  378. self.hide();
  379. jQuery(document.body).unbind('keypress.boxy');
  380. }
  381. });
  382. }
  383. }
  384. this.boxy.stop().css({opacity: 1}).show();
  385. this.visible = true;
  386. this._fire('afterShow');
  387. return this;
  388. },
  389. // Hide this boxy instance
  390. hide: function(after) {
  391. if (!this.visible) return;
  392. var self = this;
  393. if (this.options.modal) {
  394. jQuery(document.body).unbind('keypress.boxy');
  395. this.modalBlackout.animate({opacity: 0}, function() {
  396. jQuery(this).remove();
  397. });
  398. }
  399. this.boxy.stop().animate({opacity: 0}, 300, function() {
  400. self.boxy.css({display: 'none'});
  401. self.visible = false;
  402. self._fire('afterHide');
  403. if (after) after(self);
  404. if (self.options.unloadOnHide) self.unload();
  405. });
  406. return this;
  407. },
  408. toggle: function() {
  409. this[this.visible ? 'hide' : 'show']();
  410. return this;
  411. },
  412. hideAndUnload: function(after) {
  413. this.options.unloadOnHide = true;
  414. this.hide(after);
  415. return this;
  416. },
  417. unload: function() {
  418. this._fire('beforeUnload');
  419. this.boxy.remove();
  420. if (this.options.actuator) {
  421. jQuery.data(this.options.actuator, 'active.boxy', false);
  422. }
  423. },
  424. // Move this dialog box above all other boxy instances
  425. toTop: function() {
  426. this.boxy.css({zIndex: Boxy._nextZ()});
  427. return this;
  428. },
  429. // Returns the title of this dialog
  430. getTitle: function() {
  431. return jQuery('> .title-bar h2', this.getInner()).html();
  432. },
  433. // Sets the title of this dialog
  434. setTitle: function(t) {
  435. jQuery('> .title-bar h2', this.getInner()).html(t);
  436. return this;
  437. },
  438. //
  439. // Don't touch these privates
  440. _getBoundsForResize: function(width, height) {
  441. var csize = this.getContentSize();
  442. var delta = [width - csize[0], height - csize[1]];
  443. var p = this.getPosition();
  444. return [Math.max(p[0] - delta[0] / 2, 0),
  445. Math.max(p[1] - delta[1] / 2, 0), width, height];
  446. },
  447. _setupTitleBar: function() {
  448. if (this.options.title) {
  449. var self = this;
  450. var tb = jQuery("<div class='title-bar'></div>").html("<h2>" + this.options.title + "</h2>");
  451. if (this.options.closeable) {
  452. tb.append(jQuery("<a href='#' class='close'></a>").html(this.options.closeText));
  453. }
  454. if (this.options.draggable) {
  455. tb[0].onselectstart = function() { return false; }
  456. tb[0].unselectable = 'on';
  457. tb[0].style.MozUserSelect = 'none';
  458. if (!Boxy.dragConfigured) {
  459. jQuery(document).mousemove(Boxy._handleDrag);
  460. Boxy.dragConfigured = true;
  461. }
  462. tb.mousedown(function(evt) {
  463. self.toTop();
  464. Boxy.dragging = [self, evt.pageX - self.boxy[0].offsetLeft, evt.pageY - self.boxy[0].offsetTop];
  465. jQuery(this).addClass('dragging');
  466. }).mouseup(function() {
  467. jQuery(this).removeClass('dragging');
  468. Boxy.dragging = null;
  469. self._fire('afterDrop');
  470. });
  471. }
  472. this.getInner().prepend(tb);
  473. this._setupDefaultBehaviours(tb);
  474. }
  475. },
  476. _setupDefaultBehaviours: function(root) {
  477. var self = this;
  478. if (this.options.clickToFront) {
  479. root.click(function() { self.toTop(); });
  480. }
  481. jQuery('.close', root).click(function() {
  482. self.hide();
  483. return false;
  484. }).mousedown(function(evt) { evt.stopPropagation(); });
  485. },
  486. _fire: function(event) {
  487. this.options[event].call(this);
  488. }
  489. };