descriptionForm.html 39.1 KB
Newer Older
Cyril Vazquez's avatar
Cyril Vazquez committed
1
2
3
4
5
6
7
<!--#
    This file is part of the auth package.
    (c) Maarch Prospre DE LAURE <prosper.delaure@maarch.org>

    For the full copyright and license information, please view the LICENSE
    file that was distributed with this source code.
#-->
8
<form id="descriptionForm" class="form-horizontal" data-translate-catalog="documentManagement/document">
Alexis Ragot's avatar
Alexis Ragot committed
9
10
11
12
    <div id="allFields">
        <div id="archivalProfileFields"></div>
        <div id="customFields"></div>
    </div>
13

Alexis Ragot's avatar
Alexis Ragot committed
14
15
    <div class="form-group">
        <div class="dropup col-xs-12" id="customFieldAddBtn">
16
            <button type="button" class="addCustomField btn btn-sm btn-link pull-right" data-type="text" title="Add field">
Alexis Ragot's avatar
Alexis Ragot committed
17
18
                &nbsp;<i class="fa fa-plus">&nbsp;</i>Add field
            </button>
19
            <!--ul class="dropdown-menu pull-right metadataList" aria-labelledby="dLabel">
Alexis Ragot's avatar
Alexis Ragot committed
20
21
22
23
24
                <li><a href='#' class="addCustomField" data-type="text">Text</a></li>
                <li><a href='#' class="addCustomField" data-type="name">Keyword</a></li>
                <li><a href='#' class="addCustomField" data-type="date">Date</a></li>
                <li><a href='#' class="addCustomField" data-type="boolean">Boolean</a></li>
                <li><a href='#' class="addCustomField" data-type="number">Numeric</a></li>
25
            </ul-->
Cyril Vazquez's avatar
Cyril Vazquez committed
26
        </div>
Alexis Ragot's avatar
Alexis Ragot committed
27
28
    </div>
</form>
Jerome Boucher's avatar
Jerome Boucher committed
29

Cyril Vazquez's avatar
Cyril Vazquez committed
30
31
32
33
34
35
36
37
38
39
40
<div class="hide">
    <!-- TEXT VARIABLE -->
    <div data-translate-catalog="documentManagement/documentMetadata">
        <span id="date_text">Date</span>
        <span id="number_text">Number</span>
        <span id="keyword_text">Keyword</span>
        <span id="byte_text">bytes</span>
    </div>

    <span id="preservation_text">Preservation</span>
    <span id="destruction_text">Destruction</span>
41
    <span id="retentionRule_text">%1$s after %2$s</span>
42
43
    <span id="object_header_new_text">Click to add '%1$s'</span>
    <span id="object_header_modify_text">Click to modify '%1$s'</span>
44
    <span id="object_required_text">* At least one field must be filled in.</span>
Cyril Vazquez's avatar
Cyril Vazquez committed
45
46
47
48
49

    <div data-translate-catalog="dates/dates">
        <span id="days_text">days</span>
        <span id="months_text">months</span>
        <span id="years_text" data-translate-context="unit">years</span>
Alexis Ragot's avatar
Alexis Ragot committed
50
        <span id="unlimited_text" >Unlimited</span>
Cyril Vazquez's avatar
Cyril Vazquez committed
51
52
53
54
55
56
57
58
59
    </div>

    <!-- ERROR MESSAGE -->
    <div data-translate-catalog="recordsManagement/message">
        <span id="fieldLabel_error">You must define all required fields.</span>
        <span id="customFieldLabel_error">You must define or remove all custom blank fields.</span>
        <span id="keywordLabel_error">A keyword label is missing.</span>
        <span id="retentionStartDateMissing_error">The retention start date is missing.</span>
        <span id="retentionRuleCodeMissing_error">The retention rule is missing.</span>
60
        <span id="documentMissing_error">You must deposit at least one document.</span>
Cyril Vazquez's avatar
Cyril Vazquez committed
61
62
    </div>
</div>
Dylan's avatar
Dylan committed
63
<span  id="addBtnTpl" class="input-group-btn hide">
64
    <button class="btn btn-success addArchiveDescriptionKeyword btn-sm" type="button" id="btnAddPrivilege" title="Add"><span class="fa fa-check"></span></button>
Dylan's avatar
Dylan committed
65
66
</span>
<div id="keywordTpl" class="input-group hide" name="keyword">
67
    <input type="text" class="form-control input-sm" data-enumerate ="true" disabled="" />
Dylan's avatar
Dylan committed
68
69
70
71
72
    <span class="input-group-btn">
        <button class="btn btn-danger btn-sm" type="button" name="btnDeleteKeyword" title="Delete"><span class="fa fa-trash-o"></span></button>
    </span>
</div>

73

Alexandre Goldstein's avatar
Alexandre Goldstein committed
74
<style>
75
.arrayDiv {
76
77
    padding-left: 0px;
    padding-right: 40px;
78
79
    list-style: none;
}
80
81
82
83

input:invalid {
    border-color: red;
}
84
85
86
87
88
89
90
91

.panel-title {
    font-size: 14px;
}

