Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 1005 | Rev 1007 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1005 Rev 1006
Línea 56... Línea 56...
56
// JsRender //
56
// JsRender //
57
$this->inlineScript()->appendFile($this->basePath('plugins/jsrender/jsrender.min.js'));
57
$this->inlineScript()->appendFile($this->basePath('plugins/jsrender/jsrender.min.js'));
Línea 58... Línea 58...
58
 
58
 
59
$this->inlineScript()->captureStart();
59
$this->inlineScript()->captureStart();
60
echo <<<JS
-
 
61
    const classFormGenerator = function() {
-
 
62
    this.id = 0,
-
 
63
        this.table = '',
-
 
64
        this.name = '',
-
 
65
        this.text = '',
-
 
66
        this.status = 'a',
-
 
67
        this.sections = [],
-
 
68
        this.clear = function() {
-
 
69
            this.sections = [];
-
 
70
            this.render();
-
 
71
        },
-
 
72
        /**
-
 
73
         * Render array sections
-
 
74
         */
-
 
75
        this.renderSection = function(slug_section) {
-
 
76
            var s = '';
-
 
77
            for (i = 0; i < this.sections.length; i++) {
-
 
78
                if (slug_section != this.sections[i].slug_section) {
-
 
79
                    continue;
-
 
80
                }
-
 
81
                for (j = 0; j < this.sections[i].sections.length; j++) {
-
 
82
                    this.sections[i].sections[j].position = j;
-
 
83
                    if (this.sections[i].sections[j].type == 'simple' || this.sections[i].sections[j].type == 'multiple' || this.sections[i].sections[j].type == 'rating-open') {
-
 
84
                        this.sections[i].sections[j].options.sort(function(a, b) {
-
 
85
                            if (a.position > b.position) {
-
 
86
                                return 1;
-
 
87
                            }
-
 
88
                            if (a.position < b.position) {
-
 
89
                                return -1;
-
 
90
                            }
-
 
91
                            return 0;
-
 
92
                        });
-
 
93
                        for (k = 0; k < this.sections[i].sections[j].options.length; k++) {
-
 
94
                            this.sections[i].sections[j].options[k].position = j;
-
 
95
                        }
-
 
96
                    }
-
 
97
                }
-
 
98
            }
-
 
99
            $('[data-toggle="tooltip"]').tooltip();
-
 
100
        },
-
 
101
        this.render = function() {
-
 
102
            this.sections.sort(function(a, b) {
-
 
103
                if (a.position > b.position) {
-
 
104
                    return 1;
-
 
105
                }
-
 
106
                if (a.position < b.position) {
-
 
107
                    return -1;
-
 
108
                }
-
 
109
                return 0;
-
 
110
            });
-
 
111
            var s = '';
-
 
112
            for (i = 0; i < this.sections.length; i++) {
-
 
113
                this.sections[i].position = i;
-
 
114
                this.sections[i].sections.sort(function(a, b) {
-
 
115
                    if (a.position > b.position) {
-
 
116
                        return 1;
-
 
117
                    }
-
 
118
                    if (a.position < b.position) {
-
 
119
                        return -1;
-
 
120
                    }
-
 
121
                    return 0;
-
 
122
                });
-
 
123
                for (j = 0; j < this.sections[i].sections.length; j++) {
-
 
124
                    this.sections[i].sections[j].position = j;
-
 
125
                    if (this.sections[i].sections[j].type == 'simple' || this.sections[i].sections[j].type == 'multiple' || this.sections[i].sections[j].type == 'rating-open') {
-
 
126
                        this.sections[i].sections[j].options.sort(function(a, b) {
-
 
127
                            if (a.position > b.position) {
-
 
128
                                return 1;
-
 
129
                            }
-
 
130
                            if (a.position < b.position) {
-
 
131
                                return -1;
-
 
132
                            }
-
 
133
                            return 0;
-
 
134
                        });
-
 
135
                        for (k = 0; k < this.sections[i].sections[j].options.length; k++) {
-
 
136
                            this.sections[i].sections[j].options[k].position = j;
-
 
137
                        }
-
 
138
                    }
-
 
139
                }
-
 
140
            }
-
 
141
            $('[data-toggle="tooltip"]').tooltip();
-
 
142
        },
-
 
143
        /**
-
 
144
         * Add element to section array 
-
 
145
         */
-
 
146
        this.addSection = function(name, text, value) {
-
 
147
            var d = new Date();
-
 
148
            var slug = 'section' + d.getTime();
-
 
149
            var position = 0;
-
 
150
            $.each(this.sections, function(index, section) {
-
 
151
                if (position < section.position) {
-
 
152
                    position = section.position;
-
 
153
                }
-
 
154
            });
-
 
155
            position++;
-
 
156
            var section = {
-
 
157
                'slug_section': slug,
-
 
158
                'name': name,
-
 
159
                'text': text,
-
 
160
                'value': value,
-
 
161
                'position': position,
-
 
162
                'sections': [],
-
 
163
                'status': 0
-
 
164
            }
-
 
165
            this.sections.push(section);
-
 
166
        },
-
 
167
         /**
-
 
168
         * Edit element to section array 
-
 
169
         */
-
 
170
        this.editSection = function(slug, name, text, value) {
-
 
171
            var renderTable = false;
-
 
172
            for (i = 0; i < this.sections.length; i++) {
-
 
173
                if (slug == this.sections[i].slug_section) {
-
 
174
                    this.sections[i].name = name;
-
 
175
                    this.sections[i].text = text;
-
 
176
                    this.sections[i].value = value;
-
 
177
                    renderTable = true;
-
 
178
                    break;
-
 
179
                }
-
 
180
            }
-
 
181
            if (renderTable) {
-
 
182
                this.renderSection(slug);
-
 
183
            }
-
 
184
        },
-
 
185
         /**
-
 
186
         * Remove element to section array
-
 
187
         */
-
 
188
        this.deleteSection = function(slug) {
-
 
189
            var renderTable = false;
-
 
190
            for (i = 0; i < this.sections.length; i++) {
-
 
191
                if (slug == this.sections[i].slug_section) {
-
 
192
                    this.sections.splice(i, 1);
-
 
193
                    renderTable = true;
-
 
194
                    break;
-
 
195
                }
-
 
196
            }
-
 
197
            if (renderTable) {
-
 
198
                $('#panel' + slug).remove();
-
 
199
            }
-
 
200
        },
-
 
201
         /**
-
 
202
         * Add element to section array
-
 
203
         */
-
 
204
        this.addsection = function(slug_section, text, value, type, maxlength, multiline, range) {
-
 
205
            var d = new Date();
-
 
206
            var slug_section = 'section' + d.getTime();
-
 
207
            var position = 0;
-
 
208
            var renderTable = false;
-
 
209
            for (i = 0; i < this.sections.length; i++) {
-
 
210
                if (slug_section == this.sections[i].slug_section) {
-
 
211
                    $.each(this.sections[i].sections, function(index, section) {
-
 
212
                        if (position < section.position) {
-
 
213
                            position = section.position;
-
 
214
                        }
-
 
215
                    });
-
 
216
                    position++;
-
 
217
                    var section = {
-
 
218
                        'slug_section': slug_section,
-
 
219
                        'slug_section': slug_section,
-
 
220
                        'text': text,
-
 
221
                        'value': value,
-
 
222
                        'type': type,
-
 
223
                        'position': position,
-
 
224
                        'maxlength': maxlength,
-
 
225
                        'multiline': multiline,
-
 
226
                        'range': range,
-
 
227
                        'options': [],
-
 
228
                        'answer': type=='multiple' ? [] : ''
-
 
229
                    }
-
 
230
                    this.sections[i].sections.push(section);
-
 
231
                    renderTable = true;
-
 
232
                    break;
-
 
233
                }
-
 
234
            }
-
 
235
            if (renderTable) {
-
 
236
                this.renderSection(slug_section);
-
 
237
            }
-
 
238
        },
-
 
239
         /**
-
 
240
         * Add element to section array
-
 
241
         */
-
 
242
        this.editsection = function(slug_section, slug_section, text, value, type, maxlength, multiline, range) {
-
 
243
            var renderTable = false;
-
 
244
            for (i = 0; i < this.sections.length; i++) {
-
 
245
                if (slug_section == this.sections[i].slug_section) {
-
 
246
                    for (j = 0; j < this.sections[i].sections.length; j++) {
-
 
247
                        if (slug_section == this.sections[i].sections[j].slug_section) {
-
 
248
                            this.sections[i].sections[j].text = text,
-
 
249
                                this.sections[i].sections[j].value = value,
-
 
250
                                this.sections[i].sections[j].type = type;
-
 
251
                            if (type == 'open') {
-
 
252
                                this.sections[i].sections[j].maxlength = maxlength;
-
 
253
                                this.sections[i].sections[j].multiline = multiline;
-
 
254
                                this.sections[i].sections[j].options = [];
-
 
255
                            } else {
-
 
256
                                this.sections[i].sections[j].maxlength = 0;
-
 
257
                                this.sections[i].sections[j].multiline = 0;
-
 
258
                            }
-
 
259
                            if (type == 'rating-range') {
-
 
260
                                this.sections[i].sections[j].range = range;
-
 
261
                            } else {
-
 
262
                                this.sections[i].sections[j].range = 0;
-
 
263
                            }
60
echo <<<JS
264
 
-
 
265
                            if(type=='multiple'){
-
 
266
                                this.sections[i].sections[j].answer = [];  
-
 
267
                            }else{
-
 
268
                                this.sections[i].sections[j].answer = '';  
-
 
269
                            }
-
 
270
 
-
 
271
                            renderTable = true;
-
 
272
                            break;
-
 
273
                        }
-
 
274
                    }
-
 
275
                }
-
 
276
                if (renderTable) {
-
 
277
                    break;
-
 
278
                }
-
 
279
            }
-
 
280
            if (renderTable) {
-
 
281
                this.renderSection(slug_section);
-
 
282
            }
-
 
283
        },
-
 
284
        /**
-
 
285
         * Remove element to section array
-
 
286
         */
-
 
287
        this.deletesection = function(slug_section, slug_section) {
-
 
288
            var renderTable = false;
-
 
289
            for (i = 0; i < this.sections.length; i++) {
-
 
290
                if (slug_section == this.sections[i].slug_section) {
-
 
291
                    for (j = 0; j < this.sections[i].sections.length; j++) {
-
 
292
                        if (slug_section == this.sections[i].sections[j].slug_section) {
-
 
293
                            this.sections[i].sections.splice(j, 1);
-
 
294
                            renderTable = true;
-
 
295
                            break;
-
 
296
                        }
-
 
297
                    }
-
 
298
                }
-
 
299
                if (renderTable) {
-
 
300
                    break;
-
 
301
                }
-
 
302
            }
-
 
303
            if (renderTable) {
-
 
304
                this.renderSection(slug_section);
-
 
305
            }
-
 
306
        },
-
 
307
        /**
-
 
308
         * Add element to option array
-
 
309
         */
-
 
310
        this.addOption = function(slug_section, slug_section, text, correct, value) {
-
 
311
            var d = new Date();
-
 
312
            var slug_option = 'option' + d.getTime();
-
 
313
            var position = 0;
-
 
314
            var renderTable = false;
-
 
315
            for (i = 0; i < this.sections.length; i++) {
-
 
316
                if (slug_section == this.sections[i].slug_section) {
-
 
317
                    for (j = 0; j < this.sections[i].sections.length; j++) {
-
 
318
                        if (slug_section == this.sections[i].sections[j].slug_section) {
-
 
319
                            $.each(this.sections[i].sections[j].options, function(index, option) {
-
 
320
                                if (position < option.position) {
-
 
321
                                    position = option.position;
-
 
322
                                }
-
 
323
                            });
-
 
324
                            position++;
-
 
325
                            var option = {
-
 
326
                                'slug_section': slug_section,
-
 
327
                                'slug_section': slug_section,
-
 
328
                                'slug_option': slug_option,
-
 
329
                                'text': text,
-
 
330
                                'correct': correct,
-
 
331
                                'value': value,
-
 
332
                                'checked': false
-
 
333
                            }
-
 
334
                            this.sections[i].sections[j].options.push(option);
-
 
335
                            renderTable = true;
-
 
336
                            break;
-
 
337
                        }
-
 
338
                        if (renderTable) {
-
 
339
                            break;
-
 
340
                        }
-
 
341
                    }
-
 
342
                }
-
 
343
            }
-
 
344
            if (renderTable) {
-
 
345
                this.renderSection(slug_section);
-
 
346
            }
-
 
347
        },
-
 
348
        /**
-
 
349
         * Edit element to option array
-
 
350
         */
-
 
351
        this.editOption = function(slug_section, slug_section, option_slug, text, correct, value) {
-
 
352
            var renderTable = false;
-
 
353
            for (i = 0; i < this.sections.length; i++) {
-
 
354
                if (slug_section == this.sections[i].slug_section) {
-
 
355
                    for (j = 0; j < this.sections[i].sections.length; j++) {
-
 
356
                        if (slug_section == this.sections[i].sections[j].slug_section) {
-
 
357
                            for (k = 0; k < this.sections[i].sections[j].options.length; k++) {
-
 
358
                                if (option_slug == this.sections[i].sections[j].options[k].slug_option) {
-
 
359
                                    this.sections[i].sections[j].options[k].text = text;
-
 
360
                                    this.sections[i].sections[j].options[k].correct = correct;
-
 
361
                                    this.sections[i].sections[j].options[k].value = value;
-
 
362
                                    renderTable = true;
-
 
363
                                    break;
-
 
364
                                }
-
 
365
                            }
-
 
366
                        }
-
 
367
                        if (renderTable) {
-
 
368
                            break;
-
 
369
                        }
-
 
370
                    }
-
 
371
                }
-
 
372
                if (renderTable) {
-
 
373
                    break;
-
 
374
                }
-
 
375
            }
-
 
376
            if (renderTable) {
-
 
377
                this.renderSection(slug_section);
-
 
378
            }
-
 
379
        },
-
 
380
        /**
-
 
381
         * Remove element to option array
-
 
382
         */
-
 
383
        this.deleteOption = function(slug_section, slug_section, option_slug) {
-
 
384
            var renderTable = false;
-
 
385
            for (i = 0; i < this.sections.length; i++) {
-
 
386
                if (slug_section == this.sections[i].slug_section) {
-
 
387
                    for (j = 0; j < this.sections[i].sections.length; j++) {
-
 
388
                        if (slug_section == this.sections[i].sections[j].slug_section) {
-
 
389
                            for (k = 0; k < this.sections[i].sections[j].options.length; k++) {
-
 
390
                                if (option_slug == this.sections[i].sections[j].options[k].slug_option) {
-
 
391
                                    this.sections[i].sections[j].options.splice(k, 1);
-
 
392
                                    renderTable = true;
-
 
393
                                    break;
-
 
394
                                }
-
 
395
                            }
-
 
396
                        }
-
 
397
                        if (renderTable) {
-
 
398
                            break;
-
 
399
                        }
-
 
400
                    }
-
 
401
                }
-
 
402
                if (renderTable) {
-
 
403
                    break;
-
 
404
                }
-
 
405
            }
-
 
406
            if (renderTable) {
-
 
407
                this.renderSection(slug_section);
-
 
408
            }
-
 
409
        }
-
 
410
}
-
 
