~xibo-maintainers/xibo/tempel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
{#
/*
 * Spring Signage Ltd - http://www.springsignage.com
 * Copyright (C) 2015 Spring Signage Ltd
 * (${FILE_NAME})
 */

#}
{% extends "authed.twig" %}
{% import "inline.twig" as inline %}

{% block actionMenu %}
    <ul class="nav nav-pills pull-right">
        <li class="btn btn-success btn-xs"><a class="XiboFormButton btns" title="{% trans "Add a new Display Group" %}" href="{{ urlFor("displayGroup.add.form") }}"> <i class="fa fa-desktop" aria-hidden="true"></i> {% trans "Add Display Group" %}</a></li>
    </ul>
{% endblock %}

{% block pageContent %}
    <div class="widget">
        <div class="widget-title">{% trans "Display Groups" %}</div>
        <div class="widget-body">
            <div class="XiboGrid" id="{{ random() }}">
                <div class="XiboFilter well">
                    <div class="FilterDiv" id="Filter">
                        <form class="form-inline">
                            {% set title %}{% trans "Name" %}{% endset %}
                            {{ inline.input("displayGroup", title) }}

                            {% set title %}{% trans "Display" %}{% endset %}
                            {% set attributes = [
                                { name: "data-width", value: "200px" },
                                { name: "data-allow-clear", value: "true" },
                                { name: "data-placeholder--id", value: null },
                                { name: "data-placeholder--value", value: "" },
                                { name: "data-search-url", value: urlFor("display.search") },
                                { name: "data-search-term", value: "display" },
                                { name: "data-id-property", value: "displayId" },
                                { name: "data-text-property", value: "display" }
                            ] %}
                            {% set helpText %}{% trans "Return Display Groups that directly contain the selected Display." %}{% endset %}
                            {{ inline.dropdown("displayId", "single", title, "", null, "displayId", "display", helpText, "pagedSelect", "", "", "", attributes) }}

                            {% set title %}{% trans "Nested Display" %}{% endset %}
                            {% set helpText %}{% trans "Return Display Groups that contain the selected Display somewhere in the nested Display Group relationship tree." %}{% endset %}
                            {{ inline.dropdown("nestedDisplayId", "single", title, "", null, "displayId", "display", helpText, "pagedSelect", "", "", "", attributes) }}

                            {% set title %}{% trans "Dynamic Criteria" %}{% endset %}
                            {{ inline.input("dynamicCriteria", title) }}

                            {% set title %}{% trans "Tags" %}{% endset %}
                            {% set helpText %}{% trans "A comma separated list of tags to filter by. Enter --no-tag to see items without tags." %}{% endset %}
                            {{ inline.inputWithTags("tags", title, null, helpText) }}
                        </form>
                    </div>
                </div>
                <div class="XiboData">
                    <table id="displaygroups" class="table table-striped">
                        <thead>
                            <tr>
                                <th>{% trans "ID" %}</th>
                                <th>{% trans "Name" %}</th>
                                <th>{% trans "Description" %}</th>
                                <th>{% trans "Is Dynamic?" %}</th>
                                <th>{% trans "Criteria" %}</th>
                                <th>{% trans "Tags" %}</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>

                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block javaScript %}
    <script type="text/javascript">
        var table = $("#displaygroups").DataTable({ "language": dataTablesLanguage,
            serverSide: true, stateSave: true, stateDuration: 0,
            stateLoadCallback: function (settings, callback) {
                var data;
                $.ajax({
                    type: "GET",
                    async: false,
                    url: "{{ urlFor("user.pref") }}?preference=displayGroupGrid",
                    dataType: 'json',
                    success: function (json) {
                        try {
                            if (json.success) {
                                data = JSON.parse(json.data.value);
                            }
                        } catch (e) {
                            // Do nothing
                        }
                    }
                });
                return data;
            },
            stateSaveCallback: function (settings, data) {
                $.ajax({
                    type: "POST",
                    url: "{{ urlFor("user.pref") }}",
                    data: {
                        preference: [{
                            option: "displayGroupGrid",
                            value: JSON.stringify(data)
                        }]
                    }
                });
            },
            "filter": false,
            searchDelay: 3000,
            "order": [[ 1, "asc"]],
            ajax: {
                "url": "{{ urlFor("displayGroup.search") }}",
                "data": function(d) {
                    $.extend(d, $("#displaygroups").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                }
            },
            "columns": [
                { "data": "displayGroupId" },
                { "data": "displayGroup", "render": dataTableSpacingPreformatted },
                { "data": "description" },
                { "data": "isDynamic", "render": dataTableTickCrossColumn },
                { "data": "dynamicCriteria" },
                {
                    "name": "tags",
                    "sortable": false,
                    "data": dataTableCreateTags
                },
                {
                    "orderable": false,
                    "data": dataTableButtonsColumn
                }
            ]
        });

        table.on('draw', dataTableDraw);
        table.on('draw', { form: $("#displaygroups").closest(".XiboGrid").find(".FilterDiv form") }, dataTableCreateTagEvents);
        table.on('processing.dt', dataTableProcessing);
        dataTableAddButtons(table, $('#displaygroups_wrapper').find('.col-sm-6').eq(1));

        var displayTable;
        var criteria;

        function displayGroupFormOpen(dialog) {
            displayTable = null;

            $(dialog).find("input[name=dynamicCriteria]").on("keyup", $.debounce(500, function() {
                displayGroupQueryDynamicMembers(dialog);
            }));

            // First time in there
            displayGroupQueryDynamicMembers(dialog);
        }

        function displayGroupQueryDynamicMembers(dialog) {

            if ($(dialog).find("input[name=isDynamic]")[0].checked) {

                criteria = $(dialog).find("input[name=dynamicCriteria]").val();

                if (criteria == "") {
                    if (displayTable != null) {
                        displayTable.destroy();
                        displayTable = null;
                        $("#displays tbody").empty();
                    }

                    return;
                }

                if (displayTable != null) {
                    displayTable.ajax.reload();
                } else {
                    displayTable = $("#displays").DataTable({
                        "language": dataTablesLanguage,
                        serverSide: true,
                        stateSave: true, stateDuration: 0,
                        filter: false,
                        searchDelay: 3000,
                        "order": [[1, "asc"]],
                        ajax: {
                            "url": "{{ urlFor("display.search") }}",
                            "data": function (d) {
                                console.log(criteria);
                                $.extend(d, {display: criteria});
                            }
                        },
                        "columns": [
                            {"data": "displayId"},
                            {"data": "display"},
                            {
                                "data": "mediaInventoryStatus",
                                "render": function (data, type, row) {
                                    if (type != "display")
                                        return data;

                                    var icon = "";
                                    if (data == 1)
                                        icon = "fa-check";
                                    else if (data == 0)
                                        icon = "fa-times";
                                    else
                                        icon = "fa-cloud-download";

                                    return "<span class='fa " + icon + "'></span>";
                                }
                            },
                            {"data": "licensed", "render": dataTableTickCrossColumn}
                        ]
                    });

                    displayTable.on('processing.dt', dataTableProcessing);
                }
            }
        }

        function displayGroupMembersFormOpen(dialog) {
            
            var control = $(dialog).find(".controlDiv");

            // This contains the changes made since the form open
            if (control.data().members == undefined)
                control.data().members = {
                    displays: {},
                    displayGroups: {}
                };

            var table = $("#displaysMembersTable").DataTable({ 
                "language": dataTablesLanguage,
                serverSide: true, 
                stateSave: true, stateDuration: 0,
                filter: false,
                searchDelay: 3000,
                "order": [[1, "asc"]],
                ajax: {
                    "url": "{{ urlFor("display.search") }}",
                    "data": function(dataDisplay) {
                        $.extend(dataDisplay, $(dialog).find("#displayForm").serializeObject());
                        return dataDisplay;
                    }
                },
                "columns": [
                    { "data": "displayId"},
                    { "data": "display" },
                    {
                        "data": "mediaInventoryStatus",
                        "render": function (data, type, row) {
                            if (type != "display")
                                return data;

                            var icon = "";
                            if (data == 1)
                                icon = "fa-check";
                            else if (data == 0)
                                icon = "fa-times";
                            else
                                icon = "fa-cloud-download";

                            return "<span class='fa " + icon + "'></span>";
                        }
                    },
                    { "data": "loggedIn", "render": dataTableTickCrossColumn},
                    {
                        "name": "clientVersion",
                        "data": function (data, type) {
                            if (type != "display")
                                return data.clientVersion;

                            return data.clientType + ' ' + data.clientVersion + '-' + data.clientCode;
                        },
                        "visible": false
                    },
                    {
                        "name": "member",
                        "orderable": false,
                        "data": function (data, type, row) {
                            if (type != "display")
                                return data;

                            var checked = '';
                            
                            // Check if the element is already been checked/unchecked
                            if( typeof control.data().members != "undefined" && control.data().members.displays[data.displayId] != undefined){
                                checked = (control.data().members.displays[data.displayId]) ? 'checked' : '';
                            } else {
                                // If its not been altered, check for the original state
                                if( dialog.data().extra ){
                                    dialog.data().extra.displaysAssigned.forEach(function(extraElement) {
                                        if( extraElement.displayId == data.displayId ){
                                            checked = 'checked';
                                        }
                                    });    
                                }
                            }
                            
                            var checkBox = '<input type="checkbox" class="checkbox" data-member-id=' + data.displayId + ' data-member-type="display" ' + checked + '>';
                        
                            // Create checkbox
                            return checkBox;
                        }
                    },
                ]
            });

            table.on('draw', dataTableDraw);
            table.on('processing.dt', dataTableProcessing);
            
            var tableGroup = $("#displaysGroupsMembersTable").DataTable({ 
                "language": dataTablesLanguage,
                serverSide: true, stateSave: true, stateDuration: 0,
                filter: false,
                searchDelay: 3000,
                "order": [[1, "asc"]],
                ajax: {
                    "url": "{{ urlFor("displayGroup.search") }}",
                    "data": function(dataGroup) {
                        $.extend(dataGroup, $("#displaysGroupsMembersTable").closest(".XiboGrid").find(".FilterDiv form").serializeObject());
                        return dataGroup;
                    }
                },
                "columns": [
                    { "data": "displayGroupId"},
                    { "data": "displayGroup"},
                    {
                        "name": "member",
                        "orderable": false,
                        "data": function (data, type, row) {
                            if (type != "display")
                                return data;

                            var checked = '';
                            
                            // Check if the element is already been checked/unchecked
                            if( typeof control.data().members != "undefined" && control.data().members.displayGroups[data.displayGroupId] != undefined){
                                checked = (control.data().members.displayGroups[data.displayGroupId]) ? 'checked' : '';
                            } else {
                                // If its not been altered, check for the original state
                                if( dialog.data().extra ){
                                    dialog.data().extra.displayGroupsAssigned.forEach(function(extraElement) {
                                        if( extraElement.displayGroupId == data.displayGroupId ){
                                            checked = 'checked';
                                        }
                                    });    
                                }
                            }
                            
                            var checkBox = '<input type="checkbox" class="checkbox" data-member-id=' + data.displayGroupId + ' data-member-type="displayGroup" ' + checked + '>';
                        
                            // Create checkbox
                            return checkBox;
                        }
                    },
                ]
            });
            
            tableGroup.on('draw', dataTableDraw);
            tableGroup.on('processing.dt', dataTableProcessing);
                
            // Bind to the checkboxes change event
            control.on("change", ".checkbox", function() {
                
                // Update our global members data with this
                var memberId = $(this).data().memberId;
                var memberType = $(this).data().memberType;
                var value = $(this).is(":checked");

                if (memberType == "display")
                    control.data().members.displays[memberId] = (value) ? 1 : 0;
                else if (memberType == "displayGroup")
                    control.data().members.displayGroups[memberId] = (value) ? 1 : 0;
            });
        }

        function displayGroupMembersFormSubmit(id) {

            var form = $("#" + id);
            var members = form.data().members;

            // There may not have been any changes
            if (members == undefined) {
                // No changes
                XiboDialogClose();
                return;
            }

            // Create a new queue.
            window.queue = $.jqmq({

                // Next item will be processed only when queue.next() is called in callback.
                delay: -1,

                // Process queue items one-at-a-time.
                batch: 1,

                // For each queue item, execute this function, making an AJAX request. Only
                // continue processing the queue once the AJAX request's callback executes.
                callback: function( data ) {

                    // Make an AJAX call
                    $.ajax({
                        type: "POST",
                        url: data.url,
                        cache: false,
                        dataType: "json",
                        data: $.param(data.data),
                        success: function(response, textStatus, error) {

                            if (response.success) {

                                // Success - what do we do now?
                                if (response.message != '')
                                    SystemMessage(response.message, true);

                                // Process the next item
                                queue.next();
                            }
                            else {
                                // Why did we fail?
                                if (response.login) {
                                    // We were logged out
                                    LoginBox(response.message);
                                }
                                else {
                                    // Likely just an error that we want to report on
                                    form.find(".saving").remove();
                                    SystemMessageInline(response.message, form.closest(".modal"));
                                }
                            }
                        },
                        error: function(responseText) {
                            SystemMessage(responseText, false);
                        }
                    });
                },
                // When the queue completes naturally, execute this function.
                complete: function() {
                    // Remove the save button
                    form.find(".saving").parent().remove();

                    // Refresh the grids
                    // (this is a global refresh)
                    XiboRefreshAllGrids();

                    // Close the dialog
                    XiboDialogClose();
                }
            });

            var addedToQueue = false;

            // Build an array of id's to assign and an array to unassign
            var assign = [];
            var unassign = [];

            $.each(members.displays, function(name, value) {
                if (value == 1)
                    assign.push(name);
                else
                    unassign.push(name);
            });

            if (assign.length > 0 || unassign.length > 0) {

                var data = {
                    data: {},
                    url: form.data().url
                };
                data.data[form.data().param] = assign;
                data.data[form.data().paramUnassign] = unassign;

                // Queue
                queue.add(data);

                addedToQueue = true;
            }

            // Build an array of id's to assign and an array to unassign
            var assignGroup = [];
            var unassignGroup = [];

            $.each(members.displayGroups, function(name, value) {
                if (value == 1)
                    assignGroup.push(name);
                else
                    unassignGroup.push(name);
            });

            if (assignGroup.length > 0 || unassignGroup.length > 0) {
                var dataGroup = {
                    data: {},
                    url: form.data().groupsUrl
                };
                dataGroup.data[form.data().groupsParam] = assignGroup;
                dataGroup.data[form.data().groupsParamUnassign] = unassignGroup;

                // Queue
                queue.add(dataGroup);

                addedToQueue = true;
            }

            if (!addedToQueue) {
                XiboDialogClose();
            } else {
                // Start the queue
                queue.start();
            }
        }
    </script>
{% endblock %}