.control-label {
    padding-top: 3px;
}
Alexandre Goldstein's avatar
Alexandre Goldstein committed
92
93
</style>

Cyril Vazquez's avatar
Cyril Vazquez committed
94
<script type="text/javascript">
95
96
97
98
99
100
101
    $("#descriptionForm").keypress(function(e) {
    //To disable Enter keypress in this form
        if (e.which == 13) {
            return false;
        }
    });

Cyril Vazquez's avatar
Cyril Vazquez committed
102
103
104
    // METADATAS
    $('.addCustomField').on('click', function(e) {
        e.preventDefault();
Arnaud Pauget's avatar
Arnaud Pauget committed
105
        Metadata.addCustomField();
Cyril Vazquez's avatar
Cyril Vazquez committed
106
107
    });

108
109
    // Add row in array of metadata
    $('#allFields').on('click', '.addRowArray', function(e) {
Jerome Boucher's avatar
Jerome Boucher committed
110
        // click on button addRowArray won't work if input is empty
111
        var text = $(this).next("ul").children("li").children("input").val();
Alexandre Goldstein's avatar
Alexandre Goldstein committed
112
113
114
        if (text === "") {
            return;
        }
Cyril Vazquez's avatar
Cyril Vazquez committed
115
        Metadata.addArrayItem($(this).parent());
116
    });
117

118
119
120
    $('#allFields').on('click', '.removeRowArray', function(e) {
        e.preventDefault();
        Metadata.removeRowArray($(this));
121
122
    });

123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
    function onError(e) {
        var parentCollapse = e.closest(".panel-collapse");
        if (parentCollapse.length) {
            parentCollapse.addClass("in");
            var focusOn = parentCollapse;
        } else {
            focusOn = e;
        }
        $('html,body').animate({
            scrollTop: $(focusOn).offset().top
        }, 'slow');
        e.focus();
        e.css({"border-color":"red"});
        Metadata.isValid = false;
    }

139
140
    function checkErrorInput(e){
        if (!e.val()){
141
            onError(e);
142
143
144
145
146
147
148
        }
        else {
            e.css({"border-color":""});
        }
    }

    function checkNumberInput(e){
149
150
        // Regexp now accept "," and "." for type number
        if (e.val() && !RegExp("^[0-9]*[,.]?[0-9]{1,}$").test(e.val())) {
151
           onError(e);
152
153
        }
        else {
154
155
            value = e.val().replace(",", ".");
            e.val(value);
156
157
158
159
            e.css({"border-color":""});
        }
    }

160
161
162
163
164
165
166
167
168
169
    function checkInput(e){
        var field = $(e).find("input");
        if (!field.val()){
            field.css({"border-color":"red"});
        }
        else {
            field.css({"border-color":""});
        }
    }

Cyril Vazquez's avatar
Cyril Vazquez committed
170
171
172
173
174
    $('#allFields').on('click', '.removeField', function(e) {
        e.preventDefault();
        Metadata.removeField($(this));
    });

Dylan's avatar
Dylan committed
175
    //Delete keyword
176
    $("#archivalProfileFields").on("click","[name=btnDeleteKeyword]", function() {
Dylan's avatar
Dylan committed
177
178
179
180
181
182
183
        var selectId = $(this).closest("[name=keyword").find('input').attr('name');
        var val = $(this).closest("[name=keyword").find('input').val();
        $(this).closest("[name=keyword").remove();
        $('#'+selectId).find('option[value="'+val+'"]').removeClass('hide');
    });

    //Add keyword
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    $("#archivalProfileFields").on("click",".addArchiveDescriptionKeyword", function() {

        let val = null;
        let input = null;
        let name = $(this).data('select');
        let inputType = $(this).data('input-type');

        switch (inputType) {
            case "select" :
                input = $('#archivalProfileFields').find('[name="'+name+'"]').not("[disabled]");
                let option = input.find('option:selected');
                val =  option.text();
                break;

            case "input" :
                input = $('#archivalProfileFields').find('input[name="'+name+'"]').not("[disabled]");
                val =  input.val();
                break;
        }
Dylan's avatar
Dylan committed
203

204
        var tpl = $('#keywordTpl').clone().removeClass('hide');
Dylan's avatar
Dylan committed
205

206
        if(val) {
Dylan's avatar
Dylan committed
207
208
            tpl.find('input').val(val).attr('name',name);
            $('[name="list-'+name+'"]').append(tpl);
209
210
211
212
213
214
215
216
217
218
219

            switch (inputType) {
                case "select" :
                    input.find("option:selected").addClass('hide');
                    break;

                case "input" :
                    break;
            }

            input.val('');
Dylan's avatar
Dylan committed
220
        }
Dylan's avatar
Dylan committed
221
222
223

    });

224
225
226
227
228
229
230
231
232
    function guid() {
        function s4() {
            return Math.floor((1 + Math.random()) * 0x10000)
                .toString(16)
                .substring(1);
        }
        return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
    }