411
 
-
 
412
function htmlEntities(str) {
-
 
413
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
-
 
414
}
61
   
415
jQuery(document).ready(function($) {
-
 
416
    var objFormGenerator = new classFormGenerator();
-
 
-
 
62
jQuery(document).ready(function($) {
417
    objFormGenerator.render();
63
 
418
    var allowEdit = $allowEdit;
64
    var allowEdit = $allowEdit;
419
    var allowDelete = $allowDelete;
65
    var allowDelete = $allowDelete;
420
    /**
66
    /**
421
     * Get rows and set data table
67
     * Get rows and set data table
Línea 570... Línea 216...
570
            'accept': 'application/json',
216
            'accept': 'application/json',
571
            'method': 'get',
217
            'method': 'get',
572
            'url': action,
218
            'url': action,
573
        }).done(function(response) {
219
        }).done(function(response) {
574
            if (response['success']) {
220
            if (response['success']) {
575
                validatorForm.resetForm();
-
 
-
 
221
 
576
                $('#form-main').attr('action', action);
222
                $('#form-main').attr('action', action);
577
                $('#form-main #form-id').val(response['data']['id']),
223
                $('#form-main #form-id').val(response['data']['id']),
578
                $('#form-main #form-continue').val('0');
224
                $('#form-main #form-continue').val('0');
579
                $('#form-main #form-name').val(response['data']['name']),
225
                $('#form-main #form-name').val(response['data']['name']),
Línea 585... Línea 231...
585
                $('#form-main #form-language').val(response['data']['language']),
231
                $('#form-main #form-language').val(response['data']['language']),
586
                $('#form-main #form-status').val(response['data']['status']);
232
                $('#form-main #form-status').val(response['data']['status']);
Línea 587... Línea 233...
587
 
233
 
Línea 588... Línea 234...
588
                /*-------------Render Sections -------*/
234
                /*-------------Render Sections -------*/
589
                
-
 
590
                objFormGenerator.clear();
-
 
591
                objFormGenerator.sections = response['data']['content'] || [];
-
 
Línea 592... Línea 235...
592
                objFormGenerator.render();
235
                
593
                renderSectionData(objFormGenerator.sections);
236
                renderSectionData();
594
                
237
                
Línea 601... Línea 244...
601
            }
244
            }
602
        }).fail(function(jqXHR, textStatus, errorThrown) {
245
        }).fail(function(jqXHR, textStatus, errorThrown) {
603
            $.fn.showError(textStatus);
246
            $.fn.showError(textStatus);
604
        });
