jquery.ocdialog.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. (function($) {
  2. $.widget('oc.ocdialog', {
  3. options: {
  4. width: 'auto',
  5. height: 'auto',
  6. closeButton: true,
  7. closeOnEscape: true,
  8. modal: false
  9. },
  10. _create: function() {
  11. var self = this;
  12. this.originalCss = {
  13. display: this.element[0].style.display,
  14. width: this.element[0].style.width,
  15. height: this.element[0].style.height
  16. };
  17. this.originalTitle = this.element.attr('title');
  18. this.options.title = this.options.title || this.originalTitle;
  19. this.$dialog = $('<div class="oc-dialog" />')
  20. .attr({
  21. // Setting tabIndex makes the div focusable
  22. tabIndex: -1,
  23. role: 'dialog'
  24. })
  25. .insertBefore(this.element);
  26. this.$dialog.append(this.element.detach());
  27. this.element.removeAttr('title').addClass('oc-dialog-content').appendTo(this.$dialog);
  28. this.$dialog.css({
  29. display: 'inline-block',
  30. position: 'fixed'
  31. });
  32. $(document).on('keydown keyup', function(event) {
  33. if (
  34. event.target !== self.$dialog.get(0) &&
  35. self.$dialog.find($(event.target)).length === 0
  36. ) {
  37. return;
  38. }
  39. // Escape
  40. if (
  41. event.keyCode === 27 &&
  42. event.type === 'keydown' &&
  43. self.options.closeOnEscape
  44. ) {
  45. event.stopImmediatePropagation();
  46. self.close();
  47. return false;
  48. }
  49. // Enter
  50. if(event.keyCode === 13) {
  51. event.stopImmediatePropagation();
  52. if(event.type === 'keyup') {
  53. event.preventDefault();
  54. return false;
  55. }
  56. // If no button is selected we trigger the primary
  57. if (
  58. self.$buttonrow &&
  59. self.$buttonrow.find($(event.target)).length === 0
  60. ) {
  61. var $button = self.$buttonrow.find('button.primary');
  62. if($button) {
  63. $button.trigger('click');
  64. }
  65. } else if(self.$buttonrow) {
  66. $(event.target).trigger('click');
  67. }
  68. return false;
  69. }
  70. });
  71. $(window).resize(function() {
  72. self.parent = self.$dialog.parent().length > 0 ? self.$dialog.parent() : $('body');
  73. var pos = self.parent.position();
  74. self.$dialog.css({
  75. left: pos.left + ($(window).innerWidth() - self.$dialog.outerWidth())/2,
  76. top: pos.top + ($(window).innerHeight() - self.$dialog.outerHeight())/2
  77. });
  78. });
  79. this._setOptions(this.options);
  80. $(window).trigger('resize');
  81. this._createOverlay();
  82. },
  83. _init: function() {
  84. this.$dialog.focus();
  85. this._trigger('open');
  86. },
  87. _setOption: function(key, value) {
  88. var self = this;
  89. switch(key) {
  90. case 'title':
  91. if(this.$title) {
  92. this.$title.text(value);
  93. } else {
  94. var $title = $('<h3 class="oc-dialog-title">'
  95. + value
  96. + '</h3>');
  97. this.$title = $title.prependTo(this.$dialog);
  98. }
  99. this._setSizes();
  100. break;
  101. case 'buttons':
  102. if(this.$buttonrow) {
  103. this.$buttonrow.empty();
  104. } else {
  105. var $buttonrow = $('<div class="oc-dialog-buttonrow" />');
  106. this.$buttonrow = $buttonrow.appendTo(this.$dialog);
  107. }
  108. if (value.length === 1) {
  109. this.$buttonrow.addClass('onebutton');
  110. } else if (value.length === 2) {
  111. this.$buttonrow.addClass('twobuttons');
  112. } else if (value.length === 3) {
  113. this.$buttonrow.addClass('threebuttons');
  114. }
  115. $.each(value, function(idx, val) {
  116. var $button = $('<button>').text(val.text);
  117. if (val.classes) {
  118. $button.addClass(val.classes);
  119. }
  120. if(val.defaultButton) {
  121. $button.addClass('primary');
  122. self.$defaultButton = $button;
  123. }
  124. self.$buttonrow.append($button);
  125. $button.click(function() {
  126. val.click.apply(self.element[0], arguments);
  127. });
  128. });
  129. this.$buttonrow.find('button')
  130. .on('focus', function(event) {
  131. self.$buttonrow.find('button').removeClass('primary');
  132. $(this).addClass('primary');
  133. });
  134. this._setSizes();
  135. break;
  136. case 'closeButton':
  137. if(value) {
  138. var $closeButton = $('<a class="oc-dialog-close svg"></a>');
  139. this.$dialog.prepend($closeButton);
  140. $closeButton.on('click', function() {
  141. self.close();
  142. });
  143. } else {
  144. this.$dialog.find('.oc-dialog-close').remove();
  145. }
  146. break;
  147. case 'width':
  148. this.$dialog.css('width', value);
  149. break;
  150. case 'height':
  151. this.$dialog.css('height', value);
  152. break;
  153. case 'close':
  154. this.closeCB = value;
  155. break;
  156. }
  157. //this._super(key, value);
  158. $.Widget.prototype._setOption.apply(this, arguments );
  159. },
  160. _setOptions: function(options) {
  161. //this._super(options);
  162. $.Widget.prototype._setOptions.apply(this, arguments);
  163. },
  164. _setSizes: function() {
  165. var content_height = this.$dialog.height();
  166. if(this.$title) {
  167. content_height -= this.$title.outerHeight(true);
  168. }
  169. if(this.$buttonrow) {
  170. content_height -= this.$buttonrow.outerHeight(true);
  171. }
  172. this.parent = this.$dialog.parent().length > 0 ? this.$dialog.parent() : $('body');
  173. content_height = Math.min(content_height, this.parent.height()-20);
  174. if (content_height> 0) {
  175. this.element.css({
  176. height: content_height + 'px',
  177. width: this.$dialog.innerWidth()-20 + 'px'
  178. });
  179. } else {
  180. this.element.css({
  181. width : this.$dialog.innerWidth() - 20 + 'px'
  182. });
  183. }
  184. },
  185. _createOverlay: function() {
  186. if(!this.options.modal) {
  187. return;
  188. }
  189. var self = this;
  190. this.overlay = $('<div>')
  191. .addClass('oc-dialog-dim')
  192. .appendTo($('#content'));
  193. this.overlay.on('click keydown keyup', function(event) {
  194. if(event.target !== self.$dialog.get(0) && self.$dialog.find($(event.target)).length === 0) {
  195. event.preventDefault();
  196. event.stopPropagation();
  197. return;
  198. }
  199. });
  200. },
  201. _destroyOverlay: function() {
  202. if (!this.options.modal) {
  203. return;
  204. }
  205. if (this.overlay) {
  206. this.overlay.off('click keydown keyup');
  207. this.overlay.remove();
  208. this.overlay = null;
  209. }
  210. },
  211. widget: function() {
  212. return this.$dialog;
  213. },
  214. close: function() {
  215. this._destroyOverlay();
  216. var self = this;
  217. // Ugly hack to catch remaining keyup events.
  218. setTimeout(function() {
  219. self._trigger('close', self);
  220. self.$dialog.hide();
  221. }, 200);
  222. },
  223. destroy: function() {
  224. if(this.$title) {
  225. this.$title.remove();
  226. }
  227. if(this.$buttonrow) {
  228. this.$buttonrow.remove();
  229. }
  230. if(this.originalTitle) {
  231. this.element.attr('title', this.originalTitle);
  232. }
  233. this.element.removeClass('oc-dialog-content')
  234. .css(this.originalCss).detach().insertBefore(this.$dialog);
  235. this.$dialog.remove();
  236. }
  237. });
  238. }(jQuery));