· 4 years ago · Apr 05, 2021, 03:30 PM
1// selectize for category and location selects
2$(function(){
3
4 // create 1st category select
5 category_select = createCategorySelect();
6 // remove d-none class
7 $('#category-chained .select-category[data-level="0"]').parent('div').removeClass('d-none');
8
9 // load options for 1st category select
10 category_select.load(function(callback) {
11 $.ajax({
12 url: $('#category-chained').data('apiurl'),
13 type: 'GET',
14 data: {
15 "id_category_parent": 1,
16 "sort": 'order',
17 },
18 success: function(results) {
19 callback(results.categories);
20 },
21 error: function() {
22 callback();
23 }
24 });
25 });
26
27 // advertisement location is enabled?
28 if ($('#location-chained').length ) {
29
30 // create 1st location select
31 location_select = createLocationSelect();
32 // remove d-none class
33 $('#location-chained .select-location[data-level="0"]').parent('div').removeClass('d-none');
34
35 // load options for 1st location select
36 location_select.load(function(callback) {
37 $.ajax({
38 url: $('#location-chained').data('apiurl'),
39 type: 'GET',
40 data: {
41 "id_location_parent": 1,
42 "sort": 'order',
43 },
44 success: function(results) {
45 callback(results.locations);
46 if (results.locations.length === 0)
47 $('#location-chained').closest('.form-group').remove();
48 },
49 error: function() {
50 callback();
51 }
52 });
53 });
54 }
55
56 // show custom fields
57 if ($('#category-selected').val().length > 0) {
58 $.ajax({
59 url: $('#category-chained').data('apiurl') + '/' + $('#category-selected').val(),
60 success: function(results) {
61 createCustomFieldsByCategory(results.category.customfields);
62 }
63 });
64 }
65 else {
66 $.ajax({
67 url: $('#category-chained').data('apiurl') + '/' + 1,
68 success: function(results) {
69 createCustomFieldsByCategory(results.category.customfields);
70 }
71 });
72 }
73});
74
75function createCategorySelect () {
76
77 // count how many category selects we have rendered
78 num_category_select = $('#category-chained .select-category[data-level]').length;
79
80 // clone category select from template
81 $('#select-category-template').clone().attr('id', '').insertBefore($('#select-category-template')).find('select').attr('data-level', num_category_select);
82
83 // initialize selectize on created category select
84 category_select = $('.select-category[data-level="'+ num_category_select +'"]').selectize({
85 valueField: 'id_category',
86 labelField: 'translate_name',
87 searchField: 'translate_name',
88 onChange: function (value) {
89
90 if (!value.length) return;
91
92 // get current category level
93 current_level = $('#category-chained .option[data-value="'+ value +'"]').closest('.selectize-control').prev().data('level');
94
95 // is allowed to post on selected category?
96 if ( current_level > 0 || (current_level == 0 && $('#category-chained').is('[data-isparent]')))
97 {
98 // update #category-selected input value
99 $('#category-selected').attr('value', value);
100
101 //get category price
102 $.ajax({
103 url: $('#category-chained').data('apiurl') + '/' + value,
104 success: function(results) {
105 if (decodeHtml(results.category.price) != $('#category-chained').data('price0')) {
106 price_txt = $('#paid-category .help-block').data('title').replace(/%s/g, results.category.name).replace(/%d/g, decodeHtml(results.category.price));
107 $('#paid-category').removeClass('d-none').find('.help-block span').text(price_txt);
108 }
109 else {
110 $('#paid-category').addClass('d-none');
111 }
112 // show custom fields for this category
113 createCustomFieldsByCategory(results.category.customfields);
114 }
115 });
116 }
117 else
118 {
119 // set empty value
120 $('#category-selected').attr('value', '');
121 $('#paid-category').addClass('d-none');
122 // show custom fields
123 $.ajax({
124 url: $('#category-chained').data('apiurl') + '/' + 1,
125 success: function(results) {
126 createCustomFieldsByCategory(results.category.customfields);
127 }
128 });
129 }
130
131 // get current category level
132 current_level = $('#category-chained .option[data-value="'+ value +'"]').closest('.selectize-control').prev().data('level');
133
134 destroyCategoryChildSelect(current_level);
135
136 // create category select
137 category_select = createCategorySelect();
138
139 // load options for category select
140 category_select.load(function (callback) {
141 $.ajax({
142 url: $('#category-chained').data('apiurl'),
143 data: {
144 "id_category_parent": value,
145 "sort": 'order',
146 },
147 type: 'GET',
148 success: function (results) {
149 if (results.categories.length > 0)
150 {
151 callback(results.categories);
152 $('#category-chained .select-category[data-level="' + (current_level + 1) + '"]').parent('div').removeClass('d-none');
153 }
154 else
155 {
156 destroyCategoryChildSelect(current_level);
157 }
158 },
159 error: function () {
160 callback();
161 }
162 });
163 });
164 }
165 });
166
167 // return selectize control
168 return category_select[0].selectize;
169}
170
171function createLocationSelect () {
172
173 // count how many location selects we have rendered
174 num_location_select = $('#location-chained .select-location[data-level]').length;
175
176 // clone location select from template
177 $('#select-location-template').clone().attr('id', '').insertBefore($('#select-location-template')).find('select').attr('data-level', num_location_select);
178
179 // initialize selectize on created location select
180 location_select = $('.select-location[data-level="'+ num_location_select +'"]').selectize({
181 valueField: 'id_location',
182 labelField: 'translate_name',
183 searchField: 'translate_name',
184 onChange: function (value) {
185
186 if (!value.length) return;
187
188 // update #location-selected input value
189 $('#location-selected').attr('value', value);
190
191 // get current location level
192 current_level = $('#location-chained .option[data-value="'+ value +'"]').closest('.selectize-control').prev().data('level');
193
194 destroyLocationChildSelect(current_level);
195
196 // create location select
197 location_select = createLocationSelect();
198
199 // load options for location select
200 location_select.load(function (callback) {
201 $.ajax({
202 url: $('#location-chained').data('apiurl'),
203 data: {
204 "id_location_parent": value,
205 "sort": 'order',
206 },
207 type: 'GET',
208 success: function (results) {
209 if (results.locations.length > 0)
210 {
211 callback(results.locations);
212 $('#location-chained .select-location[data-level="' + (current_level + 1) + '"]').parent('div').removeClass('d-none');
213 }
214 else
215 {
216 destroyLocationChildSelect(current_level);
217 }
218 },
219 error: function () {
220 callback();
221 }
222 });
223 });
224 }
225 });
226
227 // return selectize control
228 return location_select[0].selectize;
229}
230
231function destroyCategoryChildSelect (level) {
232 if (level === undefined) return;
233 $('#category-chained .select-category[data-level]').each(function () {
234 if ($(this).data('level') > level) {
235 $(this).parent('div').remove();
236 }
237 });
238}
239
240function destroyLocationChildSelect (level) {
241 if (level === undefined) return;
242 $('#location-chained .select-location[data-level]').each(function () {
243 if ($(this).data('level') > level) {
244 $(this).parent('div').remove();
245 }
246 });
247}
248
249$('#category-edit button').click(function(){
250 $('#category-chained').removeClass('d-none');
251 $('#category-edit').addClass('d-none');
252});
253
254$('#location-edit button').click(function(){
255 $('#location-chained').removeClass('d-none');
256 $('#location-edit').addClass('d-none');
257});
258
259// sceditor
260$('textarea[name=description]:not(.disable-bbcode)').sceditor({
261 format: 'bbcode',
262 plugins: "bbcode,plaintext",
263 toolbar: "bold,italic,underline,strike,|left,center,right,justify|" +
264 "bulletlist,orderedlist|link,unlink,youtube|source",
265 resizeEnabled: "true",
266 emoticonsEnabled: false,
267 autoUpdate: true,
268 width: '100%',
269 rtl: $('meta[name="application-name"]').data('rtl'),
270 style: $('meta[name="application-name"]').data('baseurl') + "themes/default/css/jquery.sceditor.default.min.css",
271});
272
273$('textarea[name=description]').prop('required',true);
274
275//sceditor for validation, updates iframe on submit
276$("button[name=submit]").click(function(){
277 $("textarea[name=description]").data("sceditor").updateOriginal();
278});
279
280function initLocationsGMap() {
281 jQuery.ajax({
282 url: ("https:" == document.location.protocol ? "https:" : "http:") + "//cdn.jsdelivr.net/gmaps/0.4.25/gmaps.min.js",
283 dataType: "script",
284 cache: true
285 }).done(function() {
286 locationsGMap();
287 });
288}
289
290function locationsGMap() {
291 // google map set marker on address
292 if ($('#map').length !== 0){
293 new GMaps({
294 div: '#map',
295 zoom: parseInt($('#map').attr('data-zoom')),
296 lat: $('#map').attr('data-lat'),
297 lng: $('#map').attr('data-lon')
298 });
299 var typingTimer; //timer identifier
300 var doneTypingInterval = 500; //time in ms, 5 second for example
301 //on keyup, start the countdown
302 $('#address').keyup(function () {
303 clearTimeout(typingTimer);
304 if ($(this).val()) {
305 typingTimer = setTimeout(doneTyping, doneTypingInterval);
306 }
307 });
308 //user is "finished typing," refresh map
309 function doneTyping () {
310 GMaps.geocode({
311 address: $('#address').val(),
312 callback: function (results, status) {
313 if (status == 'OK') {
314 var latlng = results[0].geometry.location;
315 map = new GMaps({
316 div: '#map',
317 lat: latlng.lat(),
318 lng: latlng.lng(),
319 });
320 map.setCenter(latlng.lat(), latlng.lng());
321 map.addMarker({
322 lat: latlng.lat(),
323 lng: latlng.lng(),
324 draggable: true,
325 dragend: function(event) {
326 var lat = event.latLng.lat();
327 var lng = event.latLng.lng();
328 GMaps.geocode({
329 lat: lat,
330 lng: lng,
331 callback: function(results, status) {
332 if (status == 'OK') {
333 $("input[name='address']").val(results[0].formatted_address)
334 }
335 }
336 });
337 $('#publish-latitude').val(lat).removeAttr("disabled");
338 $('#publish-longitude').val(lng).removeAttr("disabled");
339 },
340 });
341 $('#publish-latitude').val(latlng.lat()).removeAttr("disabled");
342 $('#publish-longitude').val(latlng.lng()).removeAttr("disabled");
343 }
344 }
345 });
346 }
347 }
348
349 // auto locate user
350 $('.locateme').click(function() {
351 var lat;
352 var lng;
353 GMaps.geolocate({
354 success: function(position) {
355 lat = position.coords.latitude;
356 lng = position.coords.longitude
357 map = new GMaps({
358 div: '#map',
359 lat: lat,
360 lng: lng,
361 });
362 map.setCenter(lat, lng);
363 map.addMarker({
364 lat: lat,
365 lng: lng,
366 });
367 $('#publish-latitude').val(lat).removeAttr("disabled");
368 $('#publish-longitude').val(lng).removeAttr("disabled");
369 GMaps.geocode({
370 lat: lat,
371 lng: lng,
372 callback: function(results, status) {
373 if (status == 'OK') {
374 $("input[name='address']").val(results[0].formatted_address)
375 }
376 }
377 });
378 },
379 error: function(error) {
380 alert('Geolocation failed: '+error.message);
381 },
382 not_supported: function() {
383 alert("Your browser does not support geolocation");
384 },
385 });
386 });
387}
388
389// Dropzone
390
391Dropzone.options.imagesDropzone = {
392 url: $('#publish-new').attr('action'),
393 timeout: 180000,
394 autoProcessQueue: false,
395 uploadMultiple: true,
396 acceptedFiles: 'image/*',
397 addRemoveLinks: true,
398 resizeMimeType: 'image/jpeg',
399 createImageThumbnails: true,
400 maxFilesize: $('.images').data('max-image-size'),
401 maxFiles: $('.images').data('max-files'),
402 parallelUploads: $('.images').data('max-files'),
403 parallelUploads: $('.images').data('max-files'),
404 resizeWidth: getResizeValue($('.images').data('image-width')),
405
406 init: function () {
407 dzClosure = this;
408
409 document.getElementById("publish-new-btn").addEventListener("click", function (e) {
410 if (dzClosure.getQueuedFiles().length > 0) {
411 e.preventDefault();
412 e.stopPropagation();
413 //Update the original textarea before validating
414 if ($('textarea[name=description]:not(.disable-bbcode)').length) {
415 $('textarea[name=description]:not(.disable-bbcode)').sceditor('instance').updateOriginal();
416 }
417
418 if ($('#publish-new').valid()) {
419 $('#processing-modal').on('shown.bs.modal', function () {
420 dzClosure.options.maxFiles++;
421 dzClosure.options.parallelUploads++;
422
423 // Get the queued files
424 var files = dzClosure.getQueuedFiles();
425
426 // Sort theme based on the DOM element index
427 files.sort(function (a, b) {
428 return ($(a.previewElement).index() > $(b.previewElement).index()) ? 1 : -1;
429 })
430
431 // Clear the dropzone queue
432 dzClosure.removeAllFiles();
433
434 // Add the reordered files to the queue
435 dzClosure.handleFiles(files);
436 dzClosure.processQueue();
437 });
438
439 if ($('#publish-new').find('.g-recaptcha').length) {
440 var response = grecaptcha.getResponse();
441 if (!response) {
442 $('#publish-new').attr('data-submit-please', 'true');
443 grecaptcha.execute();
444 } else {
445 $('#publish-new').find('input[name="g-recaptcha-response"]').val(response);
446 }
447 } else {
448 $('#processing-modal').modal('show');
449 }
450 }
451 }
452 });
453
454 this.on("sendingmultiple", function (file, xhr, formData) {
455 var data = $('#publish-new').serializeArray();
456 $.each(data, function (key, el) {
457 formData.append(el.name, el.value);
458 });
459 formData.append('ajax', true);
460 });
461
462 this.on("thumbnail", function (file, dataUrl) {
463 window.loadImage.parseMetaData(file, function (data) {
464 if (data.exif) {
465 var rotation = 1;
466 var rotate = {
467 1: 'rotate(0deg)',
468 2: 'rotate(0deg)',
469 3: 'rotate(180deg)',
470 4: 'rotate(0deg)',
471 5: 'rotate(0deg)',
472 6: 'rotate(90deg)',
473 7: 'rotate(0deg)',
474 8: 'rotate(270deg)'
475 };
476 rotation = data.exif.get('Orientation');
477
478 $(file.previewElement).find('img').css('transform', rotate[rotation]);
479 // Safari fix
480 $(file.previewElement).find('img').css('-webkit-transform', rotate[rotation]);
481 }
482 });
483 });
484
485 this.on("error", function (file, response) {
486 if (response.redirect_url) {
487 window.location = response.redirect_url;
488 }
489 });
490 },
491
492 successmultiple: function (file, response) {
493 //console.log(response);
494 window.location = response.redirect_url;
495 }
496}
497
498$("#images-dropzone").sortable({
499 items: '.dz-preview',
500 cursor: 'move',
501 opacity: 0.5,
502 containment: "parent",
503 distance: 20,
504 tolerance: 'pointer'
505});
506
507// VALIDATION with chosen fix
508$(function(){
509 $.validator.addMethod(
510 "regex",
511 function(value, element, regexp) {
512 var re = new RegExp(regexp);
513 return this.optional(element) || re.test(value);
514 }
515 );
516
517 $.validator.addMethod(
518 "required_checkbox_group",
519 function(value, element, idx) {
520 return $('#' + idx + ' :checkbox:checked').length > 0;
521 }
522 );
523
524 var $params = {
525 rules:{},
526 messages:{},
527 focusInvalid: false,
528 onkeyup: false,
529 ignore: 'input[type="text"]:hidden',
530 errorClass: 'invalid-feedback',
531 errorElement: 'div',
532 errorPlacement: function(error, element) {
533 element.removeClass('invalid-feedback');
534
535 if(element.is(':radio') || element.is(':checkbox')){
536 error.insertBefore(element.closest('label'));
537 } else if (element.is('textarea')) {
538 error.insertAfter(element.closest('textarea'));
539 } else if (element.is('select')) {
540 error.insertAfter(element.closest('select'));
541 } else if (element.is(':hidden')) {
542 checkbox_group = element.parent('.form-check').parent('div[data-type="checkbox_group"]');
543
544 if (checkbox_group && checkbox_group.find('.invalid-feedback').length === 0) {
545 checkbox_group.append(error);
546 }
547 } else {
548 error.insertAfter(element.closest('input'));
549 }
550 },
551 submitHandler: function(form) {
552 $('#processing-modal').on('shown.bs.modal', function() {
553 //Update the original textarea before validating
554 if ($('textarea[name=description]:not(.disable-bbcode)').length) {
555 $('textarea[name=description]:not(.disable-bbcode)').sceditor('instance').updateOriginal();
556 }
557 form.submit()
558 });
559
560 if ($(form).find('.g-recaptcha').length) {
561 var response = grecaptcha.getResponse();
562 if (!response) {
563 $(form).attr('data-submit-please', 'true');
564 grecaptcha.execute();
565 } else {
566 $(form).find('input[name="g-recaptcha-response"]').val(response);
567 }
568 } else {
569 $('#processing-modal').modal('show');
570 }
571 },
572 invalidHandler: function(form, validator) {
573 if (!validator.numberOfInvalids())
574 return;
575 $('html, body').animate({
576 scrollTop: $(validator.errorList[0].element).offset().top
577 }, 500);
578 }
579 };
580 $params['rules']['price'] = {regex: "^[0-9]{1,18}([,.]{1}[0-9]{1,8})?$"};
581 $params['rules']['title'] = {maxlength: 145};
582 $params['rules']['address'] = {maxlength: 145};
583 $params['rules']['phone'] = {maxlength: 30};
584 $params['rules']['website'] = {maxlength: 200};
585 $params['rules']['captcha'] = {
586 "remote" :
587 {
588 url: $(".post_new").attr('action'),
589 type: "post",
590 data:
591 {
592 ajaxValidateCaptcha: true
593 }
594 }
595 };
596 $params['rules']['hidden-recaptcha'] = {
597 required: function () {
598 if (grecaptcha.getResponse() == '') {
599 return true;
600 } else {
601 return false;
602 }
603 }
604 };
605 $params['rules']['email'] = {emaildomain: $('.post_new :input[name="email"]').data('domain')};
606 $params['rules']['description'] = {nobannedwords: $('.post_new :input[name="description"]').data('bannedwords')};
607 $params['messages']['email'] = {"emaildomain" : $('.post_new :input[name="email"]').data('error')};
608 $params['messages']['description'] = {"nobannedwords" : $('.post_new :input[name="description"]').data('error')};
609 $params['messages']['price'] = {"regex" : $('.post_new :input[name="price"]').data('error')};
610 $params['messages']['captcha'] = {"remote" : $('.post_new :input[name="captcha"]').data('error')};
611
612 $.validator.setDefaults({ ignore: ":hidden:not(select, .hidden-recaptcha)" });
613 var $form = $(".post_new");
614 $form.validate($params
615 // {
616 // errorLabelContainer: $(".post_new div.error"),
617 // wrapper: 'div',
618 // rules: {
619 // title: {minlength:2},
620 // price: {regex:"^[0-9]{1,18}([,.]{1}[0-9]{1,3})?$"}
621 // },
622 // messages: {
623 // price:{regex: "Format is incorect"}
624 // }
625 // }
626 );
627
628 //chosen fix
629 var settings = $.data($form[0], 'validator').settings;
630 settings.ignore += ':not(.cf_select_fields)'; // post_new location(any chosen) texarea
631 // settings.ignore += ':not(.sceditor-container)'; // post_new description texarea
632 settings.ignore += ':not(#description)'; // post_new description texarea
633});
634
635// sure you want to leave alert and processing modal
636$(function(){
637 if ($('input[name=leave_alert]').length === 0 && typeof ouibounce == 'function') {
638 var _ouibounce = ouibounce(false, {
639 aggressive: true,
640 callback: function() {
641 swal({
642 title: $('#publish-new-btn').data('swaltitle'),
643 text: $('#publish-new-btn').data('swaltext'),
644 type: "warning",
645 allowOutsideClick: true
646 });
647 }
648 });
649 }
650});
651
652function createCustomFieldsByCategory (customfields) {
653 $('#custom-fields > div').not("#custom-field-template").remove();
654 $.each(customfields, function (idx, customfield) {
655 // don't create admin privilege custom fields
656 if (customfield.admin_privilege)
657 return;
658 // clone custom field from template
659 var $template = $('#custom-field-template').clone().attr('id', '').removeClass('d-none').appendTo('#custom-fields');
660 if (customfield.required)
661 $template.addClass('required');
662 $template.find('div[data-label]').replaceWith($('<label/>').attr({'for' : idx}).html(customfield.translated_label));
663
664 switch (customfield.type) {
665 case 'string':
666 case 'url':
667 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'text',
668 'id' : idx,
669 'name' : idx,
670 'class' : 'form-control',
671 'placeholder' : customfield.translated_label,
672 'data-type' : customfield.type,
673 'data-toggle' : 'tooltip',
674 'title' : customfield.translated_tooltip,
675 'required' : customfield.required,
676 'value' : $('#custom-fields').data('customfield-values')[idx],
677 }));
678 break;
679 case 'textarea':
680 $template.find('div[data-input]').replaceWith($('<textarea/>').attr({ 'id' : idx,
681 'name' : idx,
682 'class' : 'form-control',
683 'placeholder' : customfield.translated_label,
684 'rows' : 10,
685 'cols' : 50,
686 'data-type' : customfield.type,
687 'data-toggle' : 'tooltip',
688 'title' : customfield.translated_tooltip,
689 'required' : customfield.required,
690 }).append($('#custom-fields').data('customfield-values')[idx]));
691 break;
692 case 'textarea_bbcode':
693 $template.find('div[data-input]').replaceWith($('<textarea/>').attr({ 'id' : idx,
694 'name' : idx,
695 'class' : 'form-control',
696 'placeholder' : customfield.translated_label,
697 'rows' : 10,
698 'cols' : 50,
699 'data-type' : customfield.type,
700 'data-toggle' : 'tooltip',
701 'title' : customfield.translated_tooltip,
702 'required' : customfield.required,
703 }).append($('#custom-fields').data('customfield-values')[idx]));
704 $('#custom-fields textarea[name="' + idx + '"]').sceditor({
705 format: 'bbcode',
706 plugins: "bbcode,plaintext",
707 toolbar: "bold,italic,underline,strike,|left,center,right,justify|" +
708 "bulletlist,orderedlist|link,unlink,youtube|source",
709 resizeEnabled: "true",
710 emoticonsEnabled: false,
711 autoUpdate: true,
712 width: '100%',
713 rtl: $('meta[name="application-name"]').data('rtl'),
714 style: $('meta[name="application-name"]').data('baseurl') + "themes/default/css/jquery.sceditor.default.min.css",
715 });
716 break;
717 case 'integer':
718 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'text',
719 'id' : idx,
720 'name' : idx,
721 'class' : 'form-control',
722 'placeholder' : customfield.translated_label,
723 'data-type' : customfield.type,
724 'data-toggle' : 'tooltip',
725 'title' : customfield.translated_tooltip,
726 'required' : customfield.required,
727 'value' : $('#custom-fields').data('customfield-values')[idx],
728 }));
729 $('#custom-fields input[name="' + idx + '"]').rules('add', {
730 regex: '^[0-9]{1,18}([,.]{1}[0-9]{1,3})?$'
731 });
732 break;
733 case 'decimal':
734 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'text',
735 'id' : idx,
736 'name' : idx,
737 'class' : 'form-control',
738 'placeholder' : customfield.translated_label,
739 'data-type' : customfield.type,
740 'data-toggle' : 'tooltip',
741 'title' : customfield.translated_tooltip,
742 'required' : customfield.required,
743 'value' : $('#custom-fields').data('customfield-values')[idx],
744 }));
745 $('#custom-fields input[name="' + idx + '"]').rules('add', {
746 regex: '^[0-9]{1,18}([,.]{1}[0-9]{1,3})?$'
747 });
748 break;
749 case 'range':
750 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'text',
751 'id' : idx,
752 'name' : idx,
753 'class' : 'form-control',
754 'placeholder' : customfield.translated_label,
755 'data-type' : customfield.type,
756 'data-toggle' : 'tooltip',
757 'title' : customfield.translated_tooltip,
758 'required' : customfield.required,
759 'value' : $('#custom-fields').data('customfield-values')[idx],
760 }));
761 $('#custom-fields input[name="' + idx + '"]').rules('add', {
762 regex: '^[0-9]{1,18}([,.]{1}[0-9]{1,3})?$'
763 });
764 break;
765 case 'money':
766 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'text',
767 'id' : idx,
768 'name' : idx,
769 'class' : 'form-control',
770 'placeholder' : customfield.translated_label,
771 'data-type' : customfield.type,
772 'data-toggle' : 'tooltip',
773 'title' : customfield.translated_tooltip,
774 'required' : customfield.required,
775 'value' : $('#custom-fields').data('customfield-values')[idx],
776 }));
777 $('#custom-fields input[name="' + idx + '"]').keyup(function() {
778 if ($('#price').data('decimal_point') == ',')
779 $(this).val($(this).val().replace(/[^\d,]/g, ''));
780 else
781 $(this).val($(this).val().replace(/[^\d.]/g, ''));
782 });
783 break;
784 case 'date':
785 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'text',
786 'id' : idx,
787 'name' : idx,
788 'class' : 'form-control',
789 'placeholder' : customfield.translated_label,
790 'data-type' : customfield.type,
791 'data-date-format' : 'yyyy-mm-dd',
792 'data-toggle' : 'tooltip',
793 'title' : customfield.translated_tooltip,
794 'required' : customfield.required,
795 'value' : $('#custom-fields').data('customfield-values')[idx],
796 }));
797 $('#custom-fields input[name="' + idx + '"]').datepicker({
798 autoclose: true
799 })
800 break;
801 case 'email':
802 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'email',
803 'id' : idx,
804 'name' : idx,
805 'class' : 'form-control',
806 'placeholder' : customfield.translated_label,
807 'data-type' : customfield.type,
808 'data-toggle' : 'tooltip',
809 'title' : customfield.translated_tooltip,
810 'required' : customfield.required,
811 'value' : $('#custom-fields').data('customfield-values')[idx],
812 }));
813 break;
814 case 'select':
815 $template.find('div[data-input]').replaceWith($('<select/>').attr({ 'id' : idx,
816 'name' : idx,
817 'class' : 'form-control',
818 'placeholder' : customfield.translated_label,
819 'data-type' : customfield.type,
820 'required' : customfield.required,
821 }));
822 $('#custom-fields select[name="' + idx + '"]').append($('<option/>').val(''));
823 $('#custom-fields select[name="' + idx + '"]').append($('<option/>').val(' ').html(' '));
824 for (var val in customfield.translated_values) {
825 $('#custom-fields select[name="' + idx + '"]').append($('<option/>').val(customfield.translated_values[val]).html(customfield.translated_values[val]));
826 }
827 $('#custom-fields select[name="' + idx + '"] option[value="' + $('#custom-fields').data('customfield-values')[idx] +'"]').attr('selected', true);
828 $('#custom-fields select[name="' + idx + '"]').selectize({
829 onChange: function(value) {
830 if (value == ' ')
831 $('#custom-fields select[name="' + idx + '"] option[selected]').val(null);
832 }
833 });
834 $('#custom-fields select[name="' + idx + '"] option[value=" "]').val(null);
835 break;
836 case 'country':
837 $template.find('div[data-input]').replaceWith($('<select/>').attr({ 'id' : idx,
838 'name' : idx,
839 'class' : 'form-control',
840 'placeholder' : customfield.translated_label,
841 'data-type' : customfield.type,
842 'required' : customfield.required,
843 }));
844 $('#custom-fields select[name="' + idx + '"]').append($('<option/>').val(''));
845 $('#custom-fields select[name="' + idx + '"]').append($('<option/>').val(' ').html(' '));
846 for (var val in customfield.translated_values) {
847 $('#custom-fields select[name="' + idx + '"]').append($('<option/>').val(val).html(customfield.translated_values[val]));
848 }
849 $('#custom-fields select[name="' + idx + '"] option[value="' + $('#custom-fields').data('customfield-values')[idx] +'"]').attr('selected', true);
850 $('#custom-fields select[name="' + idx + '"]').selectize({
851 onChange: function(value) {
852 if (value == ' ')
853 $('#custom-fields select[name="' + idx + '"] option[selected]').val(null);
854 }
855 });
856 $('#custom-fields select[name="' + idx + '"] option[value=" "]').val(null);
857 break;
858 case 'file':
859 case 'file_dropbox':
860 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'hidden',
861 'id' : idx,
862 'name' : idx,
863 'class' : 'form-control',
864 'placeholder' : customfield.translated_label,
865 'data-type' : customfield.type,
866 'data-toggle' : 'tooltip',
867 'title' : customfield.translated_tooltip,
868 'required' : customfield.required,
869 'value' : $('#custom-fields').data('customfield-values')[idx],
870 }));
871 $('#custom-fields input[name="' + idx + '"]').after($('<div/>').attr({'id' : idx + '_dropbox',}));
872 options = {
873 success: function(files) {
874 $('#custom-fields input[name="' + idx + '"]').val(files[0].link);
875 },
876 linkType: "direct",
877 multiselect: false,
878 extensions: customfield.translated_values.split(','),
879 };
880 document.getElementById(idx + '_dropbox').appendChild(Dropbox.createChooseButton(options));
881 break;
882 case 'video':
883 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'hidden',
884 'id' : idx,
885 'name' : idx,
886 'class' : 'form-control',
887 'placeholder' : customfield.translated_label,
888 'data-type' : customfield.type,
889 'data-toggle' : 'tooltip',
890 'title' : customfield.translated_tooltip,
891 'required' : customfield.required,
892 'value' : $('#custom-fields').data('customfield-values')[idx],
893 }));
894 $('#custom-fields input[name="' + idx + '"]').before($('<br/>'));
895 $('#custom-fields input[name="' + idx + '"]').after($('<button/>').attr({'id' : idx + '_cloudinary', 'class' : 'cloudinary-upload-button btn btn-default', 'type' : 'button'}).html('Upload Video'));
896 var cloudinaryWidget = cloudinary.createUploadWidget({
897 buttonClass : 'cloudinary-upload-button',
898 sources: [ 'local'],
899 multiple: false,
900 showAdvancedOptions: false,
901 cloudName: cloudinaryCloudName,
902 uploadPreset: cloudinaryUploadPreset}, (error, result) => {
903 if (!error && result && result.event === "success") {
904 $('#' + idx + '_cloudinary').hide();
905 $('input[data-type="video"]').val(JSON.stringify({ url: result.info.secure_url, public_id: result.info.public_id}));
906 $('#custom-fields input[name="' + idx + '"]').after($('<div/>', {
907 class: '',
908 }).append($('<video />', {
909 class: 'img-responsive thumbnail',
910 src: result.info.secure_url,
911 type: 'video/mp4',
912 controls: true
913 })));
914 }
915 }
916 )
917 document.getElementById(idx + '_cloudinary').addEventListener("click", function(){
918 cloudinaryWidget.open();
919 }, false);
920 break;
921 case 'file_gpicker':
922 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'hidden',
923 'id' : idx,
924 'name' : idx,
925 'class' : 'form-control',
926 'placeholder' : customfield.translated_label,
927 'data-type' : customfield.type,
928 'data-toggle' : 'tooltip',
929 'title' : customfield.translated_tooltip,
930 'required' : customfield.required,
931 'value' : $('#custom-fields').data('customfield-values')[idx],
932 }));
933 $('#custom-fields input[name="' + idx + '"]')
934 .after($('<div/>')
935 .attr({'id' : idx + '_gpicker',})
936 .append('<a class="gpicker btn btn-sm btn-default" href="#"><i class="fa fa-google fa-fw text-primary" aria-hidden="true"></i> <strong>' + getCFSearchLocalization('upload_file_to_google_drive') + '</strong></a>'));
937 $('.gpicker').click(function() {
938 var id = this.id;
939 var viewId = new google.picker.DocsUploadView();
940 var setOAuthToken = true;
941
942 if (authApiLoaded && ! oauthToken) {
943 viewIdForhandleAuthResult = viewId;
944 window.gapi.auth.authorize(
945 {
946 'client_id': clientId,
947 'scope': scope,
948 'immediate': false
949 },
950 handleAuthResult
951 );
952 } else {
953 createPicker(viewId, setOAuthToken);
954 }
955
956 return false;
957 });
958 break;
959 case 'radio':
960 $.each(customfield.translated_values, function (radioidx, value) {
961 $('<div/>').attr('class', 'radio').append($('<label/>').append($('<input/>').attr({ 'type' : 'radio',
962 'id' : idx,
963 'name' : idx,
964 'data-type' : customfield.type,
965 'data-toggle' : 'tooltip',
966 'title' : customfield.translated_tooltip,
967 'required' : customfield.required,
968 'value' : radioidx + 1,
969 'checked' : ((radioidx + 1) == $('#custom-fields').data('customfield-values')[idx]) ? true:false,
970 })).append(value)).insertBefore($template.find('div[data-input]'));
971 });
972 $template.find('div[data-input]').remove();
973 break;
974 case 'checkbox':
975 $template.find('div[data-input]').wrap('<div class="checkbox"></div>').wrap('<label></label>');
976 $template.find('div[data-input]').replaceWith($('<input/>').attr({ 'type' : 'checkbox',
977 'id' : idx,
978 'name' : idx,
979 'data-type' : customfield.type,
980 'data-toggle' : 'tooltip',
981 'title' : customfield.translated_tooltip,
982 'required' : customfield.required,
983 'checked' : $('#custom-fields').data('customfield-values')[idx],
984 }));
985 break;
986 case 'checkbox_group':
987 $template.find('div[data-input]').replaceWith($('<div/>').attr({
988 'id': idx,
989 'data-type': customfield.type,
990 }));
991
992 for (var key in customfield.grouped_values) {
993 var name = 'cf_' + key;
994 var label = customfield.grouped_values[key];
995
996 $('#custom-fields div[id="' + idx + '"]').append($('<input/>').attr({
997 'type': 'checkbox',
998 'id': name,
999 'name': name,
1000 'data-type': customfield.type,
1001 'data-toggle': 'tooltip',
1002 'title': customfield.translated_tooltip,
1003 'class': 'form-check-input',
1004 'checked': $('#custom-fields').data('customfield-values')[label],
1005 }));
1006
1007 $('input[name="' + name + '"]').wrap('<div class="form-check"></div>').after($('<label class="form-check-label"></label>').text(label));
1008
1009 $('input[name="' + name + '"]').before($('<input/>').attr({
1010 'type': 'hidden',
1011 'name': name,
1012 'value': 0,
1013 }));
1014
1015 if (customfield.required) {
1016 $('input[name="' + name + '"]').rules('add', {
1017 required_checkbox_group: idx
1018 });
1019 }
1020 }
1021 break;
1022 case 'json':
1023 if (idx === 'cf_openinghours') {
1024 $template.find('div:first').removeClass('col-sm-4').addClass('col-sm-8').find('div[data-input]').replaceWith($('#opening-hours-form-group').clone().attr({'id' : idx}).removeClass('d-none').append($('#custom-fields').data('customfield-values')[idx]));
1025
1026 $('input[class*=dayopen]').on('change', function () {
1027 if ($(this).val() == '1') //open
1028 {
1029 $(this).closest('.form-group').find('.openninghours').removeClass('d-none');
1030 $(this).closest('.form-group').find('select').select2({
1031 width: '100%'
1032 });
1033 }
1034 else //closed
1035 {
1036 $(this).closest('.form-group').find('.openninghours').addClass('d-none');
1037 }
1038 });
1039 } else {
1040 $template.find('div[data-input]').replaceWith($('<textarea/>').attr({ 'id' : idx,
1041 'name' : idx,
1042 'class' : 'form-control',
1043 'placeholder' : customfield.translated_label,
1044 'rows' : 10,
1045 'cols' : 50,
1046 'data-type' : customfield.type,
1047 'data-toggle' : 'tooltip',
1048 'title' : customfield.translated_tooltip,
1049 'required' : customfield.required,
1050 }).append($('#custom-fields').data('customfield-values')[idx]));
1051 }
1052
1053 break;
1054 }
1055 });
1056
1057 $('input[data-toggle=tooltip]').tooltip({
1058 placement: "right",
1059 trigger: "focus"
1060 });
1061
1062 if (typeof CarQuery !== 'undefined' && $.isFunction(CarQuery) && customfields['cf_make'] != undefined && customfields['cf_model'] != undefined && customfields['cf_year'] != undefined) {
1063 $('select#cf_make')[0].selectize.destroy();
1064 $('select#cf_model')[0].selectize.destroy();
1065 $('select#cf_year')[0].selectize.destroy();
1066
1067 var carquery = new CarQuery();
1068 carquery.init('', '', '');
1069 carquery.initYearMakeModelTrim('cf_year', 'cf_make', 'cf_model');
1070 }
1071
1072 if (customfields['cf_brand'] != undefined && customfields['cf_model'] != undefined && customfields['cf_generation'] != undefined) {
1073 autoDataAPI(customfields, 'select#cf_brand', 'select#cf_model', 'select#cf_generation')
1074 }
1075}
1076
1077$("#price").keyup(function() {
1078 if ($(this).data('decimal_point') == ',')
1079 $(this).val($(this).val().replace(/[^\d,]/g, ''));
1080 else
1081 $(this).val($(this).val().replace(/[^\d.]/g, ''));
1082});
1083
1084function onApiLoad() {
1085 gapi.load('auth', {'callback': onAuthApiLoad});
1086 gapi.load('picker', {'callback': onPickerApiLoad});
1087}
1088
1089function onAuthApiLoad() {
1090 authApiLoaded = true;
1091}
1092
1093function onPickerApiLoad() {
1094 pickerApiLoaded = true;
1095}
1096
1097function handleAuthResult(authResult) {
1098 if (authResult && ! authResult.error) {
1099 oauthToken = authResult.access_token;
1100 createPicker(viewIdForhandleAuthResult, true);
1101 }
1102}
1103
1104function createPicker(viewId, setOAuthToken) {
1105 if (authApiLoaded && pickerApiLoaded) {
1106 var picker;
1107
1108 if (authApiLoaded && oauthToken && setOAuthToken) {
1109 picker = new google.picker.PickerBuilder().
1110 addView(viewId).
1111 setOAuthToken(oauthToken).
1112 setDeveloperKey(developerKey).
1113 setCallback(pickerCallback).
1114 build();
1115 } else {
1116 picker = new google.picker.PickerBuilder().
1117 addView(viewId).
1118 setDeveloperKey(developerKey).
1119 setCallback(pickerCallback).
1120 build();
1121 }
1122
1123 picker.setVisible(true);
1124 }
1125}
1126
1127function pickerCallback(data) {
1128 if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
1129 doc = data[google.picker.Response.DOCUMENTS][0];
1130 $('input[data-type="file"]').val(doc.downloadUrl);
1131 }
1132}
1133
1134if ($('#phone').length) {
1135 $("#phone").intlTelInput({
1136 formatOnDisplay: false,
1137 autoPlaceholder: false,
1138 initialCountry: $('#phone').data('country')
1139 });
1140}
1141