247
        });
605
    });
248
    });
606
    $(document).on('click', '[data-type="select_all"]', function() {
-
 
607
        if ($('input[name="select_all"]:checked').val() == 'all') {
-
 
608
            $('[data-type="select"]').prop('checked', true);
-
 
609
            $('[data-action="delete"]').removeClass('hide');
-
 
610
        } else {
-
 
611
            $('[data-type="select"]').prop('checked', false);
-
 
612
            $('[data-action="delete"]').addClass('hide');
-
 
613
        }
-
 
614
    });
-
 
615
 
249
   
616
    function getLength() {
-
 
617
        return $('[data-type="select"]').length;
-
 
618
    }
-
 
619
 
-
 
620
    function currentSelected() {
-
 
621
        var selected = [];
-
 
622
        $.each($("input[name='select[]']:checked"), function() {
-
 
623
            selected.push($(this).val());
-
 
624
        });
-
 
625
        return selected;
-
 
626
    }
-
 
627
    $(document).on('change', "input[name='select[]']", function() {
-
 
628
        var c = currentSelected();
-
 
629
        var cc = getLength();
-
 
630
        if (c.length == cc) {
-
 
631
            if ($('[data-action="delete"]').hasClass('hide')) {
-
 
632
                $('[data-action="delete"]').removeClass('hide');
-
 
633
            }
-
 
634
            return true;
-
 
635
        } else {
-
 
636
            $('input[name="select_all"]').prop('checked', false);
-
 
637
        }
-
 
638
        if (c.length > 0) {
-
 
639
            if ($('[data-action="delete"]').hasClass('hide')) {
-
 
640
                $('[data-action="delete"]').removeClass('hide');
-
 
641
            }
-
 
642
        } else {
-
 
643
            if (!$('[data-action="delete"]').hasClass('hide')) {
-
 
644
                $('[data-action="delete"]').addClass('hide');
-
 
645
            }
-
 
646
        }
-
 
647
    });
