components_modals.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Bootstrap modals and extensions
  4. *
  5. * Demo JS code for components_modals.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var Modals = function () {
  11. //
  12. // Setup module components
  13. //
  14. // Load remote content
  15. var _componentModalRemote = function() {
  16. $('#modal_remote').on('show.bs.modal', function() {
  17. $(this).find('.modal-body').load('../../../../global_assets/demo_data/wizard/education.html', function() {
  18. _componentSelect2();
  19. });
  20. });
  21. };
  22. // Modal callbacks
  23. var _componentModalCallbacks = function() {
  24. // onShow callback
  25. $('#modal_onshow').on('show.bs.modal', function() {
  26. alert('onShow callback fired.')
  27. });
  28. // onShown callback
  29. $('#modal_onshown').on('shown.bs.modal', function() {
  30. alert('onShown callback fired.')
  31. });
  32. // onHide callback
  33. $('#modal_onhide').on('hide.bs.modal', function() {
  34. alert('onHide callback fired.')
  35. });
  36. // onHidden callback
  37. $('#modal_onhidden').on('hidden.bs.modal', function() {
  38. alert('onHidden callback fired.')
  39. });
  40. };
  41. // Bootbox extension
  42. var _componentModalBootbox = function() {
  43. if (typeof bootbox == 'undefined') {
  44. console.warn('Warning - bootbox.min.js is not loaded.');
  45. return;
  46. }
  47. // Alert dialog
  48. $('#alert').on('click', function() {
  49. bootbox.alert({
  50. title: 'Check this out!',
  51. message: 'Native alert dialog has been replaced with Bootbox alert box.'
  52. });
  53. });
  54. // Confirmation dialog
  55. $('#confirm').on('click', function() {
  56. bootbox.confirm({
  57. title: 'Confirm dialog',
  58. message: 'Native confirm dialog has been replaced with Bootbox confirm box.',
  59. buttons: {
  60. confirm: {
  61. label: 'Yes',
  62. className: 'btn-primary'
  63. },
  64. cancel: {
  65. label: 'Cancel',
  66. className: 'btn-link'
  67. }
  68. },
  69. callback: function (result) {
  70. bootbox.alert({
  71. title: 'Confirmation result',
  72. message: 'Confirm result: ' + result
  73. });
  74. }
  75. });
  76. });
  77. // Prompt dialog
  78. $('#prompt').on('click', function() {
  79. bootbox.prompt({
  80. title: 'Please enter your name',
  81. buttons: {
  82. confirm: {
  83. label: 'Yes',
  84. className: 'btn-primary'
  85. },
  86. cancel: {
  87. label: 'Cancel',
  88. className: 'btn-link'
  89. }
  90. },
  91. callback: function (result) {
  92. if (result === null) {
  93. bootbox.alert({
  94. title: 'Prompt dismissed',
  95. message: 'You have cancelled this damn thing'
  96. });
  97. } else {
  98. bootbox.alert({
  99. title: 'Hi <strong>' + result + '</strong>',
  100. message: 'How are you doing today?'
  101. });
  102. }
  103. }
  104. });
  105. });
  106. // Prompt dialog with default value
  107. $('#prompt_value').on('click', function() {
  108. bootbox.prompt({
  109. title: 'What is your real name?',
  110. value: 'Eugene Kopyov',
  111. buttons: {
  112. confirm: {
  113. label: 'Yes',
  114. className: 'btn-primary'
  115. },
  116. cancel: {
  117. label: 'Cancel',
  118. className: 'btn-link'
  119. }
  120. },
  121. callback: function(result) {
  122. if (result === null) {
  123. bootbox.alert({
  124. title: 'Prompt dismissed',
  125. message: 'You have cancelled this damn thing'
  126. });
  127. } else {
  128. bootbox.alert({
  129. title: 'Hi <strong>' + result + '</strong>',
  130. message: 'How are you doing today?'
  131. });
  132. }
  133. }
  134. });
  135. });
  136. // Custom bootbox dialog
  137. $('#bootbox_custom').on('click', function() {
  138. bootbox.dialog({
  139. message: 'I am a custom dialog',
  140. title: 'Custom title',
  141. buttons: {
  142. success: {
  143. label: 'Success!',
  144. className: 'btn-success',
  145. callback: function() {
  146. bootbox.alert({
  147. title: 'Success!',
  148. message: 'This is a great success!'
  149. });
  150. }
  151. },
  152. danger: {
  153. label: 'Danger!',
  154. className: 'btn-danger',
  155. callback: function() {
  156. bootbox.alert({
  157. title: 'Ohh noooo!',
  158. message: 'Uh oh, look out!'
  159. });
  160. }
  161. },
  162. main: {
  163. label: 'Click ME!',
  164. className: 'btn-primary',
  165. callback: function() {
  166. bootbox.alert({
  167. title: 'Hooray!',
  168. message: 'Something awesome just happened!'
  169. });
  170. }
  171. }
  172. }
  173. });
  174. });
  175. // Custom bootbox dialog with form
  176. $('#bootbox_form').on('click', function() {
  177. bootbox.dialog({
  178. title: 'This is a form in a modal.',
  179. message: '<div class="row"> ' +
  180. '<div class="col-md-12">' +
  181. '<form action="">' +
  182. '<div class="form-group row">' +
  183. '<label class="col-md-4 col-form-label">Name</label>' +
  184. '<div class="col-md-8">' +
  185. '<input id="name" name="name" type="text" placeholder="Your name" class="form-control">' +
  186. '<span class="form-text text-muted">Here goes your name</span>' +
  187. '</div>' +
  188. '</div>' +
  189. '<div class="form-group row">' +
  190. '<label class="col-md-4 col-form-label">How awesome is this?</label>' +
  191. '<div class="col-md-8">' +
  192. '<div class="form-check">' +
  193. '<label class="form-check-label">' +
  194. '<input type="radio" class="form-check-input" name="awesomeness" id="awesomeness-0" value="Really awesome" checked>' +
  195. 'Really awesomeness' +
  196. '</label>' +
  197. '</div>' +
  198. '<div class="form-check">' +
  199. '<label class="form-check-label">' +
  200. '<input type="radio" class="form-check-input" name="awesomeness" id="awesomeness-1" value="Super awesome">' +
  201. 'Super awesome' +
  202. '</label>' +
  203. '</div>' +
  204. '</div>' +
  205. '</div>' +
  206. '</form>' +
  207. '</div>' +
  208. '</div>',
  209. buttons: {
  210. success: {
  211. label: 'Save',
  212. className: 'btn-success',
  213. callback: function () {
  214. var name = $('#name').val();
  215. var answer = $('input[name="awesomeness"]:checked').val()
  216. bootbox.alert({
  217. title: 'Hello ' + name + '!',
  218. message: 'You have chosen <strong>' + answer + '</strong>.'
  219. });
  220. }
  221. }
  222. }
  223. }
  224. );
  225. });
  226. };
  227. // Select2
  228. var _componentSelect2 = function() {
  229. if (!$().select2) {
  230. console.warn('Warning - select2.min.js is not loaded.');
  231. return;
  232. }
  233. // Initialize
  234. $('.form-control-select2').select2({
  235. minimumResultsForSearch: Infinity
  236. });
  237. };
  238. //
  239. // Return objects assigned to module
  240. //
  241. return {
  242. initComponents: function() {
  243. _componentModalRemote();
  244. _componentModalCallbacks();
  245. _componentModalBootbox();
  246. }
  247. }
  248. }();
  249. // Initialize module
  250. // ------------------------------
  251. document.addEventListener('DOMContentLoaded', function() {
  252. Modals.initComponents();
  253. });