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