J40.js
319 lines
| 1 | (function($, w) { |
| 2 | 'use strict'; |
| 3 | |
| 4 | /** |
| 5 | * Wrap procedures within a registered function, in order |
| 6 | * to execute them only in certain cases. |
| 7 | * |
| 8 | * @return void |
| 9 | */ |
| 10 | w['__vikappointments_j40_adapter'] = (selector) => { |
| 11 | if (!selector) { |
| 12 | // use default selector when not specified |
| 13 | selector = 'body.com_vikappointments #content, body.com_vikappointments.component'; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Make text, email and phone fields bigger. |
| 18 | */ |
| 19 | $(selector) |
| 20 | .find('input,textarea') |
| 21 | .each(function() { |
| 22 | if ($(this).is('input')) { |
| 23 | // get input type |
| 24 | switch ($(this).attr('type')) { |
| 25 | case 'checkbox': |
| 26 | case 'hidden': |
| 27 | // ignore field |
| 28 | return true; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | $(this).addClass('form-control'); |
| 33 | }); |
| 34 | |
| 35 | /** |
| 36 | * Adjust button groups to new Bootstrap structure. |
| 37 | */ |
| 38 | $(selector) |
| 39 | .find('.input-append,.input-prepend') |
| 40 | .removeClass('input-append') |
| 41 | .removeClass('input-prepend') |
| 42 | .each(function() { |
| 43 | $($(this).children()).wrapAll('<div class="input-group"></div>'); |
| 44 | }); |
| 45 | |
| 46 | /* |
| 47 | $(selector) |
| 48 | .find('.input-append,.input-prepend') |
| 49 | .removeClass('input-append') |
| 50 | .removeClass('input-prepend') |
| 51 | .addClass('input-group') |
| 52 | .each(function() { |
| 53 | // find first input inside button group |
| 54 | var input = $(this).find('input[type!="hidden"]'); |
| 55 | |
| 56 | // get all elements before the input |
| 57 | var prevElems = input.prevAll().not('[type="hidden"]'); |
| 58 | |
| 59 | // check if we have at least an element |
| 60 | if (prevElems.length) { |
| 61 | // create append group and insert it before the input |
| 62 | var group = $('<span class="input-group-prepend"></span>').insertBefore(input); |
| 63 | |
| 64 | // detach all elements and move them into the created group |
| 65 | prevElems.detach().appendTo(group); |
| 66 | |
| 67 | // add "primary" style to button |
| 68 | var btn = prevElems.filter('.btn').addClass('btn-primary'); |
| 69 | |
| 70 | // check if we have more than a button |
| 71 | if (btn.length > 1) { |
| 72 | // add "secondary" style to odd buttons |
| 73 | btn.filter(':odd').removeClass('btn-primary').addClass('btn-secondary'); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // get all elements next to the input |
| 78 | var nextElems = input.nextAll().not('[type="hidden"]'); |
| 79 | |
| 80 | // check if we have at least an element |
| 81 | if (nextElems.length) { |
| 82 | // create append group and insert it after the input |
| 83 | var group = $('<span class="input-group-append"></span>').insertAfter(input); |
| 84 | |
| 85 | // detach all elements and move them into the created group |
| 86 | nextElems.detach().appendTo(group); |
| 87 | |
| 88 | // add "primary" style to button |
| 89 | var btn = nextElems.filter('.btn').addClass('btn-primary'); |
| 90 | |
| 91 | // check if we have more than a button |
| 92 | if (btn.length > 1) { |
| 93 | // add "secondary" style to even buttons |
| 94 | btn.filter(':even').removeClass('btn-primary').addClass('btn-secondary'); |
| 95 | } |
| 96 | } |
| 97 | }); |
| 98 | */ |
| 99 | |
| 100 | /** |
| 101 | * Adjust form fieldsets to new Bootstrap structure. |
| 102 | */ |
| 103 | $(selector) |
| 104 | .find('.row-fluid') |
| 105 | .removeClass('row-fluid') |
| 106 | .addClass('row') |
| 107 | .find('div[class^="span"]') |
| 108 | .each(function() { |
| 109 | // fetch span size |
| 110 | var size = $(this).attr('class').match(/\bspan(\d+)\b/); |
| 111 | |
| 112 | if (size) { |
| 113 | // remove previous class and insert the new one |
| 114 | $(this).removeClass(size[0]).addClass('col-lg-' + size[1]); |
| 115 | } |
| 116 | }); |
| 117 | |
| 118 | /** |
| 119 | * Replace class of small buttons with the correct one. |
| 120 | */ |
| 121 | $(selector) |
| 122 | .find('.btn-mini') |
| 123 | .filter('button,a') |
| 124 | .each(function() { |
| 125 | $(this).removeClass('btn-mini'); |
| 126 | $(this).addClass('btn-sm btn-secondary'); |
| 127 | }); |
| 128 | |
| 129 | /** |
| 130 | * Add classes needed to properly display the buttons according |
| 131 | * to the classes used by Joomla 4. |
| 132 | */ |
| 133 | $(selector) |
| 134 | .find('.btn') |
| 135 | .filter('button,a,span') |
| 136 | .not('[onclick^="vapToggleSearchToolsButton"]') |
| 137 | .each(function() { |
| 138 | var _class = $(this).attr('class'); |
| 139 | |
| 140 | if ($(this).parent().hasClass('buttons-wrapper')) { |
| 141 | // we are probably editing the buttons of the calendar |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | // make sure the button doesn't own any style |
| 146 | if (!_class.match(/\bbtn-(?:success|danger|primary|secondary)\b/)) { |
| 147 | // add default primary class to buttons |
| 148 | $(this).addClass('btn-primary'); |
| 149 | } |
| 150 | }); |
| 151 | |
| 152 | /** |
| 153 | * Adjust style and text of search tools button. |
| 154 | * |
| 155 | * @note: DO NOT add btn-primary class to button, otherwise |
| 156 | * the first click will have no effect. |
| 157 | */ |
| 158 | $(selector) |
| 159 | .find('button[onclick^="vapToggleSearchToolsButton"]') |
| 160 | .each(function() { |
| 161 | // get button HTML |
| 162 | var html = $(this).html(); |
| 163 | // replace JSEARCH_TOOLS with localised string |
| 164 | $(this).html(html.replace(/JSEARCH_TOOLS/, Joomla.JText._('JFILTER_OPTIONS'))); |
| 165 | // add special class |
| 166 | $(this).addClass('js-stools-btn-filter'); |
| 167 | |
| 168 | // look for a clear button |
| 169 | var clear = $(this).closest('.btn-toolbar').find('button[onclick^="clearFilter"]'); |
| 170 | |
| 171 | if (clear.length) { |
| 172 | // save parent of clear button |
| 173 | var dummy = clear.parent(); |
| 174 | |
| 175 | // detach clear button from its position and move it inside the |
| 176 | // same button group of the search tools |
| 177 | clear.detach().appendTo($(this).parent()); |
| 178 | // add special class |
| 179 | clear.addClass('js-stools-btn-clear'); |
| 180 | |
| 181 | // remove wrapper in case there are no children in it |
| 182 | if (dummy.children().length == 0) { |
| 183 | dummy.remove(); |
| 184 | } |
| 185 | } |
| 186 | }); |
| 187 | |
| 188 | /** |
| 189 | * Add style to filter bar. |
| 190 | */ |
| 191 | $(selector) |
| 192 | .find('.btn-toolbar') |
| 193 | .next('.btn-toolbar') |
| 194 | .addClass('js-stools-container-filters clearfix js-stools-container-filters-visible'); |
| 195 | |
| 196 | /** |
| 197 | * Adjust warnings to be a bit less obtrusive. |
| 198 | */ |
| 199 | $(selector) |
| 200 | .find('.btn-toolbar') |
| 201 | .nextAll('.alert-warning') |
| 202 | .each(function() { |
| 203 | // make sure the alert is not dismissible |
| 204 | if ($(this).find('.close').length == 0) { |
| 205 | // remove warning class |
| 206 | $(this).removeClass('alert-warning'); |
| 207 | // add info class |
| 208 | $(this).addClass('alert-info'); |
| 209 | |
| 210 | // replace alert icon with the correct one |
| 211 | $(this).find('span.fas') |
| 212 | .removeClass('fa-exclamation-triangle') |
| 213 | .addClass('fa-info-circle'); |
| 214 | } |
| 215 | }); |
| 216 | |
| 217 | $(selector) |
| 218 | .find('.dashboard-wrapper') |
| 219 | .removeClass('row'); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Propagate Bootstrap modal events without the namespace usage. |
| 224 | */ |
| 225 | $(document).on('show.bs.modal hide.bs.modal shown.bs.modal hidden.bs.modal', '.joomla-modal', function(event) { |
| 226 | if (event.namespace == 'bs.modal') { |
| 227 | // propagate only in case of namespace set in order |
| 228 | // to avoid a recursive loop |
| 229 | $(this).trigger(event.type); |
| 230 | } |
| 231 | }); |
| 232 | |
| 233 | /** |
| 234 | * Catch all the inspectors that loads the contents via AJAX. |
| 235 | * Then reformat the HTML with the j40 adapter callback. |
| 236 | */ |
| 237 | $(w).on('inspector.aftershow', function(event) { |
| 238 | // check whether the inspector needs to load the contents via AJAX |
| 239 | if (event.params && event.params.url) { |
| 240 | // adapt the inspector body |
| 241 | __vikappointments_j40_adapter(event.target); |
| 242 | } |
| 243 | }); |
| 244 | |
| 245 | // only once the document is ready, do the following tasks |
| 246 | $(function() { |
| 247 | /** |
| 248 | * Auto-adjusts the HTML structure while adding a new option to |
| 249 | * a select custom field. |
| 250 | */ |
| 251 | $('#vap-customf-select-choose').on('select.option.add', function(event) { |
| 252 | __vikappointments_j40_adapter(this); |
| 253 | }); |
| 254 | |
| 255 | /** |
| 256 | * Adds an hook to the request used to load the Payment fields. |
| 257 | * Adjust Payment form on request completion. |
| 258 | */ |
| 259 | $('.vikpayparamdiv').on('payment.load', function(event) { |
| 260 | __vikappointments_j40_adapter(this); |
| 261 | }); |
| 262 | |
| 263 | /** |
| 264 | * Adds an hook to the request used to load the SMS API fields. |
| 265 | * Adjust SMS API form on request completion. |
| 266 | */ |
| 267 | $('#vap-smsapi-params-table').on('smsapi.load', function(event) { |
| 268 | __vikappointments_j40_adapter(this); |
| 269 | }); |
| 270 | |
| 271 | /** |
| 272 | * Adds an hook to the request used to complete a wizard step. |
| 273 | * Adjust wizard step layout on request completion. |
| 274 | */ |
| 275 | $(window).on('wizard.postflight.after', (event) => { |
| 276 | __vikappointments_j40_adapter(event.params.step); |
| 277 | }); |
| 278 | |
| 279 | /** |
| 280 | * Attach click listener to the button used to append an IP address |
| 281 | * within the list of an API user. |
| 282 | */ |
| 283 | $(document).on('click', '#add-ip', function(event) { |
| 284 | __vikappointments_j40_adapter('#ips-container'); |
| 285 | }); |
| 286 | |
| 287 | /** |
| 288 | * Attach click listener to the button used to append a ZIP interval. |
| 289 | */ |
| 290 | $(document).on('click', '#vapaddzipbutton', function(event) { |
| 291 | __vikappointments_j40_adapter('#vapzipcodescont'); |
| 292 | }); |
| 293 | |
| 294 | /** |
| 295 | * Add custom class to any select rendered with Chosen. |
| 296 | */ |
| 297 | $.fn.chosen = function() { |
| 298 | /** |
| 299 | * Style only single select on Joomla 4. |
| 300 | */ |
| 301 | $(this).not('[multiple]').not('.time').addClass('form-select'); |
| 302 | |
| 303 | /** |
| 304 | * Check whether the page supports at least a multiple select, |
| 305 | * then render them through select2 on Joomla 4. |
| 306 | */ |
| 307 | const multiSelect = $(this).filter('[multiple]'); |
| 308 | |
| 309 | if (multiSelect.length) { |
| 310 | multiSelect.select2({ |
| 311 | allowClear: true, |
| 312 | width: 'resolve', |
| 313 | }); |
| 314 | } |
| 315 | }; |
| 316 | }); |
| 317 | |
| 318 | })(jQuery, window); |
| 319 |