-
 
648
    var form3 = $('#form_sample_3');
-
 
649
    var error3 = $('.alert-danger', form3);
-
 
650
    var success3 = $('.alert-success', form3);
-
 
651
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
-
 
652
    $("#form-main").on('submit', function() {
-
 
653
        for (var instanceName in CKEDITOR.instances) {
-
 
654
            CKEDITOR.instances[instanceName].updateElement();
-
 
655
        }
-
 
656
    })
-
 
657
    /**
-
 
658
     * Validate rules form 
-
 
659
     */
-
 
660
    var validatorForm = $("#form-main").validate({
-
 
661
        ignore: [],
-
 
662
        errorClass: 'help-block',
-
 
663
        errorElement: 'span',
-
 
664
        rules: {
-
 
665
            'form-name': {
-
 
666
                required: true,
-
 
667
                minlength: 2,
-
 
668
                maxlength: 50
-
 
669
            },
-
 
670
            'form-description': {
-
 
671
                required: true,
-
 
672
            },
-
 
673
            'form-text': {
-
 
674
                required: true,
-
 
675
            },
-
 
676
            'form-language': {
-
 
677
                required: true,
-
 
678
            },
-
 
679
            'forrm-status': {
-
 
680
                required: true,
-
 
681
            },
-
 
682
        },
-
 
683
        highlight: function(element) {
-
 
684
            $(element).closest('.form-group').addClass('has-error');
-
 
685
        },
-
 
686
        unhighlight: function(element) {
-
 
687
            $(element).closest('.form-group').removeClass('has-error');
-
 
688
        },
-
 
689
        errorPlacement: function(error, element) {
-
 
690
            if (element.attr("data-error-container")) {
-
 
691
                error.appendTo(element.attr("data-error-container"));
-
 
692
            } else {
-
 
693
                error.insertAfter(element);
-
 
694
            }
-
 
695
        },
-
 
696
        invalidHandler: function(form, validator) {
-
 
697
            if (!validator.numberOfInvalids())
-
 
698
                return;
-
 
699
            $('html, body').animate({
-
 
700
                scrollTop: $(validator.errorList[0].element).offset().top - 100
-
 
701
            }, 1000);
-
 
702
        },
-
 
703
        submitHandler: function(form) {
-
 
704
            var error = false;
-
 
705
            if (objFormGenerator.sections.length == 0) {
-
 
706
                $.fn.showError('ERROR_SECCTIONS');
-
 
707
                return false;
-
 
708
            } else {
-
 
709
                for (i = 0; i < objFormGenerator.sections.length; i++) {
-
 
710
                    if (objFormGenerator.sections[i].sections.length == 0) {
-
 
711
                        $.fn.showError('ERROR_sectionS'.replace('%s', objFormGenerator.sections[i].name));
-
 
712
                        return false;
-
 
713
                    }
-
 
714
                    var valueSection = parseInt(objFormGenerator.sections[i].value);
-
 
715
                    var totalValuesection = 0;
-
 
716
                    for (j = 0; j < objFormGenerator.sections[i].sections.length; j++) {
-
 
717
                        valuesection = parseInt(objFormGenerator.sections[i].sections[j].value);
-
 
718
                        totalValuesection = totalValuesection + valuesection;
-
 
719
                        if (objFormGenerator.sections[i].sections[j].type == 'simple' ||
-
 
720
                            objFormGenerator.sections[i].sections[j].type == 'multiple' ||
-
 
721
                            objFormGenerator.sections[i].sections[j].type == 'rating-open') {
-
 
722
                            var sectionNumber = j + 1;
-
 
723
                            var numberCorrect = 0;
-
 
724
                            if (objFormGenerator.sections[i].sections[j].options.length == 0) {
-
 
725
                                $.fn.showError('ERROR_OPTIONS'.replace('%s', objFormGenerator.sections[i].name).replace('%n', sectionNumber));
-
 
726
                                return false;
-
 
727
                            }
-
 
728
                            var totalOption = 0;
-
 
729
                            var maxOption = 0;
-
 
730
                            for (k = 0; k < objFormGenerator.sections[i].sections[j].options.length; k++) {
-
 
731
                                if (objFormGenerator.sections[i].sections[j].type == 'simple' || objFormGenerator.sections[i].sections[j].type == 'multiple') {
-
 
732
                                    if (objFormGenerator.sections[i].sections[j].options[k].correct == 1) {
-
 
733
                                        numberCorrect++;
-
 
734
                                    }
-
 
735
                                }
-
 
736
                                if (objFormGenerator.sections[i].sections[j].type == 'multiple' && objFormGenerator.sections[i].sections[j].options[k].correct == 1) {
-
 
737
                                    totalOption = totalOption + parseInt(objFormGenerator.sections[i].sections[j].options[k].value);
-
 
738
                                }
-
 
739
                                if (objFormGenerator.sections[i].sections[j].type == 'rating-open') {
-
 
740
                                    if (parseInt(objFormGenerator.sections[i].sections[j].options[k].value) > maxOption) {
-
 
741
                                        maxOption = parseInt(objFormGenerator.sections[i].sections[j].options[k].value);
-
 
742
                                    }
-
 
743
                                }
-
 
744
                            }
-
 
745
                            if (objFormGenerator.sections[i].sections[j].type == 'simple' || objFormGenerator.sections[i].sections[j].type == 'multiple') {
-
 
746
                                if (numberCorrect == 0) {
-
 
747
                                    $.fn.showError('ERROR_OPTIONS_CORRECT'.replace('%s', objFormGenerator.sections[i].name).replace('%n', sectionNumber));
-
 
748
                                    return false;
-
 
749
                                }
-
 
750
                                if (
-
 
751
                                    objFormGenerator.sections[i].sections[j].type == 'simple' && numberCorrect > 1) {
-
 
752
                                    $.fn.showError('ERROR_OPTIONS_DUPLICATE_CORRECT'.replace('%s', objFormGenerator.sections[i].name).replace('%n', sectionNumber));
-
 
753
                                    return false;
-
 
754
                                }
-
 
755
                                if (objFormGenerator.sections[i].sections[j].type == 'multiple' && numberCorrect == 1) {
-
 
756
                                    $.fn.showError('ERROR_OPTIONS_ONE_CORRECT'.replace('%s', objFormGenerator.sections[i].name).replace('%n', sectionNumber));
-
 
757
                                    return false;
-
 
758
                                }
-
 
759
                            }
-
 
760
                            if (objFormGenerator.sections[i].sections[j].type == 'multiple' && totalOption != valuesection) {
-
 
761
                                $.fn.showError('ERROR_OPTIONS_SUM_VALUES'.replace('%s', objFormGenerator.sections[i].name).replace('%n', sectionNumber));
-
 
762
                                return false;
-
 
763
                            }
-
 
764
                            if (objFormGenerator.sections[i].sections[j].type == 'rating-open' && maxOption > valuesection) {
-
 
765
                                $.fn.showError('ERROR_OPTIONS_MAX_OPTION'.replace('%s', objFormGenerator.sections[i].name).replace('%n', sectionNumber));
-
 
766
                                return false;
-
 
767
                            }
-
 
768
                        }
-
 
769
                    }
-
 
770
                    if (valueSection != totalValuesection) {
-
 
771
                        $.fn.showError('ERROR_VALUE_SECTIONS'.replace('%s', objFormGenerator.sections[i].name));
-
 
772
                        return false;
-
 
773
                    }
-
 
774
                }
-
 
775
                var formId = parseInt($('#form-main #form-id').val());
-
 
776
                var formContinue = parseInt($('#form-main #form-continue').val());
-
 
777
                var data = {
-
 
778
                    'id': formId,
-
 
779
                    'name': $('#form-main #form-name').val(),
-
 
780
                    'description': $('#form-main #form-description').val(),
-
 
781
                    'text': $('#form-main #form-text').val(),
-
 
782
                    'language': $('#form-main #form-language').val(),
-
 
783
                    'status': $('#form-main #form-status').val(),
-
 
784
                    'content': JSON.stringify(objFormGenerator.sections)
-
 
785
                }
-
 
786
                $.ajax({
-
 
787
                    'dataType': 'json',
-
 
788
                    'method': 'post',
-
 
789
                    'url': $('#form-main').attr('action'),
-
 
790
                    'data': data,
-
 
791
                }).done(function(response) {
-
 
792
                    if (response['success']) {
-
 
793
                        $.fn.showSuccess(response['data']);
-
 
794
                        if (formContinue == 1) {
-
 
795
                            $('#form-main').attr('action',response['action_edit']);
-
 
796
                            $('#form-main #form-id').val(response['id']);
-
 
797
                            $('#form-main #form-continue').val(0);
-
 
798
                        } else {
-
 
799
                            $('#row-form').hide();
-
 
800
                            $('#row-lists').show();
-
 
801
                        /*---------- Reset Form -------- */
-
 
802
                        $('#form-main')[0].reset();
-
 
803
                        /*--------Reset Ckeditor ----------*/
-
 
804
                        CKEDITOR.instances['form-text'].setData('');
-
 
805
                        CKEDITOR.instances['form-description'].setData('');
-
 
806
                        /*--------Reset Sections ----------*/
-
 
807
                        clearSectionData();
-
 
808
                        /* ------- Refresh Table -----------*/
-
 
Línea 809... Línea -...
809
 
-
 
810
                        }
-
 
811
                        tableForm.fnDraw();
-
 
812
                    } else {
-
 
813
                        $.fn.showError(response['message'] || 'ERROR_UNKNOWN');
-
 
814
                    }
-
 
815
                }).fail(function(jqXHR, textStatus, errorThrown) {
-
 
816
                    $.fn.showError(textStatus);
-
 
817
                });
-
 
818
                return false;
-
 
819
            }
-
 
820
        }
-
 
821
    });
