editor.jqueryui.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*! jQuery UI integration for DataTables' Editor
  2. * ©2015 SpryMedia Ltd - datatables.net/license
  3. */
  4. (function( factory ){
  5. if ( typeof define === 'function' && define.amd ) {
  6. // AMD
  7. define( ['jquery', 'datatables.net-jqui', 'datatables.net-editor'], function ( $ ) {
  8. return factory( $, window, document );
  9. } );
  10. }
  11. else if ( typeof exports === 'object' ) {
  12. // CommonJS
  13. module.exports = function (root, $) {
  14. if ( ! root ) {
  15. root = window;
  16. }
  17. if ( ! $ || ! $.fn.dataTable ) {
  18. $ = require('datatables.net-jqui')(root, $).$;
  19. }
  20. if ( ! $.fn.dataTable.Editor ) {
  21. require('datatables.net-editor')(root, $);
  22. }
  23. return factory( $, root, root.document );
  24. };
  25. }
  26. else {
  27. // Browser
  28. factory( jQuery, window, document );
  29. }
  30. }(function( $, window, document, undefined ) {
  31. 'use strict';
  32. var DataTable = $.fn.dataTable;
  33. var Editor = DataTable.Editor;
  34. var doingClose = false;
  35. /*
  36. * Set the default display controller to be our foundation control
  37. */
  38. Editor.defaults.display = "jqueryui";
  39. /*
  40. * Change the default classes from Editor to be classes for Bootstrap
  41. */
  42. $.extend( true, $.fn.dataTable.Editor.classes, {
  43. form: {
  44. button: "btn ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
  45. }
  46. } );
  47. /*
  48. * jQuery UI display controller - this is effectively a proxy to the jQuery UI
  49. * modal control.
  50. */
  51. Editor.display.jqueryui = $.extend( true, {}, Editor.models.displayController, {
  52. init: function ( dte ) {
  53. dte.__dialouge = $('<div/>')
  54. .css('display', 'none')
  55. .appendTo('body')
  56. .dialog( $.extend( true, Editor.display.jqueryui.modalOptions, {
  57. autoOpen: false,
  58. buttons: { "A": function () {} }, // fake button so the button container is created
  59. closeOnEscape: false // allow editor's escape function to run
  60. } ) );
  61. // Need to know when the dialogue is closed using its own trigger
  62. // so we can reset the form
  63. $(dte.__dialouge).on( 'dialogclose', function (e) {
  64. if ( ! doingClose ) {
  65. dte.close();
  66. }
  67. } );
  68. return Editor.display.jqueryui;
  69. },
  70. open: function ( dte, append, callback ) {
  71. dte.__dialouge
  72. .append( append )
  73. .dialog( 'open' );
  74. $(dte.dom.formError).appendTo(
  75. dte.__dialouge.parent().find('div.ui-dialog-buttonpane')
  76. );
  77. dte.__dialouge.parent().find('.ui-dialog-title').html( dte.dom.header.innerHTML );
  78. dte.__dialouge.parent().addClass('DTED');
  79. // Modify the Editor buttons to be jQuery UI suitable
  80. var buttons = $(dte.dom.buttons)
  81. .children()
  82. .addClass( 'ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only' )
  83. .each( function () {
  84. $(this).wrapInner( '<span class="ui-button-text" />' );
  85. } );
  86. // Move the buttons into the jQuery UI button set
  87. dte.__dialouge.parent().find('div.ui-dialog-buttonset')
  88. .empty()
  89. .append( buttons.parent() );
  90. if ( callback ) {
  91. callback();
  92. }
  93. },
  94. close: function ( dte, callback ) {
  95. if ( dte.__dialouge ) {
  96. // Don't want to trigger a close() call from dialogclose!
  97. doingClose = true;
  98. dte.__dialouge.dialog( 'close' );
  99. doingClose = false;
  100. }
  101. if ( callback ) {
  102. callback();
  103. }
  104. },
  105. node: function ( dte ) {
  106. return dte.__dialouge[0];
  107. },
  108. // jQuery UI dialogues perform their own focus capture
  109. captureFocus: false
  110. } );
  111. Editor.display.jqueryui.modalOptions = {
  112. width: 600,
  113. modal: true
  114. };
  115. return DataTable.Editor;
  116. }));