Cyril Vazquez's avatar
Cyril Vazquez committed
233
    var Metadata = {
234
235
        flagEmptyRequiredField : false,
        flagEmptyCustomField : false,
236
        isValid : true,
237
        isModification : false,
238

Cyril Vazquez's avatar
Cyril Vazquez committed
239
        addArchiveDescription : function(archiveDescription) {
240
            var descriptionField = archiveDescription.descriptionField;
241
242
            descriptionField.required = archiveDescription.required;
            descriptionField.readonly = archiveDescription.readonly;
Cyril Vazquez's avatar
Cyril Vazquez committed
243
            var target = $("#archivalProfileFields");
Cyril Vazquez's avatar
Cyril Vazquez committed
244
            var formGroup = Metadata.getFormGroup(descriptionField);
Jerome Boucher's avatar
Jerome Boucher committed
245
            target.append(formGroup);
246
        },
Cyril Vazquez's avatar
Cyril Vazquez committed
247

Cyril Vazquez's avatar
Cyril Vazquez committed
248
        getFormGroup: function(descriptionField) {
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
249
            var formGroup = $('<div class="form-group" />');
250
            var label = $('<label class="col-md-3 control-label" />')
251
252
            var spanLabel = $('<span name=descriptionLabel />').text(descriptionField.label);
            label.append(spanLabel);
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
253
            formGroup.append(label);
Alexandre Morin's avatar
Alexandre Morin committed
254
            var div = $('<div class="col-md-9" />');
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
255
256
            formGroup.append(div);

257
            switch (descriptionField.type) {
258
                case 'array':
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
259
                    var field = Metadata.getArrayDiv(descriptionField);
260
261
262
                    break;

                case 'object':
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
263
                    var field = Metadata.getObjectDiv(descriptionField);
264
265
                    break;

Cyril Vazquez's avatar
Cyril Vazquez committed
266
267
                case 'text':
                case 'string':
Cyril Vazquez's avatar
Cyril Vazquez committed
268
                    var field = Metadata.getTextInput(descriptionField);
Cyril Vazquez's avatar
Cyril Vazquez committed
269
                    break;
Jerome Boucher's avatar
Jerome Boucher committed
270

Cyril Vazquez's avatar
Cyril Vazquez committed
271
                case 'name':
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
272
                    var field = Metadata.getKeywordInput(descriptionField);
Cyril Vazquez's avatar
Cyril Vazquez committed
273
                    break;
274

Cyril Vazquez's avatar
Cyril Vazquez committed
275
                case 'number':
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
276
                    var field = Metadata.getNumberInput(descriptionField);
Cyril Vazquez's avatar
Cyril Vazquez committed
277
                    break;
278

Cyril Vazquez's avatar
Cyril Vazquez committed
279
                case 'date':
280
                    var field = Metadata.getDateInput(descriptionField, div);
Cyril Vazquez's avatar
Cyril Vazquez committed
281
282
                    break;

Cyril Vazquez's avatar
Cyril Vazquez committed
283
                case 'boolean':
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
284
                    var field = Metadata.getRadioInput(descriptionField);
Cyril Vazquez's avatar
Cyril Vazquez committed
285
                    break;
286

Cyril Vazquez's avatar
Cyril Vazquez committed
287
                default:
288
                    console.log("Type "+descriptionField.type+" of field "+descriptionField.name+" is not managed");
Cyril Vazquez's avatar
Cyril Vazquez committed
289
290
                    return;
            }
Dylan's avatar
Dylan committed
291

Cyril Vazquez's avatar
Cyril Vazquez committed
292
293
            formGroup.attr('name', descriptionField.name).attr('data-type', descriptionField.type);
            formGroup.data('description', descriptionField);
Dylan's avatar
Dylan committed
294

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
295
            div.append(field);
Jerome Boucher's avatar
Jerome Boucher committed
296

297
            if (descriptionField.required) {
298
                label.append($("<span style='color: red'>").text(" *"));
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
299
                field.attr("data-required", descriptionField.required);
Cyril Vazquez's avatar
Cyril Vazquez committed
300
301
            }

302
303
304
305
            if (descriptionField.readonly) {
                field.attr("data-readonly", descriptionField.readonly);
            }

Cyril Vazquez's avatar
Cyril Vazquez committed
306
307
            formGroup.data('field', field);

308
309
310
            return formGroup;
        },

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
311
        getTextInput : function (descriptionField) {
Jerome Boucher's avatar
Jerome Boucher committed
312
313
314
315
            var field = $('<input>')
                        .attr('type', 'text')
                        .addClass('form-control')
                        .addClass('input-sm');
316

317
318
319
320
321
            if(descriptionField.readonly && Metadata.isModification) {
                field.attr("readonly", true);
                field.attr("disabled", true);
            }

322
323
            if (descriptionField.default === "now") {
                let date = new Date();
324
325
                var day = ('0' + (date.getDate())).slice(-2);
                var month = ('0' + (date.getMonth()+1)).slice(-2);
326
327
328
329
330
331
332
333
334
                var year = date.getFullYear();

                let now = day + '-' + month + '-' + year;
                if (descriptionField.format === "datetime") {
                    now += ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
                }
                field.val(now);
            }

Cyril Vazquez's avatar
Cyril Vazquez committed
335
            field.getValue = function() {
336
337
338
                if(field.data('required')){
                    checkErrorInput(field);
                }
Cyril Vazquez's avatar
Cyril Vazquez committed
339
340
341
                return this.val();
            };

342
343
344
            return field;
        },

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
345
        getKeywordInput : function (descriptionField) {
346
347
348
            if (descriptionField.ref) {
                return Metadata.getTypeahead(descriptionField);
            } else if (descriptionField.enumeration) {
Jerome Boucher's avatar
Jerome Boucher committed
349
                return Metadata.getSelect(descriptionField);
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
350
            } else {
Jerome Boucher's avatar
Jerome Boucher committed
351
                return Metadata.getTextInput(descriptionField);
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
352
353
354
355
356
            }
        },

        getNumberInput : function(descriptionField) {
            var field = Metadata.getTextInput(descriptionField);
357
            field.attr('placeholder', $('#number_text').text()).attr("pattern", "^[0-9]*[,.]?[0-9]{1,}$");
358
359
360
361
362
363
364
365

            field.getValue = function() {
                checkNumberInput(field);
                if(field.data('required')){
                    checkErrorInput(field);
                }
                return this.val();
            };
366
367
368
369

            return field;
        },

370
        getDateInput : function(descriptionField, parent) {
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
371
            var field = Metadata.getTextInput(descriptionField);
372
            field.attr('placeholder', $('#date_text').text());
373

374
            parent.append(field);
375
            if (descriptionField.format == 'datetime') {
376
                field.data('format', 'datetime');
377
                field.datetimepicker(dateTimePickerParams);
378
379
380
            } else {
                field.datepicker(DatePickerParams);
            }
381

382
            field.getValue = function() {
383
384
385
                if(field.data('required')){
                    checkErrorInput(field);
                }
386
                if (this.val()){
387
                    if (field.data('format') == 'datetime') {
388
                        var dateObject = this.data('DateTimePicker').date().format('YYYY-MM-DDTHH:mm:ss');
389

390
                        return dateObject;
391
392
                    } else {
                        var dateObject = this.datepicker('getDate');
393

394
395
                        return $.datepicker.formatDate('yy-mm-dd', dateObject);
                    }
396
397
398
                }
            };

399
            return field;
Cyril Vazquez's avatar
Cyril Vazquez committed
400
401
        },

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
402
403
        getTextArea : function(descriptionField) {
            var field = $('<textarea class="form-control input-sm" rows="1" style="resize: none; resize: vertical; height: 2.5em;" />');
404
405
406
407
            if (descriptionField.default !== null && descriptionField.default !== undefined && descriptionField.default !== "") {
                field.val(descriptionField.default);
            }

408
409
410
411
412
            if(descriptionField.readonly && Metadata.isModification) {
                field.attr("readonly", true);
                field.attr("disabled", true);
            }

Cyril Vazquez's avatar
Cyril Vazquez committed
413
            field.getValue = function() {
414
415
416
                if(field.data('required')){
                    checkErrorInput(field);
                }
Cyril Vazquez's avatar
Cyril Vazquez committed
417
418
419
                return $.trim(this.val());
            };

420
421
422
            return field;
        },

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
423
424
425
426
427
428
429
430
        getRadioInput : function(descriptionField) {
            var field = $('<div class="btn-group" data-toggle="buttons"/>');
            var falseGroup = $('<label class="btn btn-default"/>');
            falseGroup.append('<input type="radio" name="defaultValue" autocomplete="off" value="0">')
                      .append($('<i class="fa fa-times" />'));
            field.append(falseGroup);

            var nullGroup = $('<label class="btn btn-default active"/>');
431
            nullGroup.append('<input type="radio" name="defaultValue" autocomplete="off" value=""> &nbsp');
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
432
433
434
435
436
437
            field.append(nullGroup);

            var trueGroup = $('<label class="btn btn-default"/>');
            trueGroup.append('<input type="radio" name="defaultValue" autocomplete="off" value="1">')
                     .append($('<i class="fa fa-check" />'));
            field.append(trueGroup);
438
439

            if (descriptionField.default == 1 || descriptionField.default == 0) {
Cyril Vazquez's avatar
Cyril Vazquez committed
440
441
                field.find('[value=""]').parent().removeClass("active");
                field.find('[value="'+descriptionField.default+'"]').parent().addClass("active");
Jerome Boucher's avatar
Jerome Boucher committed
442
                field.find('[value="'+descriptionField.default+'"]').prop("checked", "checked");
443
            }
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
444

445
446
447
448
            if(descriptionField.readonly && Metadata.isModification) {
                $(field).find(".btn").addClass("disabled");
            }

Cyril Vazquez's avatar
Cyril Vazquez committed
449
            field.getValue = function() {
450
451
452
453
454
455
456
457
458
459
460
461
                if(field.data('required')){
                    if (this.find('.active').children("input").val()=="") {
                        this.css({"border-color":"red",
                                    "border-style":"solid"});
                        this.focus();
                        Metadata.isValid = false;
                    } else {
                        this.css({"border-color":"",
                                    "border-style":"unset"});
                    }
                }
                return this.find('.active').children("input").val();
Cyril Vazquez's avatar
Cyril Vazquez committed
462
463
            };

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
464
            return field;
465
        },
466

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
467
        getSelect : function(descriptionField) {
Jerome Boucher's avatar
Jerome Boucher committed
468
469
            var field = $('<select class="form-control input-sm"><option value="" /></select>')

470
            $.each(descriptionField.enumeration, function (index, value) {
471
                text = value;
472
                if (descriptionField.enumNames && descriptionField.enumNames[index]) {
473
474
                    text = descriptionField.enumNames[index]
                }
475
476
                field.append($('<option>', {
                    value: value,
477
                    text: text
478
479
480
481
482
483
                }));
            });
            if (descriptionField.default !== null && descriptionField.default !== undefined && descriptionField.default !== "") {
                field.val(descriptionField.default);
            }

484
485
486
487
488
            if(descriptionField.readonly && Metadata.isModification) {
                field.attr("readonly", true);
                field.attr("disabled", true);
            }

Cyril Vazquez's avatar
Cyril Vazquez committed
489
            field.getValue = function() {
490
491
492
                if(field.data('required')){
                    checkErrorInput(field);
                }
Cyril Vazquez's avatar
Cyril Vazquez committed
493
494
495
                return $.trim(this.val());
            };

496
497
498
            return field;
        },

499
500
        getTypeahead : function(descriptionField) {
            var field = Metadata.getTextInput(descriptionField);
501
            field.attr('name', descriptionField.name);
Jerome Boucher's avatar
Jerome Boucher committed
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
            setTimeout(function(){
                var data = new Bloodhound({
                    datumTokenizer: Bloodhound.tokenizers.obj.whitespace(),
                    queryTokenizer: Bloodhound.tokenizers.whitespace,
                    remote: {
                        url: '/descriptionRef/'+descriptionField.name+'?query=%QUERY',
                        wildcard: '%QUERY'
                    }
                });
                data.initialize();

                field.typeahead(
                    {
                        hint: true,
                        highlight: true,
                        minLength: 1
                    },
                    {
                        name: 'data',
                        source: data.ttAdapter(),
                        templates: {
                            suggestion: function(data) {
                                delete data._query;
                                var html = $('<div>');
                                var keyname = Object.keys(data)[0];
                                var key = data.keyname;
                                html.append($('<strong class="badge">').text(key));
                                delete data[keyname];
                                var value = Object.keys(data).map(function(name){return data[name]}).join(" - ");
                                html.append(' '+value);
                                return html;
                            }
                        }
                    }
                ).on('typeahead:selected', function ($event, suggestion, source) {
                    var keys = Object.keys(suggestion); //fetched the key at first index
                    field.val(suggestion[keys[0]]);
                    field.typeahead('val', suggestion[keys[0]]);
                });
            }, 300);
542
543
544
545
546


            return field;
        },

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
547
548
549
550
551
552
553
        getObjectDiv : function(descriptionField) {
            id = guid();
            var div = $('<div class="panel panel-default" style="margin-bottom:0px;">');

            var heading = $('<div class="panel-heading"/>');
            div.append(heading);

554
            var title = $('<h4 class="panel-title" style="display: inline-block;" />');
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
555
556
557
            heading.append(title);

            var anchor = $('<a role="button" data-toggle="collapse" aria-expanded="true"/>');
558
559
560

            // Add title to Collapse
            var objectHeader = $("#object_header_new_text").text().replace(/%1\$s/g, descriptionField.label);
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
561
            anchor.attr('href', "#"+id);
562
            anchor.text(objectHeader);
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
563
564
            title.append(anchor);

565
            if (descriptionField.required) {
566
567
                var requiredTitle = $("#object_required_text").clone();
                requiredTitle.css({"font-size":"12px", "color":"red", "display":"block"});
568
569
570
                heading.append(requiredTitle);
            }

571
            var collapse = $('<div class="panel-collapse collapse">').attr('id', id);
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
572
573
574
575
576
577
578
            div.append(collapse);

            var panelBody = $('<div class="panel-body">');
            collapse.append(panelBody);

            var input = $('<input class="hide" type="text" data-type="object" value="">');
            panelBody.append(input);
Jerome Boucher's avatar
Jerome Boucher committed
579

580
            var propertyList = $('<ul style="padding-left: 0px; padding-right: 40px; list-style: none">');
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
581
            panelBody.append(propertyList);
Jerome Boucher's avatar
Jerome Boucher committed
582

583
            $.each(descriptionField.properties, function() {
584
585
586
                if (this.internal) {
                    return;
                }
Cyril Vazquez's avatar
Cyril Vazquez committed
587
588
                formGroup = Metadata.getFormGroup(this);

Cyril Vazquez's avatar
Cyril Vazquez committed
589
                var item = $("<li class='col-12'></li>");
Cyril Vazquez's avatar
Cyril Vazquez committed
590
591
592
593
594
595
                item.append(formGroup);

                item.data('formgroup', formGroup);
                item.data('field', formGroup.data('field'));

                propertyList.append(item);
596
            });
597

Cyril Vazquez's avatar
Cyril Vazquez committed
598
599
            div.data('propertyList', propertyList);

600
601
602
603
604
605
            if(descriptionField.readonly && Metadata.isModification) {
                div.find("input, select").attr("readonly", true);
                div.find("input, select").attr("disabled", true);
                $(div).find(".btn").addClass("disabled");
            }

Cyril Vazquez's avatar
Cyril Vazquez committed
606
607
608
            div.getValue = function() {
                var object = {};
                var properties = this.data('propertyList').children('li');
Jerome Boucher's avatar
Jerome Boucher committed
609

Cyril Vazquez's avatar
Cyril Vazquez committed
610
611
612
613
614
615
616
617
618
                $.each(properties, function() {
                    var field = $(this).data('field');
                    var name = $(this).data('formgroup').attr('name');
                    var value = field.getValue();
                    if (value) {
                        object[name] = value;
                    }
                });

619
620
621
                if (!div.parent("li").length){
                    if(field.data('required')){
                        if (!Object.keys(object).length) {
622
                            onError(field);
623
624
625
626
627
628
                        } else {
                            field.css({"border-color":""});
                        }
                    }
                }

Cyril Vazquez's avatar
Cyril Vazquez committed
629
630
631
632
633
                if (Object.keys(object).length) {
                    return object;
                }
            };

634
635
636
            return div;
        },

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
637
638
        getArrayDiv : function(descriptionField) {
            var div = $('<div>');
Cyril Vazquez's avatar
Cyril Vazquez committed
639
640
641
642
643

            div.attr("data-itemtype", descriptionField.itemType);
            if (descriptionField.itemType.type) {
                div.attr("data-itemtype", descriptionField.itemType.type)
            }
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
644

645
            var addButton = $('<button type="button" class="addRowArray btn btn-sm btn-success pull-right" title="Add data">&nbsp;<i class="fa fa-check">&nbsp;</i></button>');
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
646
647
            div.append(addButton);

648
            var itemList = $('<ul class="arrayDiv">');
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
649
            div.append(itemList);
Cyril Vazquez's avatar
Cyril Vazquez committed
650
            div.data('itemlist', itemList);
651
            div.data('item-description', descriptionField.itemType);
Cyril Vazquez's avatar
Cyril Vazquez committed
652
            div.data('description', descriptionField);
Jerome Boucher's avatar
Jerome Boucher committed
653

654
655
            var item = Metadata.getArrayItem(descriptionField);
            itemList.append(item);
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
656

657
658
659
660
661
            if(descriptionField.readonly && Metadata.isModification) {
                div.find("input, select").attr("readonly", true);
                div.find("input, select").attr("disabled", true);
                $(div).find(".btn").addClass("disabled");
            }
Cyril Vazquez's avatar
Cyril Vazquez committed
662
663
664
665

            div.getValue = function() {
                var values = [];
                var items = this.data('itemlist').children('li');
Jerome Boucher's avatar
Jerome Boucher committed
666

Cyril Vazquez's avatar
Cyril Vazquez committed
667
668
669
670
671
672
673
674
                $.each(items, function() {
                    var field = $(this).data('field');
                    var value = field.getValue();
                    if (value) {
                        values.push(value);
                    }
                });

675
676
677
                if(this.data('required')){
                    if (values.length == 0) {
                        if (this.data("itemtype") == "object") {
678
                            onError(div.find("div").first());
679
                        } else {
680
                            onError(div.find("input").first());
681
682
683
684
685
686
687
688
689
690
                        }
                    } else {
                        if (this.data("itemtype") == "object") {
                            div.find("div").first().css({"border-color":""});
                        } else {
                            div.find("input").first().css({"border-color":""});
                        }
                    }
                }

Cyril Vazquez's avatar
Cyril Vazquez committed
691
692
693
694
                if (values.length) {
                    return values;
                }
            };
695

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
696
697
            return div;
        },
698

699
        getArrayItem : function(descriptionField) {
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
700
            var item = $('<li class="input-group col-sm-12">');
701
702

            switch (descriptionField.itemType) {
703
704
                case 'text':
                case 'string':
Cyril Vazquez's avatar
Cyril Vazquez committed
705
                    var field = Metadata.getTextInput(descriptionField);
706
                    break;
Jerome Boucher's avatar
Jerome Boucher committed
707

708
                case 'name':
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
709
                    var field = Metadata.getKeywordInput(descriptionField);
710
                    break;
711

712
                case 'number':
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
713
                    var field = Metadata.getNumberInput(descriptionField);
714
                    break;
715

716
                case 'date':
717
                    var field = Metadata.getDateInput(descriptionField, item);
Cyril Vazquez's avatar
Cyril Vazquez committed
718
719
                    break;

720
                case 'boolean':
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
721
                    var field = Metadata.getRadioInput(descriptionField);
722
723
724
                    break;

                default:
725
726
727
                    if ($.isPlainObject(descriptionField.itemType)) {
                        switch (descriptionField.itemType.type) {
                            case 'object' :
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
728
                                var field = Metadata.getObjectDiv(descriptionField.itemType);
729
730

                                // Add title to Collapse
731
                                var objectHeader = $("#object_header_new_text").text().replace(/%1\$s/g, descriptionField.label);
732
733
734
                                header = field.find(".panel-title").first().find("a");
                                header.text(objectHeader);

735
736
                                break;
                            case 'array' :
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
737
                                var field = Metadata.getArrayDiv(descriptionField.itemType);
738
739
740
741
742
743
                                break;
                        }
                    } else {
                        console.log("Type "+descriptionField.itemType+" of field "+descriptionField.name+" is not managed");
                        return;
                    }
744
            }
745

Cyril Vazquez's avatar
Cyril Vazquez committed
746
747
            item.data('field', field);

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
748
            item.append(field);
749

Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
750
            return item;
751
752
        },

Cyril Vazquez's avatar
Cyril Vazquez committed
753
754
755
        addArrayItem : function(arrayDiv) {
            var itemList = arrayDiv.data('itemlist');
            var descriptionField = arrayDiv.data('description');
756

757
758
            // New empty item
            var div = Metadata.getArrayItem(descriptionField);
759

760
            var items = itemList.children("li");
761
            items.css({'margin-bottom': '0px'});
762

763
764
            if (items.length) {
                var index = items.length;
Jerome Boucher's avatar
Jerome Boucher committed
765

766
767
                previous = items.first();
                previous.find('a').first().text('#'+index);
768

769
770
                // Button to remove item
                if ($.isPlainObject(descriptionField.itemType)) {
771
                    previous.children().css({'border-color':''});
772
                    previous.find(".panel-heading").css({ "padding-top": "2px", "padding-bottom": "2px" });
773
                    previous.find(".panel-collapse").removeClass("in");
774
                    previous.find(".panel-heading").append('<a href="#" class="btn btn-default removeRowArray" style="display: inline-block;margin-left: 90%;"><i class="fa fa-times text-danger"></i></a>');
775
                } else {
776
                    previous.children().css({'border-color':''});
777
                    previous.attr("oninput", "checkInput(this)");
778
779
                    previous.append('<a href="#" class="btn btn-default removeRowArray input-group-addon"><i class="fa fa-times text-danger"></i></a>');
                }
Jerome Boucher's avatar
Jerome Boucher committed
780
            }
781
782
783
784

            if(descriptionField.readonly && Metadata.isModification) {
                div.find("input, select").attr("readonly", true);
                div.find("input, select").attr("disabled", true);
785
786
787
                div.find(".btn").addClass("disabled");
                // Disable 'removeRowArray' button
                previous.find(".btn").addClass("disabled");
788
789
            }

790
            div.css({'margin-bottom': '5px'});
Cyril Vazquez's avatar
WIP    
Cyril Vazquez committed
791
            itemList.prepend(div);
792
            itemList.append(previous);
793
794
        },

795
796
797
798
799
800
801
802
        addCustomField : function(source, target) {
            // to inititate default value and avoid bug on IE 11
            if (source === undefined) {
                source = "";
            }
            if (target === undefined) {
                target = $("#customFields");
            }
Cyril Vazquez's avatar
Cyril Vazquez committed
803
804
805
            var formGroup = $('<div class="form-group" />');
            var label = $('<div class="col-md-3"/>');
            var name = $('<input type="text" class="form-control input-sm name" />');
Arnaud Pauget's avatar
Arnaud Pauget committed
806
807
            name.val(source.property);

Cyril Vazquez's avatar
Cyril Vazquez committed
808
809
            label.append(name);
            formGroup.append(label);
Arnaud Pauget's avatar
Arnaud Pauget committed
810
            var div = $('<div class="input-group col-md-9" style="padding-left: 15px;"/>');
Cyril Vazquez's avatar
Cyril Vazquez committed
811
            formGroup.append(div);
Cyril Vazquez's avatar
Cyril Vazquez committed
812

Cyril Vazquez's avatar
Cyril Vazquez committed
813
814
            var field = $('<textarea class="form-control input-sm" rows="1" style="resize: none; resize: vertical; height: 2.5em;" />');
            field.attr('data-type', 'text');
Arnaud Pauget's avatar
Arnaud Pauget committed
815
            field.text(source.value);
Cyril Vazquez's avatar
Cyril Vazquez committed
816

817
            var removeButton = $('<a href="#" class="btn btn-default removeField input-group-addon"><i class="fa fa-times text-danger"></i></a>');
Jerome Boucher's avatar
Jerome Boucher committed
818

Cyril Vazquez's avatar
Cyril Vazquez committed
819
            div.append(field);
820
            div.append(removeButton);
821

Cyril Vazquez's avatar
Cyril Vazquez committed
822
823
            formGroup.data('name', name);
            formGroup.data('field', field);
Cyril Vazquez's avatar
Cyril Vazquez committed
824

Cyril Vazquez's avatar
Cyril Vazquez committed
825
826
827
            field.getValue = function() {
                return $.trim(this.val());
            };
Cyril Vazquez's avatar
Cyril Vazquez committed
828

Cyril Vazquez's avatar
Cyril Vazquez committed
829
            target.append(formGroup);
Cyril Vazquez's avatar
Cyril Vazquez committed
830
831
        },

832
        removeRowArray : function(source) {
833
            source.closest('li').remove();
834
835
        },

Cyril Vazquez's avatar
Cyril Vazquez committed
836
        removeField : function(source) {
837
            source.closest('.form-group').remove();
Cyril Vazquez's avatar
Cyril Vazquez committed
838
839
840
        },

        serialize : function(target) {
841
842
843
            Metadata.flagEmptyRequiredField = false;
            Metadata.flagEmptyCustomField = false;

Cyril Vazquez's avatar
Cyril Vazquez committed
844
            var selectedProfile = $('#archivalProfile').find('option:selected').data('json');
Cyril Vazquez's avatar
Cyril Vazquez committed
845
846
847
            var formGroups = target.find("#archivalProfileFields").children('div');
            var customFormGroups = target.find("#customFields").children('div');

848
            var descriptionObject = {};
Cyril Vazquez's avatar
Cyril Vazquez committed
849

Cyril Vazquez's avatar
Cyril Vazquez committed
850
851
            $.each(formGroups, function() {
                name = $(this).attr('name');
Jerome Boucher's avatar
Jerome Boucher committed
852
                field = $(this).data('field');
Cyril Vazquez's avatar
Cyril Vazquez committed
853
854
855
856
                value = field.getValue();

                if (value) {
                    descriptionObject[name] = value;
857
                }
858
            });
859
860
            if (!Metadata.isValid) {
                Metadata.isValid = true;
861
862
                Metadata.flagEmptyRequiredField.field = true;
                return -1 ;
863
            }
Jerome Boucher's avatar
Jerome Boucher committed
864

Cyril Vazquez's avatar
Cyril Vazquez committed
865
866
867
            $.each(customFormGroups, function() {
                name = $(this).data('name').val();
                var value = $.trim($(this).find('.value').val());
Jerome Boucher's avatar
Jerome Boucher committed
868
                field = $(this).data('field');
Cyril Vazquez's avatar
Cyril Vazquez committed
869
870
871
872
873
874
875
876
877
                value = field.getValue();

                if (value == undefined || value == null || name == ''|| name == undefined || name == null) {
                    $(this).closest(".form-group").addClass('has-error');
                    Metadata.flagEmptyCustomField = true;
                    return;
                } else {
                    $(this).closest(".form-group").removeClass('has-error');
                }
Dylan's avatar
Dylan committed
878

Cyril Vazquez's avatar
Cyril Vazquez committed
879
                descriptionObject[name] = value;
880
            });
Dylan's avatar
Dylan committed
881

882
883
            if (Metadata.flagEmptyRequiredField) {
                gritter.show($('#fieldLabel_error').text(), false);
Cyril Vazquez's avatar
Cyril Vazquez committed
884

885
886
887
888
                return -1;
            }
            if (Metadata.flagEmptyCustomField) {
                gritter.show($('#customFieldLabel_error').text(), false);
Dylan's avatar
Dylan committed
889

890
891
892
893
                return -1;
            }
            return descriptionObject;
        },
894

Cyril Vazquez's avatar
Cyril Vazquez committed
895
896
897
898
        clear : function() {
            $('#archivalProfileFields').empty();
            $('#customFields').empty();
        },
Dylan's avatar
Dylan committed
899

Cyril Vazquez's avatar
Cyril Vazquez committed
900
901
902
903
904
905
906
907
        acceptUserIndex : function(value) {
            if (value) {
                $('#customFields').empty();
                $('#customFieldAddBtn').removeClass("hide");
            } else {
                $('#customFields').empty();
                $('#customFieldAddBtn').addClass("hide");
            }
908
909
910
911
912
913
914
915
916
917
        },

        fillInMetadata : function(archiveId) {
            $.ajax({
                url         : "/archive/"+archiveId,
                type        : "GET",
                headers     : {"Accept":"application/json"},
                async       : false,
                success     : function (response) {
                    Metadata.archive = response;
918
                    // console.log(response);
919
                    Metadata.checkAndFill(Metadata.archive, $("#importPanel"));
920
921
922
                },
                error       : function (response) {
                    gritter.show(response.responseText, false);
923
924
                }
            });
925
            $('#modifyArchive').data("archive-id", archiveId);
926
927
            $('#modifyArchive').removeClass("hide");
            $("#syncImport").addClass("hide");
928
            $("#asyncImport").addClass("hide");
929
            $("#managementMetadataDiv").addClass("hide");
930
931
932
933
        },

        checkAndFill : function(data, target) {
            if (target.attr("data-type")==="date") {
934
                field = target.find("input");
935
                if (field.data('format') == 'datetime') {
936
                    data = data.replace('T', ' ');
937
938
                    field.data('DateTimePicker').date(data)
                    field.data('DateTimePicker').format('YYYY-MM-DD HH:mm:ss');
939
                } else {
940
                    data = new Date(data).toLocaleDateString("[?merge locale ?]").replace(/\//g, "-");
941
942
                    field.datepicker('setDate', data);
                }
Cyril Vazquez's avatar
Cyril Vazquez committed
943
944
            }

945
            if (target.attr("data-type")==="name") {
946
947
948
                field = target.find("select, input");
                field.val(data);
            } else if