-
 
822
    /**
-
 
823
     * Validate rules form sections
-
 
824
     */
-
 
825
    var validatorFormsection = $("#form-section").validate({
-
 
826
        ignore: [],
-
 
827
        errorClass: 'help-block',
-
 
828
        errorElement: 'span',
-
 
829
        rules: {
-
 
830
            'section-text': {
-
 
831
                required: true,
-
 
832
            },
-
 
833
            'section-value': {
-
 
834
                required: true,
-
 
835
                number: true,
-
 
836
                min: 1
-
 
837
            },
-
 
838
            'section-type': {
-
 
839
                required: true,
-
 
840
            },
-
 
841
            'section-max-length': {
-
 
842
                required: true,
-
 
843
                digits: true,
-
 
844
                min: 0
-
 
845
            },
-
 
846
            'section-range': {
-
 
847
                required: true,
-
 
848
                number: true,
-
 
849
                min: 1
-
 
850
            },
-
 
851
        },
-
 
852
        highlight: function(element) {
-
 
853
            $(element).closest('.form-group').addClass('has-error');
-
 
854
        },
-
 
855
        unhighlight: function(element) {
-
 
856
            $(element).closest('.form-group').removeClass('has-error');
-
 
857
        },
-
 
858
        errorPlacement: function(error, element) {
-
 
859
            if (element.attr("data-error-container")) {
-
 
860
                error.appendTo(element.attr("data-error-container"));
-
 
861
            } else {
-
 
862
                error.insertAfter(element);
-
 
863
            }
-
 
864
        },
-
 
865
        invalidHandler: function(form, validator) {
-
 
866
            if (!validator.numberOfInvalids())
-
 
867
                return;
-
 
868
            $('html, body').animate({
-
 
869
                scrollTop: $(validator.errorList[0].element).offset().top - 100
-
 
870
            }, 1000);
-
 
871
        },
-
 
872
        submitHandler: function(form) {
-
 
873
            if ($('#form-section #section-id').val()) {
-
 
874
                objFormGenerator.editsection(
-
 
875
                    $('#form-section #section-section').val(),
-
 
876
                    $('#form-section #section-id').val(),
-
 
877
                    $('#form-section #section-text').val(),
-
 
878
                    $('#form-section #section-value').val(),
-
 
879
                    $('#form-section #section-type').val(),
-
 
880
                    $('#form-section #section-max-length').val(),
-
 
881
                    $('#form-section #section-multiline').val(),
-
 
882
                    $('#form-section #section-range').val()
-
 
883
                );
-
 
884
            } else {
-
 
885
                objFormGenerator.addsection(
-
 
886
                    $('#form-section #section-section').val(),
-
 
887
                    $('#form-section #section-text').val(),
-
 
888
                    $('#form-section #section-value').val(),
-
 
889
                    $('#form-section #section-type').val(),
-
 
890
                    $('#form-section #section-max-length').val(),
-
 
891
                    $('#form-section #section-multiline').val(),
-
 
892
                    $('#form-section #section-range').val()
-
 
893
                );
-
 
894
            }
-
 
895
            renderSectionData(objFormGenerator.sections);
-
 
896
            $('#modal-section').modal('hide');
-
 
897
            return false;
-
 
898
        }
-
 
899
    });
