admin
1 week ago
front
1 day ago
actions.js
1 day ago
latecheckbox.js
1 year ago
lateselect.js
1 year ago
notifications.js
1 year ago
shared.js
1 year ago
time.js
1 year ago
actions.js
445 lines
| 1 | function latepoint_generate_form_message_html(messages, status){ |
| 2 | var message_html = '<div class="os-form-message-w status-' + status + '"><ul>'; |
| 3 | if(Array.isArray(messages)){ |
| 4 | messages.forEach(function(message){ |
| 5 | message_html+= '<li>' + message + '</li>'; |
| 6 | }); |
| 7 | }else{ |
| 8 | message_html+= '<li>' + messages + '</li>'; |
| 9 | } |
| 10 | message_html+= '</ul></div>'; |
| 11 | return message_html; |
| 12 | } |
| 13 | |
| 14 | function latepoint_display_in_side_sub_panel(html){ |
| 15 | if(!jQuery('.latepoint-side-panel-w').length) latepoint_show_data_in_side_panel(''); |
| 16 | jQuery('.latepoint-side-panel-w .latepoint-side-panels .side-sub-panel-wrapper').remove(); |
| 17 | jQuery('.latepoint-side-panel-w .latepoint-side-panels').append(html); |
| 18 | } |
| 19 | |
| 20 | function latepoint_clear_form_messages($form){ |
| 21 | $form.find('.os-form-message-w').remove(); |
| 22 | } |
| 23 | |
| 24 | function latepoint_show_data_in_side_panel(message, extra_classes = '', close_btn = true){ |
| 25 | jQuery('.latepoint-side-panel-w').remove(); |
| 26 | jQuery('body').append('<div class="latepoint-side-panel-w ' + extra_classes + ' os-loading"><div class="latepoint-side-panel-shadow"></div><div class="latepoint-side-panels"><div class="latepoint-side-panel-i"></div></div></div>'); |
| 27 | jQuery('.latepoint-side-panel-i').html(message); |
| 28 | if(close_btn){ |
| 29 | jQuery('.latepoint-side-panel-i').find('.os-form-header .latepoint-side-panel-close').remove(); |
| 30 | jQuery('.latepoint-side-panel-i').find('.os-form-header').append('<a href="#" class="latepoint-side-panel-close latepoint-side-panel-close-trigger"><i class="latepoint-icon latepoint-icon-x"></i></a>'); |
| 31 | } |
| 32 | setTimeout(function(){ |
| 33 | jQuery('.latepoint-side-panel-w').removeClass('os-loading'); |
| 34 | }, 100); |
| 35 | } |
| 36 | |
| 37 | function latepoint_show_data_in_lightbox(message, extra_classes = '', close_btn = true, tag = 'div', inner_extra_classes = '', inner_tag = 'div'){ |
| 38 | jQuery('.latepoint-lightbox-w').remove(); |
| 39 | let lightbox_css_classes = 'latepoint-lightbox-w latepoint-w latepoint-border-radius-' + latepoint_helper.style_border_radius+ ' '; |
| 40 | if(extra_classes) lightbox_css_classes+= extra_classes; |
| 41 | let lightbox_css_inner_classes = 'latepoint-lightbox-i '; |
| 42 | if(inner_extra_classes) lightbox_css_inner_classes += inner_extra_classes; |
| 43 | |
| 44 | let close_btn_html = close_btn ? '<a href="#" class="latepoint-lightbox-close" tabindex="0"><i class="latepoint-icon latepoint-icon-x"></i></a>' : ''; |
| 45 | jQuery('body').append('<'+tag+' class="'+ lightbox_css_classes +'"><'+inner_tag+' class="'+ lightbox_css_inner_classes +'">' + message + close_btn_html + '</'+inner_tag+'><div class="latepoint-lightbox-shadow"></div></'+tag+'>'); |
| 46 | |
| 47 | jQuery('body').addClass('latepoint-lightbox-active'); |
| 48 | } |
| 49 | |
| 50 | // Styled confirmation lightbox — replaces the native confirm() for any data-os-action element that |
| 51 | // carries the "os-confirm-alert" class. It reuses the standard lightbox heading/content/footer markup so it |
| 52 | // matches the rest of the UI. Title/button labels come from data attributes (server-translated); the |
| 53 | // body is the element's existing data-os-prompt. On confirm it re-fires the trigger's own |
| 54 | // data-os-action flow (mirrors the os-delete-confirm gate, without the type-to-confirm step). |
| 55 | function latepoint_confirm_show($trigger){ |
| 56 | var title = $trigger.data('os-confirm-title') || ''; |
| 57 | var body = $trigger.data('os-prompt') || ''; |
| 58 | var confirm_label = $trigger.data('os-confirm-button') || 'Confirm'; |
| 59 | |
| 60 | // Build via jQuery DOM methods so every supplied string is assigned with .text()/.attr() (auto-escaped). |
| 61 | var $wrap = jQuery('<div class="os-confirm-modal" />'); |
| 62 | if(title){ |
| 63 | $wrap.append(jQuery('<div class="latepoint-lightbox-heading" />').append(jQuery('<h2 />').text(title))); |
| 64 | } |
| 65 | var $content = jQuery('<div class="latepoint-lightbox-content" />'); |
| 66 | if(body){ |
| 67 | $content.append(jQuery('<p class="os-confirm-body" />').text(body)); |
| 68 | } |
| 69 | $wrap.append($content); |
| 70 | $wrap.append( |
| 71 | jQuery('<div class="latepoint-lightbox-footer" />').append( |
| 72 | jQuery('<a href="#" class="latepoint-btn latepoint-btn-danger latepoint-btn-block os-confirm-go" />').append(jQuery('<span />').text(confirm_label)) |
| 73 | ) |
| 74 | ); |
| 75 | |
| 76 | latepoint_show_data_in_lightbox($wrap[0].outerHTML, 'width-450 os-confirm-lightbox', true); |
| 77 | jQuery('.os-confirm-lightbox .os-confirm-modal').data('onConfirm', function(){ |
| 78 | $trigger.data('os-confirm-alert-approved', true).trigger('click'); |
| 79 | }); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | |
| 84 | // DOCUMENT READY |
| 85 | jQuery(function( $ ) { |
| 86 | |
| 87 | if($('.latepoint').find('[data-os-action-onload]').length){ |
| 88 | $('.latepoint').find('[data-os-action-onload]').each(function(){ |
| 89 | var $this = jQuery(this); |
| 90 | $this.addClass('os-loading'); |
| 91 | var params = $this.data('os-params'); |
| 92 | var return_format = $this.data('os-return-format') ? $this.data('os-return-format') : 'json' |
| 93 | var data = { action: 'latepoint_route_call', route_name: $this.data('os-action-onload'), params: params, return_format: return_format } |
| 94 | jQuery.ajax({ |
| 95 | type : "post", |
| 96 | dataType : "json", |
| 97 | url : latepoint_timestamped_ajaxurl(), |
| 98 | data : data, |
| 99 | success: function(response) { |
| 100 | $this.removeClass('os-loading'); |
| 101 | if (response.status === "success") { |
| 102 | if($this.data('os-output-target') == 'self'){ |
| 103 | $this.html(response.message); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | }); |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | jQuery('body.latepoint').on('change', 'select[data-os-on-change]', function(e){ |
| 112 | let $this = jQuery(this); |
| 113 | |
| 114 | let func_name = $this.data('os-on-change'); |
| 115 | if(func_name.includes('.')){ |
| 116 | let func_arr = func_name.split('.'); |
| 117 | if(typeof window[func_arr[0]][func_arr[1]] !== 'function'){ |
| 118 | console.log(func_name + ' is undefined'); |
| 119 | }else{ |
| 120 | window[func_arr[0]][func_arr[1]]($this); |
| 121 | } |
| 122 | }else{ |
| 123 | if(typeof window[func_name] !== 'function'){ |
| 124 | console.log(func_name + ' is undefined'); |
| 125 | }else{ |
| 126 | window[func_name]($this); |
| 127 | } |
| 128 | } |
| 129 | }); |
| 130 | |
| 131 | // os-confirm modal: on confirm, close the lightbox and run the trigger's own data-os-action flow. |
| 132 | jQuery('body').on('click', '.os-confirm-go', function(e){ |
| 133 | e.preventDefault(); |
| 134 | var onConfirm = jQuery(this).closest('.os-confirm-modal').data('onConfirm'); |
| 135 | jQuery('.latepoint-lightbox-w').remove(); |
| 136 | jQuery('body').removeClass('latepoint-lightbox-active'); |
| 137 | if(typeof onConfirm === 'function') onConfirm(); |
| 138 | }); |
| 139 | |
| 140 | /* |
| 141 | Ajax buttons action |
| 142 | */ |
| 143 | $('.latepoint').on('click', 'button[data-os-action], a[data-os-action], div[data-os-action], span[data-os-action], tr[data-os-action]', function(e){ |
| 144 | var $this = jQuery(this); |
| 145 | if($this.hasClass('os-delete-confirm') && typeof latepoint_delete_confirm_show === 'function'){ |
| 146 | if(!$this.data('os-delete-approved')){ |
| 147 | latepoint_delete_confirm_show($this); |
| 148 | return false; |
| 149 | } |
| 150 | }else if($this.hasClass('os-confirm-alert')){ |
| 151 | if(!$this.data('os-confirm-alert-approved')){ |
| 152 | latepoint_confirm_show($this); |
| 153 | return false; |
| 154 | } |
| 155 | }else{ |
| 156 | if($this.data('os-prompt') && !confirm($this.data('os-prompt'))) return false; |
| 157 | } |
| 158 | $this.removeData('os-delete-approved').removeData('os-confirm-alert-approved'); |
| 159 | var params = $this.data('os-params'); |
| 160 | if($this.data('os-source-of-params')){ |
| 161 | var form_data = latepoint_create_form_data_from_non_form_element($($this.data('os-source-of-params'))); |
| 162 | params = latepoint_formdata_to_url_encoded_string(form_data); |
| 163 | } |
| 164 | var return_format = $this.data('os-return-format') ? $this.data('os-return-format') : 'json' |
| 165 | var data = { action: 'latepoint_route_call', route_name: $this.data('os-action'), params: params, return_format: return_format } |
| 166 | $this.addClass('os-loading'); |
| 167 | if($this.data('os-output-target') == 'side-panel'){ |
| 168 | $('.latepoint-side-panel-w').remove(); |
| 169 | let css_classes = $this.data('os-lightbox-classes') ? $this.data('os-lightbox-classes') : ''; |
| 170 | $('body').append('<div class="latepoint-side-panel-w ' + css_classes + ' os-loading"><div class="latepoint-side-panel-shadow"></div><div class="latepoint-side-panels"><div class="latepoint-side-panel-i"></div></div></div>'); |
| 171 | }else if($this.data('os-output-target') == 'full-panel'){ |
| 172 | $('.latepoint-full-panel-w').remove(); |
| 173 | $('body').append('<div class="latepoint-full-panel-w os-loading"></div>'); |
| 174 | } |
| 175 | $.ajax({ |
| 176 | type : "post", |
| 177 | dataType : "json", |
| 178 | url : latepoint_timestamped_ajaxurl(), |
| 179 | data : data, |
| 180 | success: function(response){ |
| 181 | if(response.status === "success"){ |
| 182 | if($this.data('os-output-target') == 'lightbox'){ |
| 183 | latepoint_show_data_in_lightbox(response.message, $this.data('os-lightbox-classes'), ($this.data('os-lightbox-no-close-button') !== 'yes'), $this.data('os-lightbox-tag'), $this.data('os-lightbox-inner-classes'), $this.data('os-lightbox-inner-tag')); |
| 184 | }else if($this.data('os-output-target') == 'side-panel'){ |
| 185 | $('.latepoint-side-panel-i').html(response.message); |
| 186 | jQuery('.latepoint-side-panel-i').find('.os-form-header .latepoint-side-panel-close').remove(); |
| 187 | jQuery('.latepoint-side-panel-i').find('.os-form-header').append('<a href="#" class="latepoint-side-panel-close latepoint-side-panel-close-trigger"><i class="latepoint-icon latepoint-icon-x"></i></a>'); |
| 188 | setTimeout(function(){ |
| 189 | $('.latepoint-side-panel-w').removeClass('os-loading'); |
| 190 | }, 100); |
| 191 | }else if($this.data('os-output-target') == 'full-panel'){ |
| 192 | $('.latepoint-full-panel-w').html(response.message); |
| 193 | setTimeout(function(){ |
| 194 | $('.latepoint-full-panel-w').removeClass('os-loading'); |
| 195 | }, 100); |
| 196 | }else if($this.data('os-success-action') == 'reload'){ |
| 197 | latepoint_add_notification(response.message); |
| 198 | location.reload(); |
| 199 | return; |
| 200 | }else if($this.data('os-success-action') == 'redirect'){ |
| 201 | if($this.data('os-redirect-to')){ |
| 202 | latepoint_add_notification(response.message); |
| 203 | window.location.replace($this.data('os-redirect-to')); |
| 204 | }else{ |
| 205 | window.location.replace(response.message); |
| 206 | } |
| 207 | return; |
| 208 | }else if($this.data('os-output-target') && $($this.data('os-output-target')).length){ |
| 209 | if($this.data('os-output-target-do') == 'append') { |
| 210 | $($this.data('os-output-target')).append(response.message); |
| 211 | }else if($this.data('os-output-target-do') == 'prepend'){ |
| 212 | $($this.data('os-output-target')).prepend(response.message); |
| 213 | }else{ |
| 214 | $($this.data('os-output-target')).html(response.message); |
| 215 | } |
| 216 | }else{ |
| 217 | switch($this.data('os-before-after')){ |
| 218 | case 'before': |
| 219 | $this.before(response.message); |
| 220 | break; |
| 221 | case 'after': |
| 222 | $this.after(response.message); |
| 223 | break; |
| 224 | case 'replace': |
| 225 | $this.replaceWith(response.message); |
| 226 | break; |
| 227 | case 'none': |
| 228 | break; |
| 229 | default: |
| 230 | latepoint_add_notification(response.message); |
| 231 | } |
| 232 | } |
| 233 | if($this.data('os-after-call')){ |
| 234 | var func_name = $this.data('os-after-call'); |
| 235 | var callback = false; |
| 236 | if(func_name.includes('.')){ |
| 237 | var func_arr = func_name.split('.'); |
| 238 | if(typeof window[func_arr[0]][func_arr[1]] !== 'function'){ |
| 239 | console.log(func_name + ' is undefined'); |
| 240 | } |
| 241 | if($this.data('os-pass-this') && $this.data('os-pass-response')){ |
| 242 | window[func_arr[0]][func_arr[1]]($this, response); |
| 243 | }else if($this.data('os-pass-this')){ |
| 244 | window[func_arr[0]][func_arr[1]]($this); |
| 245 | }else if($this.data('os-pass-response')){ |
| 246 | window[func_arr[0]][func_arr[1]](response); |
| 247 | }else{ |
| 248 | window[func_arr[0]][func_arr[1]](); |
| 249 | } |
| 250 | }else{ |
| 251 | if(typeof window[func_name] !== 'function'){ |
| 252 | console.log(func_name + ' is undefined'); |
| 253 | } |
| 254 | if($this.data('os-pass-this') && $this.data('os-pass-response')){ |
| 255 | window[func_name]($this, response); |
| 256 | }else if($this.data('os-pass-this')){ |
| 257 | window[func_name]($this); |
| 258 | }else if($this.data('os-pass-response')){ |
| 259 | window[func_name](response); |
| 260 | }else{ |
| 261 | window[func_name](); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | $this.removeClass('os-loading'); |
| 266 | }else{ |
| 267 | $this.removeClass('os-loading'); |
| 268 | if($this.data('os-output-target') && $($this.data('os-output-target')).length){ |
| 269 | $($this.data('os-output-target')).prepend(latepoint_generate_form_message_html(response.message, 'error')); |
| 270 | }else{ |
| 271 | alert(response.message); |
| 272 | } |
| 273 | if($this.data('os-after-call-error')){ |
| 274 | var func_name = $this.data('os-after-call-error'); |
| 275 | var callback = false; |
| 276 | if(func_name.includes('.')){ |
| 277 | var func_arr = func_name.split('.'); |
| 278 | if(typeof window[func_arr[0]][func_arr[1]] !== 'function'){ |
| 279 | console.log(func_name + ' is undefined'); |
| 280 | } |
| 281 | if($this.data('os-pass-this') && $this.data('os-pass-response')){ |
| 282 | window[func_arr[0]][func_arr[1]]($this, response); |
| 283 | }else if($this.data('os-pass-this')){ |
| 284 | window[func_arr[0]][func_arr[1]]($this); |
| 285 | }else if($this.data('os-pass-response')){ |
| 286 | window[func_arr[0]][func_arr[1]](response); |
| 287 | }else{ |
| 288 | window[func_arr[0]][func_arr[1]](); |
| 289 | } |
| 290 | }else{ |
| 291 | if(typeof window[func_name] !== 'function'){ |
| 292 | console.log(func_name + ' is undefined'); |
| 293 | } |
| 294 | if($this.data('os-pass-this') && $this.data('os-pass-response')){ |
| 295 | window[func_name]($this, response); |
| 296 | }else if($this.data('os-pass-this')){ |
| 297 | window[func_name]($this); |
| 298 | }else if($this.data('os-pass-response')){ |
| 299 | window[func_name](response); |
| 300 | }else{ |
| 301 | window[func_name](); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | }); |
| 308 | return false; |
| 309 | }); |
| 310 | |
| 311 | |
| 312 | $('.latepoint').on('click', 'form[data-os-action] button[type="submit"]', function(e){ |
| 313 | $(this).addClass('os-loading'); |
| 314 | }); |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | /* |
| 333 | Form ajax submit action |
| 334 | */ |
| 335 | $('.latepoint').on('submit', 'form[data-os-action]', function(e){ |
| 336 | e.preventDefault(); // prevent native submit |
| 337 | var $form = $(this); |
| 338 | var form_data = new FormData($form[0]); |
| 339 | |
| 340 | if (('lp_intlTelInputGlobals' in window) && ('lp_intlTelInputUtils' in window)) { |
| 341 | // Get e164 formatted number from phone fields when form is submitted |
| 342 | $form.find('input.os-mask-phone').each(function () { |
| 343 | let telInstance = window.lp_intlTelInputGlobals.getInstance(this); |
| 344 | if(telInstance){ |
| 345 | const phoneInputName = this.getAttribute('name'); |
| 346 | const phoneInputValue = window.lp_intlTelInputGlobals.getInstance(this).getNumber(window.lp_intlTelInputUtils.numberFormat.E164); |
| 347 | form_data.set(phoneInputName, phoneInputValue); |
| 348 | } |
| 349 | }); |
| 350 | } |
| 351 | |
| 352 | let data = latepoint_create_form_data($form, $(this).data('os-action')); |
| 353 | |
| 354 | // var data = { action: 'latepoint_route_call', route_name: $(this).data('os-action'), params: latepoint_formdata_to_url_encoded_string(form_data), return_format: 'json' } |
| 355 | $form.find('button[type="submit"]').addClass('os-loading'); |
| 356 | $.ajax({ |
| 357 | type : "post", |
| 358 | dataType : "json", |
| 359 | processData: false, |
| 360 | contentType: false, |
| 361 | url : latepoint_timestamped_ajaxurl(), |
| 362 | data : data, |
| 363 | success: function(response){ |
| 364 | $form.find('button[type="submit"].os-loading').removeClass('os-loading'); |
| 365 | latepoint_clear_form_messages($form); |
| 366 | if(response.status === "success"){ |
| 367 | if($form.data('os-success-action') == 'reload'){ |
| 368 | latepoint_add_notification(response.message); |
| 369 | location.reload(); |
| 370 | return; |
| 371 | }else if($form.data('os-success-action') == 'redirect'){ |
| 372 | if($form.data('os-redirect-to')){ |
| 373 | latepoint_add_notification(response.message); |
| 374 | window.location.replace($form.data('os-redirect-to')); |
| 375 | }else{ |
| 376 | window.location.replace(response.message); |
| 377 | } |
| 378 | return; |
| 379 | }else if($form.data('os-output-target') && $($form.data('os-output-target')).length){ |
| 380 | $($form.data('os-output-target')).html(response.message); |
| 381 | }else{ |
| 382 | if(response.message == 'redirect'){ |
| 383 | window.location.replace(response.url); |
| 384 | }else{ |
| 385 | latepoint_add_notification(response.message); |
| 386 | } |
| 387 | } |
| 388 | if($form.data('os-record-id-holder') && response.record_id){ |
| 389 | $form.find('[name="' + $form.data('os-record-id-holder') + '"]').val(response.record_id) |
| 390 | } |
| 391 | if($form.data('os-after-call')){ |
| 392 | |
| 393 | var func_name = $form.data('os-after-call'); |
| 394 | var callback = false; |
| 395 | if(func_name.includes('.')){ |
| 396 | var func_arr = func_name.split('.'); |
| 397 | if(typeof window[func_arr[0]][func_arr[1]] !== 'function'){ |
| 398 | console.log(func_name + ' is undefined'); |
| 399 | } |
| 400 | if($form.data('os-pass-this') && $form.data('os-pass-response')){ |
| 401 | window[func_arr[0]][func_arr[1]]($form, response); |
| 402 | }else if($form.data('os-pass-this')){ |
| 403 | window[func_arr[0]][func_arr[1]]($form); |
| 404 | }else if($form.data('os-pass-response')){ |
| 405 | window[func_arr[0]][func_arr[1]](response); |
| 406 | }else{ |
| 407 | window[func_arr[0]][func_arr[1]](); |
| 408 | } |
| 409 | }else{ |
| 410 | if(typeof window[func_name] !== 'function'){ |
| 411 | console.log(func_name + ' is undefined'); |
| 412 | } |
| 413 | if($form.data('os-pass-this') && $form.data('os-pass-response')){ |
| 414 | window[func_name]($form, response); |
| 415 | }else if($form.data('os-pass-this')){ |
| 416 | window[func_name]($form); |
| 417 | }else if($form.data('os-pass-response')){ |
| 418 | window[func_name](response); |
| 419 | }else{ |
| 420 | window[func_name](); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | $('button.os-loading').removeClass('os-loading'); |
| 425 | }else{ |
| 426 | $('button.os-loading').removeClass('os-loading'); |
| 427 | if($form.data('os-show-errors-as-notification')){ |
| 428 | latepoint_add_notification(response.message, 'error'); |
| 429 | }else{ |
| 430 | latepoint_add_notification(response.message, 'error'); |
| 431 | $([document.documentElement, document.body]).animate({ |
| 432 | scrollTop: ($form.find(".os-form-message-w").offset().top - 30) |
| 433 | }, 200); |
| 434 | } |
| 435 | } |
| 436 | if(response.form_values_to_update){ |
| 437 | $.each(response.form_values_to_update, function(name, value){ |
| 438 | $form.find('[name="'+ name +'"]').val(value); |
| 439 | }); |
| 440 | } |
| 441 | } |
| 442 | }); |
| 443 | return false; |
| 444 | }); |
| 445 | }); |