admin
1 week ago
front
1 week ago
actions.js
3 weeks 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
399 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 | |
| 51 | |
| 52 | // DOCUMENT READY |
| 53 | jQuery(function( $ ) { |
| 54 | |
| 55 | if($('.latepoint').find('[data-os-action-onload]').length){ |
| 56 | $('.latepoint').find('[data-os-action-onload]').each(function(){ |
| 57 | var $this = jQuery(this); |
| 58 | $this.addClass('os-loading'); |
| 59 | var params = $this.data('os-params'); |
| 60 | var return_format = $this.data('os-return-format') ? $this.data('os-return-format') : 'json' |
| 61 | var data = { action: 'latepoint_route_call', route_name: $this.data('os-action-onload'), params: params, return_format: return_format } |
| 62 | jQuery.ajax({ |
| 63 | type : "post", |
| 64 | dataType : "json", |
| 65 | url : latepoint_timestamped_ajaxurl(), |
| 66 | data : data, |
| 67 | success: function(response) { |
| 68 | $this.removeClass('os-loading'); |
| 69 | if (response.status === "success") { |
| 70 | if($this.data('os-output-target') == 'self'){ |
| 71 | $this.html(response.message); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | }); |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | jQuery('body.latepoint').on('change', 'select[data-os-on-change]', function(e){ |
| 80 | let $this = jQuery(this); |
| 81 | |
| 82 | let func_name = $this.data('os-on-change'); |
| 83 | if(func_name.includes('.')){ |
| 84 | let func_arr = func_name.split('.'); |
| 85 | if(typeof window[func_arr[0]][func_arr[1]] !== 'function'){ |
| 86 | console.log(func_name + ' is undefined'); |
| 87 | }else{ |
| 88 | window[func_arr[0]][func_arr[1]]($this); |
| 89 | } |
| 90 | }else{ |
| 91 | if(typeof window[func_name] !== 'function'){ |
| 92 | console.log(func_name + ' is undefined'); |
| 93 | }else{ |
| 94 | window[func_name]($this); |
| 95 | } |
| 96 | } |
| 97 | }); |
| 98 | |
| 99 | /* |
| 100 | Ajax buttons action |
| 101 | */ |
| 102 | $('.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){ |
| 103 | var $this = jQuery(this); |
| 104 | if($this.hasClass('os-delete-confirm') && typeof latepoint_delete_confirm_show === 'function'){ |
| 105 | if(!$this.data('os-delete-approved')){ |
| 106 | latepoint_delete_confirm_show($this); |
| 107 | return false; |
| 108 | } |
| 109 | }else{ |
| 110 | if($this.data('os-prompt') && !confirm($this.data('os-prompt'))) return false; |
| 111 | } |
| 112 | $this.removeData('os-delete-approved'); |
| 113 | var params = $this.data('os-params'); |
| 114 | if($this.data('os-source-of-params')){ |
| 115 | var form_data = latepoint_create_form_data_from_non_form_element($($this.data('os-source-of-params'))); |
| 116 | params = latepoint_formdata_to_url_encoded_string(form_data); |
| 117 | } |
| 118 | var return_format = $this.data('os-return-format') ? $this.data('os-return-format') : 'json' |
| 119 | var data = { action: 'latepoint_route_call', route_name: $this.data('os-action'), params: params, return_format: return_format } |
| 120 | $this.addClass('os-loading'); |
| 121 | if($this.data('os-output-target') == 'side-panel'){ |
| 122 | $('.latepoint-side-panel-w').remove(); |
| 123 | let css_classes = $this.data('os-lightbox-classes') ? $this.data('os-lightbox-classes') : ''; |
| 124 | $('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>'); |
| 125 | }else if($this.data('os-output-target') == 'full-panel'){ |
| 126 | $('.latepoint-full-panel-w').remove(); |
| 127 | $('body').append('<div class="latepoint-full-panel-w os-loading"></div>'); |
| 128 | } |
| 129 | $.ajax({ |
| 130 | type : "post", |
| 131 | dataType : "json", |
| 132 | url : latepoint_timestamped_ajaxurl(), |
| 133 | data : data, |
| 134 | success: function(response){ |
| 135 | if(response.status === "success"){ |
| 136 | if($this.data('os-output-target') == 'lightbox'){ |
| 137 | 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')); |
| 138 | }else if($this.data('os-output-target') == 'side-panel'){ |
| 139 | $('.latepoint-side-panel-i').html(response.message); |
| 140 | jQuery('.latepoint-side-panel-i').find('.os-form-header .latepoint-side-panel-close').remove(); |
| 141 | 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>'); |
| 142 | setTimeout(function(){ |
| 143 | $('.latepoint-side-panel-w').removeClass('os-loading'); |
| 144 | }, 100); |
| 145 | }else if($this.data('os-output-target') == 'full-panel'){ |
| 146 | $('.latepoint-full-panel-w').html(response.message); |
| 147 | setTimeout(function(){ |
| 148 | $('.latepoint-full-panel-w').removeClass('os-loading'); |
| 149 | }, 100); |
| 150 | }else if($this.data('os-success-action') == 'reload'){ |
| 151 | latepoint_add_notification(response.message); |
| 152 | location.reload(); |
| 153 | return; |
| 154 | }else if($this.data('os-success-action') == 'redirect'){ |
| 155 | if($this.data('os-redirect-to')){ |
| 156 | latepoint_add_notification(response.message); |
| 157 | window.location.replace($this.data('os-redirect-to')); |
| 158 | }else{ |
| 159 | window.location.replace(response.message); |
| 160 | } |
| 161 | return; |
| 162 | }else if($this.data('os-output-target') && $($this.data('os-output-target')).length){ |
| 163 | if($this.data('os-output-target-do') == 'append') { |
| 164 | $($this.data('os-output-target')).append(response.message); |
| 165 | }else if($this.data('os-output-target-do') == 'prepend'){ |
| 166 | $($this.data('os-output-target')).prepend(response.message); |
| 167 | }else{ |
| 168 | $($this.data('os-output-target')).html(response.message); |
| 169 | } |
| 170 | }else{ |
| 171 | switch($this.data('os-before-after')){ |
| 172 | case 'before': |
| 173 | $this.before(response.message); |
| 174 | break; |
| 175 | case 'after': |
| 176 | $this.after(response.message); |
| 177 | break; |
| 178 | case 'replace': |
| 179 | $this.replaceWith(response.message); |
| 180 | break; |
| 181 | case 'none': |
| 182 | break; |
| 183 | default: |
| 184 | latepoint_add_notification(response.message); |
| 185 | } |
| 186 | } |
| 187 | if($this.data('os-after-call')){ |
| 188 | var func_name = $this.data('os-after-call'); |
| 189 | var callback = false; |
| 190 | if(func_name.includes('.')){ |
| 191 | var func_arr = func_name.split('.'); |
| 192 | if(typeof window[func_arr[0]][func_arr[1]] !== 'function'){ |
| 193 | console.log(func_name + ' is undefined'); |
| 194 | } |
| 195 | if($this.data('os-pass-this') && $this.data('os-pass-response')){ |
| 196 | window[func_arr[0]][func_arr[1]]($this, response); |
| 197 | }else if($this.data('os-pass-this')){ |
| 198 | window[func_arr[0]][func_arr[1]]($this); |
| 199 | }else if($this.data('os-pass-response')){ |
| 200 | window[func_arr[0]][func_arr[1]](response); |
| 201 | }else{ |
| 202 | window[func_arr[0]][func_arr[1]](); |
| 203 | } |
| 204 | }else{ |
| 205 | if(typeof window[func_name] !== 'function'){ |
| 206 | console.log(func_name + ' is undefined'); |
| 207 | } |
| 208 | if($this.data('os-pass-this') && $this.data('os-pass-response')){ |
| 209 | window[func_name]($this, response); |
| 210 | }else if($this.data('os-pass-this')){ |
| 211 | window[func_name]($this); |
| 212 | }else if($this.data('os-pass-response')){ |
| 213 | window[func_name](response); |
| 214 | }else{ |
| 215 | window[func_name](); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | $this.removeClass('os-loading'); |
| 220 | }else{ |
| 221 | $this.removeClass('os-loading'); |
| 222 | if($this.data('os-output-target') && $($this.data('os-output-target')).length){ |
| 223 | $($this.data('os-output-target')).prepend(latepoint_generate_form_message_html(response.message, 'error')); |
| 224 | }else{ |
| 225 | alert(response.message); |
| 226 | } |
| 227 | if($this.data('os-after-call-error')){ |
| 228 | var func_name = $this.data('os-after-call-error'); |
| 229 | var callback = false; |
| 230 | if(func_name.includes('.')){ |
| 231 | var func_arr = func_name.split('.'); |
| 232 | if(typeof window[func_arr[0]][func_arr[1]] !== 'function'){ |
| 233 | console.log(func_name + ' is undefined'); |
| 234 | } |
| 235 | if($this.data('os-pass-this') && $this.data('os-pass-response')){ |
| 236 | window[func_arr[0]][func_arr[1]]($this, response); |
| 237 | }else if($this.data('os-pass-this')){ |
| 238 | window[func_arr[0]][func_arr[1]]($this); |
| 239 | }else if($this.data('os-pass-response')){ |
| 240 | window[func_arr[0]][func_arr[1]](response); |
| 241 | }else{ |
| 242 | window[func_arr[0]][func_arr[1]](); |
| 243 | } |
| 244 | }else{ |
| 245 | if(typeof window[func_name] !== 'function'){ |
| 246 | console.log(func_name + ' is undefined'); |
| 247 | } |
| 248 | if($this.data('os-pass-this') && $this.data('os-pass-response')){ |
| 249 | window[func_name]($this, response); |
| 250 | }else if($this.data('os-pass-this')){ |
| 251 | window[func_name]($this); |
| 252 | }else if($this.data('os-pass-response')){ |
| 253 | window[func_name](response); |
| 254 | }else{ |
| 255 | window[func_name](); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | }); |
| 262 | return false; |
| 263 | }); |
| 264 | |
| 265 | |
| 266 | $('.latepoint').on('click', 'form[data-os-action] button[type="submit"]', function(e){ |
| 267 | $(this).addClass('os-loading'); |
| 268 | }); |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | /* |
| 287 | Form ajax submit action |
| 288 | */ |
| 289 | $('.latepoint').on('submit', 'form[data-os-action]', function(e){ |
| 290 | e.preventDefault(); // prevent native submit |
| 291 | var $form = $(this); |
| 292 | var form_data = new FormData($form[0]); |
| 293 | |
| 294 | if (('lp_intlTelInputGlobals' in window) && ('lp_intlTelInputUtils' in window)) { |
| 295 | // Get e164 formatted number from phone fields when form is submitted |
| 296 | $form.find('input.os-mask-phone').each(function () { |
| 297 | let telInstance = window.lp_intlTelInputGlobals.getInstance(this); |
| 298 | if(telInstance){ |
| 299 | const phoneInputName = this.getAttribute('name'); |
| 300 | const phoneInputValue = window.lp_intlTelInputGlobals.getInstance(this).getNumber(window.lp_intlTelInputUtils.numberFormat.E164); |
| 301 | form_data.set(phoneInputName, phoneInputValue); |
| 302 | } |
| 303 | }); |
| 304 | } |
| 305 | |
| 306 | let data = latepoint_create_form_data($form, $(this).data('os-action')); |
| 307 | |
| 308 | // var data = { action: 'latepoint_route_call', route_name: $(this).data('os-action'), params: latepoint_formdata_to_url_encoded_string(form_data), return_format: 'json' } |
| 309 | $form.find('button[type="submit"]').addClass('os-loading'); |
| 310 | $.ajax({ |
| 311 | type : "post", |
| 312 | dataType : "json", |
| 313 | processData: false, |
| 314 | contentType: false, |
| 315 | url : latepoint_timestamped_ajaxurl(), |
| 316 | data : data, |
| 317 | success: function(response){ |
| 318 | $form.find('button[type="submit"].os-loading').removeClass('os-loading'); |
| 319 | latepoint_clear_form_messages($form); |
| 320 | if(response.status === "success"){ |
| 321 | if($form.data('os-success-action') == 'reload'){ |
| 322 | latepoint_add_notification(response.message); |
| 323 | location.reload(); |
| 324 | return; |
| 325 | }else if($form.data('os-success-action') == 'redirect'){ |
| 326 | if($form.data('os-redirect-to')){ |
| 327 | latepoint_add_notification(response.message); |
| 328 | window.location.replace($form.data('os-redirect-to')); |
| 329 | }else{ |
| 330 | window.location.replace(response.message); |
| 331 | } |
| 332 | return; |
| 333 | }else if($form.data('os-output-target') && $($form.data('os-output-target')).length){ |
| 334 | $($form.data('os-output-target')).html(response.message); |
| 335 | }else{ |
| 336 | if(response.message == 'redirect'){ |
| 337 | window.location.replace(response.url); |
| 338 | }else{ |
| 339 | latepoint_add_notification(response.message); |
| 340 | } |
| 341 | } |
| 342 | if($form.data('os-record-id-holder') && response.record_id){ |
| 343 | $form.find('[name="' + $form.data('os-record-id-holder') + '"]').val(response.record_id) |
| 344 | } |
| 345 | if($form.data('os-after-call')){ |
| 346 | |
| 347 | var func_name = $form.data('os-after-call'); |
| 348 | var callback = false; |
| 349 | if(func_name.includes('.')){ |
| 350 | var func_arr = func_name.split('.'); |
| 351 | if(typeof window[func_arr[0]][func_arr[1]] !== 'function'){ |
| 352 | console.log(func_name + ' is undefined'); |
| 353 | } |
| 354 | if($form.data('os-pass-this') && $form.data('os-pass-response')){ |
| 355 | window[func_arr[0]][func_arr[1]]($form, response); |
| 356 | }else if($form.data('os-pass-this')){ |
| 357 | window[func_arr[0]][func_arr[1]]($form); |
| 358 | }else if($form.data('os-pass-response')){ |
| 359 | window[func_arr[0]][func_arr[1]](response); |
| 360 | }else{ |
| 361 | window[func_arr[0]][func_arr[1]](); |
| 362 | } |
| 363 | }else{ |
| 364 | if(typeof window[func_name] !== 'function'){ |
| 365 | console.log(func_name + ' is undefined'); |
| 366 | } |
| 367 | if($form.data('os-pass-this') && $form.data('os-pass-response')){ |
| 368 | window[func_name]($form, response); |
| 369 | }else if($form.data('os-pass-this')){ |
| 370 | window[func_name]($form); |
| 371 | }else if($form.data('os-pass-response')){ |
| 372 | window[func_name](response); |
| 373 | }else{ |
| 374 | window[func_name](); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | $('button.os-loading').removeClass('os-loading'); |
| 379 | }else{ |
| 380 | $('button.os-loading').removeClass('os-loading'); |
| 381 | if($form.data('os-show-errors-as-notification')){ |
| 382 | latepoint_add_notification(response.message, 'error'); |
| 383 | }else{ |
| 384 | latepoint_add_notification(response.message, 'error'); |
| 385 | $([document.documentElement, document.body]).animate({ |
| 386 | scrollTop: ($form.find(".os-form-message-w").offset().top - 30) |
| 387 | }, 200); |
| 388 | } |
| 389 | } |
| 390 | if(response.form_values_to_update){ |
| 391 | $.each(response.form_values_to_update, function(name, value){ |
| 392 | $form.find('[name="'+ name +'"]').val(value); |
| 393 | }); |
| 394 | } |
| 395 | } |
| 396 | }); |
| 397 | return false; |
| 398 | }); |
| 399 | }); |