-
 
900
    //IMPORTANT: update CKEDITOR textarea with actual content before submit
-
 
901
    $("#form-option").on('submit', function() {
-
 
902
        for (var instanceName in CKEDITOR.instances) {
-
 
903
            CKEDITOR.instances[instanceName].updateElement();
-
 
904
        }
-
 
905
    })
-
 
906
    /**
-
 
907
     * Validate rules form options
-
 
908
     */
-
 
909
    var validatorFormOption = $("#form-option").validate({
-
 
910
        ignore: [],
-
 
911
        errorClass: 'help-block',
-
 
912
        errorElement: 'span',
-
 
913
        rules: {
-
 
914
            'option-text': {
-
 
915
                required: true,
-
 
916
            },
-
 
917
            'option-value': {
-
 
918
                required: true,
-
 
919
                number: true,
-
 
920
                min: 1
-
 
921
            }
-
 
922
        },
-
 
923
        highlight: function(element) {
-
 
924
            $(element).closest('.form-group').addClass('has-error');
-
 
925
        },
-
 
926
        unhighlight: function(element) {
-
 
927
            $(element).closest('.form-group').removeClass('has-error');
-
 
928
        },
-
 
929
        errorPlacement: function(error, element) {
-
 
930
            if (element.attr("data-error-container")) {
-
 
931
                error.appendTo(element.attr("data-error-container"));
-
 
932
            } else {
-
 
933
                error.insertAfter(element);
-
 
934
            }
-
 
935
        },
-
 
936
        invalidHandler: function(form, validator) {
-
 
937
            if (!validator.numberOfInvalids())
-
 
938
                return;
-
 
939
            $('html, body').animate({
-
 
940
                scrollTop: $(validator.errorList[0].element).offset().top - 100
-
 
941
            }, 1000);
-
 
942
        },
-
 
943
        submitHandler: function(form) {
-
 
944
            if ($('#form-option #option-slug').val()) {
-
 
945
                objFormGenerator.editOption(
-
 
946
                    $('#form-option #option-section').val(),
-
 
947
                    $('#form-option #option-section').val(),
-
 
948
                    $('#form-option #option-slug').val(),
-
 
949
                    $('#form-option #option-text').val(),
-
 
950
                    $('#form-option #option-correct').val(),
-
 
951
                    $('#form-option #option-value').val()
-
 
952
                );
-
 
953
            } else {
-
 
954
                objFormGenerator.addOption(
-
 
955
                    $('#form-option #option-section').val(),
-
 
956
                    $('#form-option #option-section').val(),
-
 
957
                    $('#form-option #option-text').val(),
-
 
958
                    $('#form-option #option-correct').val(),
-
 
959
                    $('#form-option #option-value').val()
-
 
960
                );
-
 
961
            }
-
 
962
            renderSectionData(objFormGenerator.sections);
-
 
963
            $('#modal-option').modal('hide');
-
 
964
            return false;
-
 
965
        }
-
 
966
    });
