-
Notifications
You must be signed in to change notification settings - Fork 14
/
panel_type_form_builder.js
2322 lines (2090 loc) · 74.3 KB
/
panel_type_form_builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018, Tribal Limited
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Zenario, Tribal Limited nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL TRIBAL LTD BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
This file contains JavaScript source code.
The code here is not the code you see in your browser. Before this file is downloaded:
1. Compilation macros are applied (e.g. "foreach" is a macro for "for .. in ... hasOwnProperty").
2. It is minified (e.g. using Google Closure Compiler).
3. It may be wrapped togther with other files (this is to reduce the number of http requests on a page).
For more information, see js_minify.shell.php for steps (1) and (2), and organizer.wrapper.js.php for step (3).
*/
zenario.lib(function(
undefined,
URLBasePath,
document, window, windowOpener, windowParent,
zenario, zenarioA, zenarioT, zenarioAB, zenarioAT, zenarioO,
encodeURIComponent, defined, engToBoolean, get, htmlspecialchars, jsEscape, phrase,
extensionOf, methodsOf, has,
panelTypes
) {
"use strict";
//Note: extensionOf() and methodsOf() are our shortcut functions for class extension in JavaScript.
//extensionOf() creates a new class (optionally as an extension of another class).
//methodsOf() allows you to get to the methods of a class.
var methods = methodsOf(
panelTypes.form_builder = extensionOf(panelTypes.form_builder_base_class)
);
//Draw the panel, as well as the header at the top and the footer at the bottom
//This is called every time the panel is loaded, refreshed or when something in the header toolbar is changed.
methods.showPanel = function($header, $panel, $footer) {
$header.html('').hide();
$footer.html('').show();
thus.sortOutItems();
//Load main panel
var html = thus.loadPanelHTML();
$panel.html(html).show();
//On initial load select the first page
if (_.isEmpty(thus.tuix.items)) {
thus.addNewPage(true);
}
if (!thus.currentPageId) {
var pages = thus.getOrderedPages();
if (pages.length > 0) {
thus.currentPageId = pages[0].id;
}
}
thus.editFieldName = false;
thus.doneEditingName = false;
thus.pagesReordered = false;
thus.deletedPages = [];
thus.deletedFields = [];
thus.deletedValues = [];
thus.changeDetected = false;
thus.maxNewCustomField = 1;
thus.maxNewCustomFieldValue = 1;
thus.selectedDetailsPage = thus.selectedDetailsPage ? thus.selectedDetailsPage : false;
thus.selectedFieldId = thus.selectedFieldId ? thus.selectedFieldId : false;
thus.selectedPageId = thus.selectedPageId ? thus.selectedPageId : false;
//Load right panel
thus.loadPagesList(thus.currentPageId);
thus.loadFieldsList(thus.currentPageId);
//Load left panel
if (thus.selectedFieldId) {
thus.loadFieldDetailsPanel(thus.selectedFieldId, true);
} else if (thus.selectedPageId) {
thus.loadPageDetailsPanel(thus.selectedPageId, true);
} else {
thus.loadNewFieldsPanel(true);
}
//Show Growl message if saved changes
if (thus.changesSaved) {
thus.changesSaved = false;
var toast = {
message_type: 'success',
message: 'Your changes have been saved!'
};
zenarioA.toast(toast);
}
};
methods.sortOutItems = function() {
var pageId, page, fieldId, field;
foreach (thus.tuix.fields as fieldId => field) {
//This array is turned into an object when parsed. Turn it back into an array...
if (field.invalid_responses) {
field.invalid_responses = _.toArray(field.invalid_responses);
}
if (field.visible_condition_checkboxes_field_value) {
field.visible_condition_checkboxes_field_value = _.toArray(field.visible_condition_checkboxes_field_value);
}
if (field.mandatory_condition_checkboxes_field_value) {
field.mandatory_condition_checkboxes_field_value = _.toArray(field.mandatory_condition_checkboxes_field_value);
}
}
foreach (thus.tuix.items as pageId => page) {
if (page.visible_condition_checkboxes_field_value) {
page.visible_condition_checkboxes_field_value = _.toArray(page.visible_condition_checkboxes_field_value);
}
if (page.mandatory_condition_checkboxes_field_value) {
page.mandatory_condition_checkboxes_field_value = _.toArray(page.mandatory_condition_checkboxes_field_value);
}
}
};
methods.loadPanelHTML = function() {
var mergeFields = {
form_title: thus.tuix.form_title
};
var html = thus.microTemplate('zenario_organizer_form_builder', mergeFields);
return html;
};
methods.loadPageDetailsPanel = function(pageId, stopEffect) {
thus.loadFieldDetailsPanel(pageId, stopEffect, true);
var label, page, message, fields;
$('#field__name').on('keyup', function() {
label = thus.getPageLabel($(this).val());
$('#organizer_form_tab_' + pageId + ' span').text(label);
});
$('#organizer_remove_form_tab').on('click', function(e) {
page = thus.tuix.items[pageId];
if (page) {
message = '<p>Are you sure you want to delete this page?</p>';
fields = thus.getOrderedFields(pageId);
if (fields.length) {
message += '<p>All fields on this page will be moved onto the previous page.</p>';
}
zenarioA.floatingBox(message, 'Delete', 'warning', true, false, function() {
thus.deletePage(pageId);
});
}
});
};
methods.loadFieldDetailsPanel = function(fieldId, stopEffect, isPage, justAdded) {
thus.editFieldName = false;
thus.doneEditingName = false;
var field;
if (isPage) {
field = thus.tuix.items[fieldId];
field.type = 'page_break';
} else {
field = thus.tuix.fields[fieldId];
}
//Remove "just_added" status if editing after adding
if (!justAdded && field.just_added) {
delete(field.just_added);
}
var typePhrase = thus.getFieldReadableType(field.type);
if (field.dataset_field_id) {
typePhrase += ', linked field → ' + field.db_column;
}
//Load HTML
var mergeFields = {
mode: 'field_details',
name: field.name,
type: field.type,
typePhrase: typePhrase,
just_added: field.just_added,
hide_tab_bar: thus.tuix.field_details.hide_tab_bar,
is_tab: isPage
};
if (field.dataset_field_id) {
mergeFields.type += ', dataset field';
if (field.db_column) {
mergeFields.type += ' (' + field.db_column + ')';
}
}
var html = thus.microTemplate('zenario_organizer_form_builder_left_panel', mergeFields);
var $div = $('#organizer_form_builder .form_fields_palette .form_fields_palette_outer').html(html);
if (!stopEffect) {
$div.hide().show('drop', {direction: 'left'}, 200);
}
//Load TUIX pages HTML
var tuix = thus.tuix.field_details.tabs;
var pages = thus.sortAndLoadTUIXPages(tuix, field, thus.selectedDetailsPage);
var html = thus.getTUIXPagesHTML(pages);
$('#organizer_field_details_tabs').html(html);
//Add field details page events
$('#organizer_field_details_tabs .tab').on('click', function() {
var page = $(this).data('name');
if (page && page != thus.selectedDetailsPage) {
var errors = thus.saveCurrentOpenDetails();
if (!errors) {
thus.loadFieldDetailsPage(page, fieldId);
}
}
});
//Load TUIX fields html
thus.loadFieldDetailsPage(thus.selectedDetailsPage, fieldId);
//Add panel events
//Go to new fields panel
$('#organizer_field_details_inner span.add_new_field').on('click', function() {
var errors = thus.saveCurrentOpenDetails();
if (!errors) {
thus.highlightField(false);
thus.loadNewFieldsPanel();
}
});
//Editing field name
$('#zenario_field_details_header_content .edit_field_name_button').on('click', function() {
$('#zenario_field_details_header_content .view_mode').hide();
$('#zenario_field_details_header_content .edit_mode').show();
thus.editFieldName = true;
});
$('#zenario_field_details_header_content .done_field_name_button').on('click', function() {
$('#zenario_field_details_header_content .edit_mode').hide();
$('#zenario_field_details_header_content .view_mode').show();
thus.editFieldName = false;
thus.doneEditingName = true;
var name = $('#zenario_field_details_header_content .edit_mode input[name="name"]').val();
field.name = name;
$('#zenario_field_details_header_content .view_mode h5').text(name);
});
$('#field__name').on('keyup', function() {
thus.changeMadeToPanel();
});
//Edit form settings
$('input.form_settings').on('click', function() {
thus.openFormSettings();
});
};
methods.openFormSettings = function() {
zenarioAB.open(
'zenario_user_form',
{id: zenarioO.pi.refiner.id},
undefined, undefined,
function(key, values) {
//Update form title (note if lots of changes might be better to redraw entire form)
var title = '';
if (values.details.show_title) {
title = values.details.title;
}
thus.tuix.title = title;
$('#organizer_form_builder .form_outer .form_header h5').text(title);
}
);
};
methods.loadPageDetailsPage = function(page, pageId, errors) {
thus.loadFieldDetailsPage(page, pageId, errors);
};
methods.loadFieldDetailsPage = function(page, fieldId, errors) {
var item;
thus.selectedDetailsPage = page;
if (thus.selectedFieldId && thus.tuix.fields[fieldId]) {
item = thus.tuix.fields[fieldId]
} else if (thus.selectedPageId && thus.tuix.items[fieldId]) {
item = thus.tuix.items[fieldId];
//Final page must be visible
var pages = thus.getOrderedPages();
for (var i = 0; i < pages.length; i++) {
if (pages[i].id == item.id) {
if (i == (pages.length - 1) && !thus.tuix.form_enable_summary_page) {
item.visibility = 'visible';
}
break;
}
}
}
var path = 'field_details/' + page;
var tuix = thus.tuix.field_details.tabs[page].fields;
var fields = thus.loadFields(path, tuix, item);
var formattedFields = thus.formatFieldDetails(fields, item, 'field', page);
var sortedFields = thus.sortFields(formattedFields, item);
var html = '';
//Add any errors
if (!_.isEmpty(errors)) {
errors = thus.sortErrors(errors);
for (var i = 0; i < errors.length; i++) {
html += '<p class="error">' + errors[i] + '</p>';
}
}
html += thus.getTUIXFieldsHTML(sortedFields);
$('#organizer_field_details').html(html);
$('#organizer_field_details_tabs .tab').removeClass('on');
$('#field_tab__' + page).addClass('on');
if (!_.isEmpty(errors)) {
$('#organizer_field_details_form').effect({
effect: 'bounce',
duration: 125,
direction: 'right',
times: 2,
distance: 5,
mode: 'effect'
});
}
//Add events
if (page == 'details') {
$('#field__field_label').on('keyup', function() {
$('#organizer_form_field_' + fieldId + ' .label').text($(this).val());
if (item.just_added && !thus.editFieldName && !thus.doneEditingName) {
var fieldName = $(this).val().replace(/:/g, '');
$('#field__name').val(fieldName);
$('#field_display__name').text(fieldName);
}
});
$('#field__note_to_user').on('keyup', function() {
var value = $(this).val().trim();
$('#organizer_form_field_note_below_' + fieldId + ' div.zenario_note_content').text(value);
$('#organizer_form_field_note_below_' + fieldId).toggle(value !== '');
});
if (item.type == 'text' || item.type == 'textarea') {
$('#field__placeholder').on('keyup', function() {
$('#organizer_form_field_' + fieldId + ' :input').prop('placeholder', $(this).val());
});
} else if (item.type == 'section_description') {
$('#field__description').on('keyup', function() {
$('#organizer_form_field_' + fieldId + ' .description').text($(this).val());
});
} else if (item.type == 'calculated') {
$('#edit_field_calculation').on('click', function() {
//Get all numeric text fields
var numericFields = {};
foreach (thus.tuix.fields as var tFieldId => var tField) {
if ((tField.type == 'text' && ['number', 'integer', 'floating_point'].indexOf(tField.field_validation) != -1)
|| (tField.type == 'calculated' && fieldId != tFieldId)
) {
numericFields[tFieldId] = {label: tField.name, ord: tField.ord};
}
}
var key = {
id: fieldId,
title: 'Editing the calculation for the field "' + item.name + '"',
calculation_code: JSON.stringify(item.calculation_code)
};
var values = {
details: {
dummy_field: JSON.stringify(numericFields)
}
};
zenarioAB.open(
'zenario_field_calculation',
key,
undefined, values,
function(key, values) {
if (values.details.calculation_code) {
item.calculation_code = JSON.parse(values.details.calculation_code);
} else {
item.calculation_code = '';
}
thus.changeMadeToPanel();
thus.saveCurrentOpenDetails();
thus.loadFieldDetailsPage('details', fieldId);
}
);
});
}
} else if (page == 'advanced') {
$('#field__css_classes').on('keyup', function() {
$('#organizer_form_field_' + fieldId + ' .form_field_classes .css').toggle(!!$(this).val()).prop('title', $(this).val());
});
$('#field__div_wrap_class').on('keyup', function() {
$('#organizer_form_field_' + fieldId + ' .form_field_classes .div').toggle(!!$(this).val()).prop('title', $(this).val());
});
} else if (page == 'values') {
if (formattedFields.values && !formattedFields.values._hidden) {
var values = thus.getOrderedFieldValues(fieldId);
html = thus.microTemplate('zenario_organizer_admin_box_builder_field_value', values);
$('#field_values_list').html(html).sortable({
containment: 'parent',
tolerance: 'pointer',
axis: 'y',
start: function(event, ui) {
thus.startIndex = ui.item.index();
},
stop: function(event, ui) {
if (thus.startIndex != ui.item.index()) {
thus.saveFieldListOfValues(thus.tuix.fields[fieldId]);
thus.loadFieldValuesListPreview(fieldId);
thus.changeMadeToPanel();
}
}
});
$('#field_values_list input').on('keyup', function() {
var id = $(this).data('id');
$('#organizer_field_value_' + id + ' label').text($(this).val());
thus.changeMadeToPanel();
});
//Add new field value
$('#organizer_add_a_field_value').on('click', function() {
thus.addFieldValue(item);
thus.saveItemTUIXPage('field_details', page, item);
thus.loadFieldDetailsPage(page, fieldId);
thus.loadFieldValuesListPreview(fieldId);
thus.changeMadeToPanel();
});
//Delete field value
$('#field_values_list .delete_icon').on('click', function() {
var id = $(this).data('id');
if (item.lov[id]) {
thus.deletedValues.push(id);
delete(item.lov[id]);
}
thus.saveItemTUIXPage('field_details', page, item);
thus.loadFieldDetailsPage(page, fieldId);
thus.loadFieldValuesListPreview(fieldId);
thus.changeMadeToPanel();
});
}
} else if (page == 'translations') {
var transFieldNamesList = _.toArray(thus.tuix.field_details.tabs[page].translatable_fields);
var transFieldNames = {};
for (var i = 0; i < transFieldNamesList.length; i++) {
transFieldNames[transFieldNamesList[i]] = true;
}
//Currently translations only support fields on the details page
page = 'details';
path = 'field_details/' + page;
tuix = thus.tuix.field_details.tabs[page].fields;
fields = thus.loadFields(path, tuix, item);
formattedFields = thus.formatFieldDetails(fields, item, 'field', page);
sortedFields = thus.sortFields(formattedFields, item);
var visibleTransFieldNamesList = [];
foreach (sortedFields as var i => var sField) {
if (transFieldNames[sField.id] && !sField._hidden) {
visibleTransFieldNamesList.push(sField.id);
}
}
var languages = thus.sortLanguages(thus.tuix.languages);
var transFields = [];
foreach (visibleTransFieldNamesList as var i => var transFieldName) {
if (formattedFields[transFieldName] && !formattedFields[transFieldName]._hidden) {
var existingTranslations = {};
if (item._translations && item._translations[transFieldName]) {
existingTranslations = item._translations[transFieldName];
}
var transField = {};
if (tuix[transFieldName] && tuix[transFieldName].label) {
transField.label = tuix[transFieldName].label;
}
if (item[transFieldName]) {
transField.value = item[transFieldName];
} else {
transField.value = '(No text is defined in the default language)';
transField.disabled = true;
}
transField.phrases = [];
foreach (languages as var j => var language) {
if (parseInt(language.translate_phrases)) {
var phrase = (existingTranslations.phrases && existingTranslations.phrases[language.id] && !transField.disabled) ? existingTranslations.phrases[language.id] : '';
transField.phrases.push({
field_column: transFieldName,
language_id: language.id,
language_name: language.english_name,
phrase: phrase,
disabled: transField.disabled
});
}
}
transFields.push(transField);
}
}
var html = thus.microTemplate('zenario_organizer_form_builder_translatable_field', transFields);
$('#organizer_field_translations').html(html);
$('#organizer_field_translations input').on('keyup', function() {
thus.changeMadeToPanel();
});
} else if (page == 'crm') {
var prefix = '';
if (page == 'crm') {
prefix = 'crm';
}
var crm_value_name = prefix + '_value';
$('#organizer_crm_button__set_labels').on('click', function() {
thus.changeMadeToPanel();
$('#organizer_field_crm_values input.crm_value_input').each(function(i, e) {
var id = $(this).data('id');
var value = '';
if (thus.tuix.fields[fieldId].lov && thus.tuix.fields[fieldId].lov[id]) {
value = thus.tuix.fields[fieldId].lov[id].label;
}
$(this).val(value);
});
});
$('#organizer_crm_button__set_values').on('click', function() {
thus.changeMadeToPanel();
$('#organizer_field_crm_values input.crm_value_input').each(function(i, e) {
var id = $(this).data('id');
$(this).val(id);
});
});
var mergeFields = {
values: []
};
if (item.type == 'checkbox' || item.type == 'group' || item.type == 'consent') {
var values = {
'unchecked': {
label: 0,
ord: 1
},
'checked': {
label: 1,
ord: 2
}
};
if (item['_' + prefix + '_data'] && item['_' + prefix + '_data'].values) {
foreach (item['_' + prefix + '_data'].values as var valueId => var value) {
if (values[valueId] && defined(value[prefix + '_value'])) {
values[valueId][prefix + '_value'] = value[prefix + '_value'];
}
}
}
foreach (values as var valueId => var value) {
value.id = valueId;
value.crm_value_name = crm_value_name;
mergeFields.values.push(value);
}
mergeFields.values.sort(thus.sortByOrd);
} else {
var values = thus.getOrderedFieldValues(fieldId);
foreach (values as var valueId => var value) {
value.crm_value_name = crm_value_name;
mergeFields.values.push(value);
}
}
var html = thus.microTemplate('zenario_organizer_form_builder_crm_values', mergeFields);
$('#organizer_field_crm_values').html(html);
$('#organizer_field_crm_values input.crm_value_input').on('keyup', function() {
thus.changeMadeToPanel();
});
}
};
//Calculation admin box methods
methods.calculationAdminBoxAddSomthing = function(type, value) {
var code = false;
switch (type) {
case 'operation_addition':
case 'operation_subtraction':
case 'operation_multiplication':
case 'operation_division':
case 'parentheses_open':
case 'parentheses_close':
code = {type: type};
break;
case 'static_value':
if (value !== '' && !isNaN(+value)) {
code = {type: type, value: +value};
$('#static_value').val('');
}
break;
case 'field':
code = {type: type, value: value};
$('#numeric_field').val('');
break;
}
if (code) {
var calculationCode = [];
if ($('#calculation_code').val()) {
calculationCode = JSON.parse($('#calculation_code').val());
}
calculationCode.push(code);
$('#calculation_code').val(JSON.stringify(calculationCode));
thus.calculationAdminBoxUpdateDisplay(calculationCode);
}
};
methods.calculationAdminBoxDelete = function() {
var calculationCode = [];
if ($('#calculation_code').val()) {
calculationCode = JSON.parse($('#calculation_code').val());
if (calculationCode) {
calculationCode.pop();
$('#calculation_code').val(JSON.stringify(calculationCode));
}
}
thus.calculationAdminBoxUpdateDisplay(calculationCode);
};
methods.calculationAdminBoxUpdateDisplay = function(calculationCode) {
var calculationDisplay = thus.getCalculationCodeDisplay(calculationCode);
$('#zenario_calculation_display').text(calculationDisplay);
};
methods.getCalculationCodeDisplay = function(calculationCode) {
var calculationDisplay = '',
fields, fieldId, field, lastIsParenthesisOpen, i, name;
if (calculationCode) {
fields = {};
foreach (thus.tuix.fields as fieldId => field) {
fields[fieldId] = field;
}
lastIsParenthesisOpen = false;
for (i = 0; i < calculationCode.length; i++) {
if (!lastIsParenthesisOpen && calculationCode[i].type != 'parentheses_close') {
calculationDisplay += ' ';
} else if (lastIsParenthesisOpen) {
lastIsParenthesisOpen = false;
}
switch (calculationCode[i].type) {
case 'operation_addition':
calculationDisplay += '+';
break;
case 'operation_subtraction':
calculationDisplay += '-';
break;
case 'operation_multiplication':
calculationDisplay += '×';
break;
case 'operation_division':
calculationDisplay += '÷';
break;
case 'parentheses_open':
calculationDisplay += '(';
lastIsParenthesisOpen = true;
break;
case 'parentheses_close':
calculationDisplay += ')';
break;
case 'static_value':
calculationDisplay += calculationCode[i].value;
break;
case 'field':
name = '';
if (fields[calculationCode[i].value]) {
name = fields[calculationCode[i].value].name;
} else {
name = 'UNKNOWN FIELD';
}
calculationDisplay += '"' + name + '"';
break;
}
calculationDisplay = calculationDisplay.trim();
}
}
return calculationDisplay;
};
methods.formatFieldDetails = function(fields, item, mode, page) {
if (mode == 'field') {
if (page == 'details') {
if (item.dataset_field_id) {
fields.values_source._disabled = true;
fields.values_source_filter._readonly = true;
fields.min_rows._readonly = true;
fields.max_rows._readonly = true;
}
var values_source = fields.values_source._value;
if (values_source && thus.tuix.centralised_lists.values[values_source]) {
var list = thus.tuix.centralised_lists.values[values_source];
if (!list.info.can_filter) {
fields.values_source_filter._hidden = true;
} else {
fields.values_source_filter.label = list.info.filter_label;
}
}
if (item.visibility == 'visible_on_condition' && item.visible_condition_field_id) {
var conditionField = thus.getField(item.visible_condition_field_id);
if (conditionField) {
if (conditionField.type == 'checkboxes') {
fields.visible_condition_checkboxes_field_value.values = conditionField.lov;
fields.visible_condition_field_value._hidden = true;
fields.visible_condition_checkboxes_operator._hidden = false;
} else if (conditionField.type == 'checkbox' || conditionField.type == 'group' || conditionField.type == 'consent') {
//Nothing to do
} else {
fields.visible_condition_field_value.empty_value = '-- Any value --';
fields.visible_condition_field_value.values = conditionField.lov;
fields.visible_condition_field_type.values = _.clone(fields.visible_condition_field_type.values);
fields.visible_condition_field_type.values.visible_if_one_of = {label: 'Visible if one of...'};
if (item.visible_condition_field_type == 'visible_if_one_of') {
fields.visible_condition_checkboxes_field_value.values = conditionField.lov;
fields.visible_condition_field_value._hidden = true;
}
}
if (conditionField.type != 'checkboxes' && item.visible_condition_field_type != 'visible_if_one_of') {
fields.visible_condition_checkboxes_field_value._hidden = true;
}
}
}
if (item.readonly_or_mandatory == 'conditional_mandatory' && item.mandatory_condition_field_id) {
var conditionField = thus.getField(item.mandatory_condition_field_id);
if (conditionField.type == 'checkboxes') {
fields.mandatory_condition_checkboxes_field_value.values = conditionField.lov;
fields.mandatory_condition_field_value._hidden = true;
fields.mandatory_condition_checkboxes_operator._hidden = false;
} else {
if (conditionField && conditionField.type != 'checkbox' && conditionField.type != 'group' && conditionField.type != 'consent') {
fields.mandatory_condition_field_value.values = conditionField.lov;
fields.mandatory_condition_field_value.empty_value = '-- Any value --';
}
fields.mandatory_condition_checkboxes_field_value._hidden = true;
}
}
if (item.type == 'repeat_start') {
fields.field_label.label = 'Section heading:';
fields.field_label.note_below = 'This will be the heading at the start of the repeating section.';
} else if (item.type == 'calculated') {
if (item.calculation_code) {
//Make sure this is an array not an object
if (!Array.isArray(item.calculation_code)) {
item.calculation_code = $.map(item.calculation_code, function(i) { return i });
}
fields.calculation_code._value = thus.getCalculationCodeDisplay(item.calculation_code);
}
} else if (item.type == 'page_break') {
var pages = thus.getOrderedPages();
for (var i = 0; i < pages.length; i++) {
if (pages[i].id == item.id) {
if (i == 0) {
fields.previous_button_text._hidden = true;
} else if (i == (pages.length - 1) && !thus.tuix.form_enable_summary_page) {
fields.next_button_text._hidden = true;
fields.visibility._disabled = true;
}
break;
}
}
}
} else if (page == 'advanced') {
if (item.default_value_options == 'value') {
if (['radios', 'centralised_radios', 'select', 'centralised_select'].indexOf(item.type) != -1) {
fields.default_value_lov.values = item.lov;
}
}
if (item.type == 'select') {
fields.default_value_options.label = 'Pre-select field:';
}
}
}
//Re-draw page tabs for visibility class
if (item.type == 'page_break') {
thus.loadPagesList(thus.currentPageId);
}
return fields;
};
methods.getField = function(fieldId) {
if (thus.tuix.fields[fieldId]) {
return thus.tuix.fields[fieldId];
}
return false;
};
methods.getFieldValuesList = function(field) {
var sortedValues = [];
var fieldName = field.id;
var values = field.values;
var value = field._value;
var disabled = field._disabled;
var usesOptGroups = false;
if (typeof(values) == 'object') {
var i = 0;
foreach (values as var tValueId => var tValue) {
var option = {
label: tValue.label,
value: tValueId,
ord: defined(tValue.ord) ? tValue.ord : ++i
};
sortedValues.push(option);
}
} else if (values == 'centralised_lists') {
var i = 0;
foreach (thus.tuix.centralised_lists.values as var func => var details) {
var option = {
ord: ++i,
label: details.label,
value: func
};
sortedValues.push(option);
}
} else if (values == 'centralised_list_filter_fields'
|| values == 'conditional_fields'
|| values == 'mirror_fields'
) {
var tPages = thus.getOrderedPages();
usesOptGroups = tPages.length > 1;
var ord = 1;
for (var i = 0; i < tPages.length; i++) {
var tFields = thus.getOrderedFields(tPages[i].id);
var parentIndex = false;
var tPage = tPages[i];
for (var j = 0; j < tFields.length; j++) {
var tField = tFields[j];
if (thus.canAddFieldToList(values, tField)) {
if (parentIndex === false && usesOptGroups) {
parentIndex = sortedValues.push({
ord: ord++,
label: tPage.name,
hasChildren: true,
options: []
}) - 1;
}
var option = {
ord: ord++,
label: tField.name,
value: tField.id,
parent: parentIndex
};
sortedValues.push(option);
}
}
}
} else if (values == 'field_lov') {
if (thus.tuix.fields[thus.selectedFieldId] && thus.tuix.fields[thus.selectedFieldId].lov) {
foreach (thus.tuix.fields[thus.selectedFieldId].lov as var tValueId => var tValue) {
var option = {
ord: tValue.ord,
label: tValue.label,
value: tValueId
};
sortedValues.push(option);
}
}
}
foreach (sortedValues as var index => var option) {
if (field.type == 'checkboxes') {
if (value) {
if (value.indexOf(option.value) != -1) {
option.selected = true;
}
}
} else {
if (option.value == value) {
option.selected = true;
}
}
if (disabled) {
option.disabled = true;
}
option.name = fieldName;
option.path = field.path;
if (usesOptGroups && defined(option.parent)) {
sortedValues[option.parent].options.push(option);
delete(sortedValues[index]);
}
}
sortedValues.sort(thus.sortByOrd);
return sortedValues;
};
methods.canAddFieldToList = function(values, field) {
if (values == 'centralised_list_filter_fields') {
return (field.id != thus.selectedFieldId) && (field.type == 'centralised_select' || (field.type == 'text' && field.autocomplete && field.values_source));
} else if (values == 'conditional_fields') {
return (field.id != thus.selectedFieldId) && (['checkbox', 'group', 'consent', 'radios', 'select', 'centralised_radios', 'centralised_select', 'checkboxes'].indexOf(field.type) != -1);
} else if (values == 'mirror_fields') {
return (field.id != thus.selectedFieldId) && (['text', 'calculated', 'select', 'centralised_select'].indexOf(field.type) != -1);
}
return false;
};
methods.loadNewFieldsPanel = function(stopEffect) {
thus.selectedPageId = false;
thus.selectedFieldId = false;
//Load HTML
var mergeFields = {
mode: 'new_fields',
datasetTabs: thus.getOrderedDatasetPagesAndFields(),
link_to_dataset: thus.tuix.link_to_dataset
};
var html = thus.microTemplate('zenario_organizer_form_builder_left_panel', mergeFields);
var $div = $('#organizer_form_builder .form_fields_palette .form_fields_palette_outer').html(html);
if (!stopEffect) {
$div.hide().show('drop', {direction: 'right'}, 200);
}
//Add events
//Allow fields to be dragged onto list
$('#organizer_field_type_list div.field_type, #organizer_centralised_field_type_list div.field_type, #organizer_linked_field_type_list div.dataset_field').draggable({
connectToSortable: '#organizer_form_fields .form_section',
appendTo: '#organizer_form_builder',
helper: 'clone'
});
//Edit form settings
$('input.form_settings').on('click', function() {
thus.openFormSettings();
});
};
methods.loadPagesList = function(pageId) {
foreach (thus.tuix.items as var tPageId => var page) {
if (pageId == tPageId) {
page._selected = true;
} else {
delete(page._selected);
}
}
//Only show page pages if there is at least 2
var pages = thus.getOrderedPages();
if (pages.length <= 1) {
pages = false;
}
//Load HTML
var mergeFields = {pages: pages};
var html = thus.microTemplate('zenario_organizer_form_builder_tabs', mergeFields);