250
 
967
    /**
251
    /**
968
     * Clicked add new section
252
     * Clicked add new section
969
     */
253
     */
970
    $('#btn-add-section').click(function(e) {
254
    $('#btn-add-section').click(function(e) {
Línea 1005... Línea 289...
1005
                    renderSectionData(objFormGenerator.sections);
289
                    renderSectionData(objFormGenerator.sections);
1006
                }
290
                }
1007
            }
291
            }
1008
        });
292
        });
1009
    });
293
    });
1010
    /**
-
 
1011
     * Clicked add new Option
-
 
1012
     */
-
 
1013
    $('body').on('click', 'button.btn-add-option', function(e) {
-
 
1014
        e.preventDefault();
-
 
1015
        var slug_section = $(this).data('section');
-
 
1016
        var slug_section = $(this).data('section');
-
 
1017
        var showForm = false;
-
 
1018
        for (i = 0; i < objFormGenerator.sections.length; i++) {
-
 
1019
            if (slug_section == objFormGenerator.sections[i].slug_section) {
-
 
1020
                for (j = 0; j < objFormGenerator.sections[i].sections.length; j++) {
-
 
1021
                    if (slug_section == objFormGenerator.sections[i].sections[j].slug_section) {
-
 
1022
                        validatorFormOption.resetForm();
-
 
1023
                        $('#form-option #option-section').val(slug_section);
-
 
1024
                        $('#form-option #option-section').val(slug_section);
-
 
1025
                        $('#form-option #option-slug').val('');
-
 
1026
                        CKEDITOR.instances['option-text'].setData('', function() {
-
 
1027
                            editor.focus();
-
 
1028
                        });
-
 
1029
                        $('#form-option #option-correct').val('0');
-
 
1030
                        if (objFormGenerator.sections[i].sections[j].type == 'rating-open') {
-
 
1031
                            $('#form-option #option-correct').parent().hide();
-
 
1032
                        } else {
-
 
1033
                            $('#form-option #option-correct').parent().show();
-
 
1034
                        }
-
 
1035
                        if (objFormGenerator.sections[i].sections[j].type == 'multiple' || objFormGenerator.sections[i].sections[j].type == 'rating-open') {
-
 
1036
                            $('#form-option #option-value').val('0');
-
 
1037
                            $('#form-option #option-value').parent().show();
-
 
1038
                        } else {
-
 
1039
                            $('#form-option #option-value').val('1');
-
 
1040
                            $('#form-option #option-value').parent().hide();
-
 
1041
                        }
-
 
1042
                        renderSectionData(objFormGenerator.sections);
-
 
1043
                        $('#modal-option h4[class="modal-title"]').html('LABEL_ADD LABEL_OPTION');
-
 
1044
                        $('#modal-option').modal('show');
-
 
1045
                        return true;
-
 
1046
                    }
-
 
1047
                }
-
 
1048
            }
-
 
1049
        }
-
 
1050
    });
-
 
1051
    /**
-
 
1052
     * Clicked edit option
-
 
1053
     */
-
 
1054
    $('body').on('click', 'button.btn-edit-option', function(e) {
-
 
1055
        e.preventDefault();
-
 
1056
        var slug_section = $(this).data('section');
-
 
1057
        var slug_section = $(this).data('section');
-
 
1058
        var slug = $(this).data('slug');
-
 
1059
        var showForm = false;
-
 
1060
        for (i = 0; i < objFormGenerator.sections.length; i++) {
-
 
1061
            if (slug_section == objFormGenerator.sections[i].slug_section) {
-
 
1062
                for (j = 0; j < objFormGenerator.sections[i].sections.length; j++) {
-
 
1063
                    if (slug_section == objFormGenerator.sections[i].sections[j].slug_section) {
-
 
1064
                        for (k = 0; k < objFormGenerator.sections[i].sections[j].options.length; k++) {
-
 
1065
                            if (slug == objFormGenerator.sections[i].sections[j].options[k].slug_option) {
-
 
1066
                                validatorFormOption.resetForm();
-
 
1067
                                $('#form-option #option-section').val(objFormGenerator.sections[i].slug_section);
-
 
1068
                                $('#form-option #option-section').val(objFormGenerator.sections[i].sections[j].slug_section);
-
 
1069
                                $('#form-option #option-slug').val(objFormGenerator.sections[i].sections[j].options[k].slug_option);
-
 
1070
                                CKEDITOR.instances['option-text'].setData(objFormGenerator.sections[i].sections[j].options[k].text,
-
 
1071
                                    function() {
-
 
1072
                                        editor.focus();
-
 
1073
                                    });
-
 
1074
                                $('#form-option #option-correct').val(objFormGenerator.sections[i].sections[j].options[k].correct);
-
 
1075
                                if (objFormGenerator.sections[i].sections[j].type == 'multiple' || objFormGenerator.sections[i].sections[j].type == 'simple') {
-
 
1076
                                    $('#form-option #option-correct').parent().show();
-
 
1077
                                } else {
-
 
1078
                                    $('#form-option #option-correct').parent().hide();
-
 
1079
                                }
-
 
1080
                                $('#form-option #option-value').val(objFormGenerator.sections[i].sections[j].options[k].value);
-
 
1081
                                if (objFormGenerator.sections[i].sections[j].type == 'multiple' || objFormGenerator.sections[i].sections[j].type == 'rating-open') {
-
 
1082
                                    $('#form-option #option-value').parent().show();
-
 
1083
                                } else {
-
 
1084
                                    $('#form-option #option-value').parent().hide();
-
 
1085
                                }
-
 
1086
                                showForm = true;
-
 
1087
                                break;
-
 
1088
                            }
-
 
1089
                        }
-
 
1090
                    }
-
 
1091
                    if (showForm) {
-
 
1092
                        break;
-
 
1093
                    }
-
 
1094
                }
-
 
1095
            }
-
 
1096
            if (showForm) {
-
 
1097
                break;
-
 
1098
            }
-
 
1099
        }
-
 
1100
        if (showForm) {
-
 
1101
            $('#modal-option h4[class="modal-title"]').html('LABEL_EDIT LABEL_OPTION');
-
 
1102
            $('#modal-option').modal('show');
-
 
1103
        }
-
 
1104
    });
-
 
1105
    /**
-
 
1106
     * Clicked remove option
-
 
1107
     */
-
 
1108
    $('body').on('click', 'button.btn-delete-option', function(e) {
-
 
1109
        e.preventDefault();
-
 
1110
        var slug_section = $(this).data('section');
-
 
1111
        var slug_section = $(this).data('section');
-
 
1112
        var slug = $(this).data('slug');
-
 
1113
        bootbox.confirm({
-
 
1114
            title: "LABEL_DELETE LABEL_OPTION",
-
 
1115
            message: "LABEL_SECTION_DELETE",
-
 
1116
            buttons: {
-
 
1117
                cancel: {
-
 
1118
                    label: '<i class="fa fa-times"></i> LABEL_CANCEL'
-
 
1119
                },
-
 
1120
                confirm: {
-
 
1121
                    label: '<i class="fa fa-check"></i> LABEL_ACCEPT'
-
 
1122
                }
-
 
1123
            },
-
 
1124
            callback: function(result) {
-
 
1125
                if (result) {
-
 
1126
                    objFormGenerator.deleteOption(slug_section, slug_section, slug);
-
 
1127
                    renderSectionData(objFormGenerator.sections);
-
 
1128
                }
-
 
1129
            }
-
 
1130
        });
-
 
1131
    })
294
    
1132
    /**
295
    /**
1133
     * Clicked new Form
296
     * Clicked new Form
1134
     */
297
     */
1135
    $('button.btn-add-form').click(function(e) {
298
    $('button.btn-add-form').click(function(e) {
1136
        e.preventDefault();
-
 
1137
        objFormGenerator.clear();
-
 
1138
        objFormGenerator.render();
-
 
1139
        validatorForm.resetForm();
-
 
1140
        clearSectionData();
-
 
1141
        $('#form-main').attr('action', '$routeAdd');
299
        $('#form-main').attr('action', '$routeAdd');
1142
        $('#form-main #form-id').val('0');
300
        $('#form-main #form-id').val('0');
1143
        $('#form-main #form-continue').val('0');
301
        $('#form-main #form-continue').val('0');
1144
        $('#form-main #form-name').val('');
302
        $('#form-main #form-name').val('');
1145
        $('#row-lists').hide();
303
        $('#row-lists').hide();