viewacl.tpl 26.5 KB
Newer Older
Naomi Guyer's avatar
Naomi Guyer committed
1
<input type="hidden" name="accesslist" value="">
2
3
4
5
<div class="panel panel-secondary view-container" id="editaccesswrap"
    data-viewtype="{{$viewtype}}"
    data-user-roles='{{$userroles}}'
    data-group-roles='{{$grouproles}}' >
Robert Lyon's avatar
Robert Lyon committed
6
7
8
    {{if  $viewtype == "profile" }}
        <h2 class="panel-heading">{{str tag=profile section=view}}</h2>
    {{/if}}
9

10
    <table id="accesslisttable" class="fullwidth accesslists table form-inline">
11
        <thead>
Robert Lyon's avatar
Robert Lyon committed
12
13
14
15
16
17
            <tr class="accesslist-head th-has-shared">
                <th></th>
                <th>{{str tag=sharedwith section=view}}</th>
                <th class="text-center">{{str tag=From}}</th>
                <th class="text-center">{{str tag=To}}</th>
                {{if  $viewtype !== "profile" }}
18
19
                <th class="text-center commentcolumn"><div class="th-shared-wrap"><span class="th-shared-heading">{{str tag=comments section=view}}</span> <span class="th-shared-title">{{str tag=allow section=view}}</span></div></th>
                <th class="text-center commentcolumn"><span class="sr-only">{{str tag=Comments}}</span> <span class="th-shared-title">{{str tag=moderate section=view}}</span></th>
Robert Lyon's avatar
Robert Lyon committed
20
                {{/if}}
21
22
            </tr>
        </thead>
Robert Lyon's avatar
Robert Lyon committed
23
        <tbody id ="accesslistitems" data-id="accesslistitems">
24
25
        </tbody>
    </table>
Naomi Guyer's avatar
Naomi Guyer committed
26
</div>
Robert Lyon's avatar
Robert Lyon committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40

<script type="text/x-tmpl" id="selectoption-template">
{% if (o.id) { %}<option value="{%=o.id%}" selected></option>{% } %}
</script>

<script type="text/x-tmpl" id="roles-template">
    <option value="" selected>{%=o.defaultText%}</option>
    {% for (var i=0; i<o.roles.length; i++) { %}
         <option value="{%=o.roles[i].name%}">{%=o.roles[i].display%}</option>
    {% } %}
</script>

<script type="text/x-tmpl" id="row-template">
<tr id="row-{%=o.id%}" data-id="{%=o.id%}">
41
    <td class="text-center with-icon tiny">
42
        <a class="{% if (o.presets.locked || o.presets.empty) { %}icon-placeholder{% } %} text-block" data-bind="remove-share" href="#" id="remove-share{%=o.id%}">
43
            <span class="text-danger icon icon-lg icon-trash" role="presentation" aria-hidden="true"></span>
Robert Lyon's avatar
Robert Lyon committed
44
45
46
47
48
            <span class="sr-only">{%={{jstr tag=remove section=view}}%}</span>
        </a>
    </td>
    <td>
        <div class="dropdown-group dropdown-single-option">
49
            <span class="picker input-short">
Robert Lyon's avatar
Robert Lyon committed
50
51
52
53
54
55
56
57
58
59
60
61
                <input data-settype="true" type="hidden" id="typehidden-{%=o.id%}" value="{%=o.presets.type%}" name="accesslist[{%=o.id%}][type]" />
                <select id="type-{%=o.id%}" name="accesslist[{%=o.id%}][searchtype]" class="js-share-type form-control input-small select" {% if (o.presets.locked) { %}disabled{% } %}>
                    <option data-type="" {% if (!o.presets.type) { %}selected{% } %} value="">{%={{jstr tag=sharewith section=view}}%}</option>

                    <optgroup label="{%={{jstr tag=searchfor section=view}}%}">
                        <option data-search-option="true" id="friend" value="friend"{% if (o.presets.type == "friend") { %} selected{% } %}>{{str tag=friends section=view}}</option>
                        <option data-search-option="true" id="group" value="group"{% if (o.presets.type == "group") { %} selected{% } %}>{{str tag=groups}}</option>
                        <option data-search-option="true" id="user" value="user"{% if (o.presets.type == "user") { %} selected{% } %}>{{str tag=users}}</option>
                    </optgroup>

                    <optgroup label="{%={{jstr tag=general section=view}}%}" id="potentialpresetitemssharewith">
                        {% for (var i=0; i<o.shareoptions.general.length; i++) { %}
62
                            <option value="{%=o.shareoptions.general[i].id%}"{% if (o.presets.type == o.shareoptions.general[i].id) { %} selected{% } %} {% if (o.shareoptions.general[i].locked) { %} disabled{% } %}>{%=o.shareoptions.general[i].name%}</option>
Robert Lyon's avatar
Robert Lyon committed
63
64
65
66
67
                        {% } %}
                    </optgroup>

                    <optgroup label="{%={{jstr tag=institutions section=view}}%}" id="potentialpresetitemsinstitutions">
                        {% for (var i=0; i<o.shareoptions.institutions.length; i++) { %}
68
                            <option data-type="institution" value="{%=o.shareoptions.institutions[i].id%}"{% if (o.presets.id == o.shareoptions.institutions[i].id && o.presets.type == 'institution') { %} selected{% } %}>{%=o.shareoptions.institutions[i].name%}</option>
Robert Lyon's avatar
Robert Lyon committed
69
70
71
72
                        {% } %}
                    </optgroup>
                    <optgroup label="{%={{jstr tag=groups section=view}}%}" id="potentialpresetitemsgroups">
                        {% for (var i=0; i<o.shareoptions.myGroups.length; i++) { %}
73
                            <option data-type="group" value="{%=o.shareoptions.myGroups[i].id%}"{% if (o.presets.id == o.shareoptions.myGroups[i].id && o.presets.type == 'group') { %} selected{% } %}>
Robert Lyon's avatar
Robert Lyon committed
74
75
76
77
78
79
80
                            {%=o.shareoptions.myGroups[i].name%}
                            </option>
                        {% } %}
                    </optgroup>
                </select>
            </span>
            {% if(o.presets.empty) { %}<p class="table-help-text">{%={{jstr tag=whosharewith section=view}}%}</p>{% } %}
81
            <div class="hidden picker input-short" data-select-wrapper="true">
Robert Lyon's avatar
Robert Lyon committed
82
83
84
85
                <select id="hidden-user-search-[{%=o.id%}]" name="accesslist[{%=o.id%}][id]" class=" select js-select2-search">
                    {% if (o.presets.id) { %}<option value="{%=o.presets.id%}">{%=o.presets.name%}</option>{% } %}
                </select>
            </div>
86
            <span class="picker input-short{% if (!(o.presets.type == 'group' || o.presets.type == 'user')) { %} hidden {% } %}">
87
                <select data-roles="grouproles" name="accesslist[{%=o.id%}][role]" class="form-control input-small select">
88
                    {% if (o.presets.type == 'group' || o.presets.type == 'user') { %}
89
90
91
92
93
                        <option value="" >{%=o.defaultText%}</option>
                        {% for (var i=0; i<o.roles.length; i++) { %}
                             <option value="{%=o.roles[i].name%}" {% if (o.presets.role == o.roles[i].name) { %} selected {% } %}>{%=o.roles[i].display%}</option>
                        {% } %}
                    {% } %}
Robert Lyon's avatar
Robert Lyon committed
94
95
96
97
98
99
                </select>
            </span>
        </div>
    </td>
    <td class="text-center js-date short" data-name='from'>
        <div class="date-picker js-date-picker js-hide-empty {% if (o.presets.empty) { %}hidden{% } %}">
100
            <div class="hasDatepickerwrapperacl"><input type="text" name="accesslist[{%=o.id%}][startdate]" class="form-control pull-left" data-setmin="true" setdatatarget="to" value="{%=o.presets.startdate%}" aria-label="{{str tag=element.calendar.datefrom section=pieforms}} ({{str tag='element.calendar.format.arialabel' section='pieforms'}})" {% if (o.presets.locked) { %}disabled{% } %}></div>
Robert Lyon's avatar
Robert Lyon committed
101
102
103
104
        </div>
    </td>
    <td class="text-center js-date short" data-name='to'>
        <div class="date-picker js-date-picker js-hide-empty {% if (o.presets.empty) { %}hidden{% } %}">
105
            <div class="hasDatepickerwrapperacl"><input type="text" name="accesslist[{%=o.id%}][stopdate]" class="form-control pull-left" data-setmax="true" setdatatarget="from" value="{%=o.presets.stopdate%}" aria-label="{{str tag=element.calendar.dateto section=pieforms}} ({{str tag='element.calendar.format.arialabel' section='pieforms'}})" value="{%=o.presets.stopdate%}" {% if (o.presets.locked) { %}disabled{% } %}></div>
Robert Lyon's avatar
Robert Lyon committed
106
107
108
        </div>
    </td>
    {% if (o.viewtype !== "profile") { %}
109
110
        <td class="text-center tiny commentcolumn">
            <input value="1" name="accesslist[{%=o.id%}][allowcomments]" class="allow-comments-checkbox checkbox js-hide-empty {% if (o.presets.empty) { %}hidden{% } %}" type="checkbox" {% if (o.presets.allowcomments == "1") { %}checked{% } else { %}{% } %} {% if (o.presets.locked) { %}disabled{% } %}>
Robert Lyon's avatar
Robert Lyon committed
111
        </td>
112
113
        <td class="text-center tiny commentcolumn">
            <input value="1" name="accesslist[{%=o.id%}][approvecomments]" class="moderate-comments-checkbox checkbox js-hide-empty {% if (o.presets.empty) { %}hidden{% } %}" type="checkbox" {% if (o.presets.approvecomments == "1" && o.presets.allowcomments == "1") { %}checked{% } else { %}{% } %}  {% if (o.presets.locked) { %}disabled{% } %}>
Robert Lyon's avatar
Robert Lyon committed
114
115
116
117
118
119
        </td>
    {% } %}

</tr>
</script>

120
<script type="application/javascript">
Naomi Guyer's avatar
Naomi Guyer committed
121
122
var count = 0;

Robert Lyon's avatar
Robert Lyon committed
123
124
125
jQuery(function($) {
"use strict";

126
    $(function() {
Robert Lyon's avatar
Robert Lyon committed
127

128
129
130
131
        // For some reasons, form validation for required select element generated by select2js
        // does not work properly on Microsoft Edge
        // This is a workaround
        // It will check if the required select is not empty and remove the attribute 'required'
132
        $j('#{{$formname}}_submit').on("click", function(e) {
133
            $j('#{{$formname}} select:required').each(function() {
134
135
                if ($j(this).val()) {
                    $j(this).prop("required", false);
136
                    $j(this).parent().parent().find('div.errmsg').remove();
137
138
                }
                else {
139
140
141
142
143
                    var b = $j(this).attr("data-type");
                    $j('#messages').html('<div class="alert alert-danger"><div>' + get_string('errorprocessingform', 'mahara') + '</div></div>');
                    if ($j(this).parent().parent().find('div.errmsg').length === 0) {
                        $j(this).parent().parent().append('<div class="errmsg"><span class="input-short-error"></span><span id="' + $j(this).prop('id') + '_error">' + get_string('rule.required.required', 'pieforms') + '</span></div>');
                    }
144
145
146
147
148
                    e.preventDefault();
                    e.stopPropagation();
                }
            });
        });
149
        // Remove 'required' on cancel
150
        $j('#cancel_{{$formname}}_submit').on("click", function(e) {
151
152
153
154
            $j('#{{$formname}} select:required').each(function() {
                $j(this).prop("required", false);
            });
        });
155

Robert Lyon's avatar
Robert Lyon committed
156
        function setDatePicker(target) {
157
            var loc = '{{strstr(current_language(), '.', true)}}'; // Get current langauge to use for locale
Robert Lyon's avatar
Robert Lyon committed
158
            target.datetimepicker({
159
160
161
162
163
164
165
166
167
                useCurrent: false,
                format: "{{str(tag='pieform_calendar_dateformat' section='langconfig')|pieform_element_calendar_convert_dateformat}} {{str(tag='pieform_calendar_timeformat' section='langconfig')|pieform_element_calendar_convert_timeformat}}",
                locale: loc,
                showClear: true,
                showTodayButton: true,
                tooltips: {{$datepickertooltips|safe}},
                icons: {
                    clear: "icon icon-trash",
                    today: "icon icon-crosshairs",
168
                },
169
170
171
            }).on('dp.hide', function(selectedDate) {
                if (selectedDate !== "") {
                    formchangemanager.setFormStateById('{{$formname}}', FORM_CHANGED);
Robert Lyon's avatar
Robert Lyon committed
172
                }
Naomi Guyer's avatar
Naomi Guyer committed
173
            });
Robert Lyon's avatar
Robert Lyon committed
174
175
176
177
178
        }

        function formatSelect2Results (data) {
            if (data.loading) {
                return data.text;
Naomi Guyer's avatar
Naomi Guyer committed
179
            }
Robert Lyon's avatar
Robert Lyon committed
180
181
182
183
184
185
186
187
188

            var markup;

            // Need to know which row
            if (data.grouptype !== undefined) {
                markup = data.name;
            }
            else {
                markup =
189
                '<img class="select2-user-icon" src="' + config.wwwroot + 'thumb.php?type=profileicon&maxwidth=40&maxheight=40&id=' + data.id + '" />' +
190
                '<span>' + data.name + '</span>';
Robert Lyon's avatar
Robert Lyon committed
191
192
            }
            return markup;
Naomi Guyer's avatar
Naomi Guyer committed
193
        }
Robert Lyon's avatar
Robert Lyon committed
194
195

        function formatSelect2Selected (data) {
196
            if (data._resultId !== undefined) {
197
                formchangemanager.setFormStateById('{{$formname}}', FORM_CHANGED);
198
            }
Robert Lyon's avatar
Robert Lyon committed
199
200
201
202
            if (data.grouptype !== undefined) {
                return '<span data-grouptype="'+ data.grouptype + '">'+ data.name + '</span>';
            }
            else {
203
                return data.name || data.text;
Robert Lyon's avatar
Robert Lyon committed
204
            }
Naomi Guyer's avatar
Naomi Guyer committed
205
        }
Robert Lyon's avatar
Robert Lyon committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

        function attachSelect2Search(object) {
            var self = object;

            $(self).select2({
                placeholder: {{jstr tag=search section=view}},
                ajax: {
                    url: "access.json.php",
                    dataType: 'json',
                    delay: 250,
                    type: 'POST',
                    data: function (params) {
                        return {
                            'type' : $(self).attr('data-type'),
                            'query': params.term, // search term
                            'offset': 0,
                            'limit': 10,
223
                            'page': params.page || 0,
Robert Lyon's avatar
Robert Lyon committed
224
225
226
227
228
229
230
231
232
233
234
235
                            'sesskey': config.sesskey
                        };
                    },
                    processResults: function (data, page) {
                        // parse the results into the format expected by Select2.
                        // since we are using custom formatting functions we do not need to
                        // alter the remote JSON data
                        if (data.message.roles) {
                            $(self).attr('data-roles', JSON.stringify(data.message.roles));
                        }

                        return {
236
237
238
239
                            results: (data.message.count > 0) ? data.message.data : [],
                            pagination: {
                                more: data.message.more
                            }
Robert Lyon's avatar
Robert Lyon committed
240
241
242
243
244
245
246
                        };
                    },
                    cache: true
                  },
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                templateResult: formatSelect2Results,
                templateSelection: formatSelect2Selected,
247
                maximumSelectionLength: 20,
Robert Lyon's avatar
Robert Lyon committed
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
                language: {
                    errorLoading: function () {
                        return {{jstr tag=errorLoading section=mahara}};
                    },
                    inputTooShort: function () {
                        return {{jstr tag=inputTooShort section=mahara}};
                    },
                    inputTooLong: function () {
                        return {{jstr tag=inputTooLong section=mahara}};
                    },
                    loadingMore: function () {
                        return {{jstr tag=loadingMore section=mahara}};
                    },
                    maximumSelected: function () {
                        return {{jstr tag=maximumSelected section=mahara}};
                    },
                    noResults: function () {
                        return {{jstr tag=noResults section=mahara}};
                    },
                    searching: function () {
                        return {{jstr tag=searching section=mahara}};
                    }
                }
            });

            $(self).on("select2:select", function (e) {
                if($(self).attr('data-roles').length > 0) {
                    showRoleSelect(e, self);
                }
277
                formchangemanager.setFormStateById('{{$formname}}', FORM_CHANGED);
Robert Lyon's avatar
Robert Lyon committed
278
            });
279
280
281
282
283
284
285

            /* Select2 lib does not hide the element with "style=display:none"
             * anymore. see https://github.com/select2/select2/issues/3065
             * We need the line below to let Chome know the text is not visible
             * and behat tests won't break
             */
            $('.select2-hidden-accessible').hide();
Naomi Guyer's avatar
Naomi Guyer committed
286
        }
Robert Lyon's avatar
Robert Lyon committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308

        /*
         * Render all existing rules into our table
         */
        function renderAccessList(shareoptions) {
            var accesslist = {{$accesslist|safe}},
                i;

            accesslist = accesslist.sort(function(a, b) {
                if (a.locked === true) {
                    return 0;
                }
                return 1;
            });

            if (accesslist.length > 0) {
                for (i = 0; i < accesslist.length; i = i + 1) {
                    addNewRow(shareoptions, accesslist[i]);
                }
            }

            // render empty row
309
            addNewRow(shareoptions, {empty: true});
Naomi Guyer's avatar
Naomi Guyer committed
310
        }
Robert Lyon's avatar
Robert Lyon committed
311
312
313
314
315
316
317
318
319

        function addNewRow(shareoptions, presets) {
            if (presets === undefined) {
                presets = {};
            }

            var data,
                lastrow,
                id,
320
321
322
323
                viewtype = $('[data-viewtype]').attr('data-viewtype'),
                roles,
                defaultText,
                grouproles;
Robert Lyon's avatar
Robert Lyon committed
324
325
326
327
328
329
330
331
332

            if($('#accesslistitems tr').length > 0){
                lastrow = $('#accesslistitems tr:last-child');
                id = parseInt(lastrow.attr('data-id'), 10) + 1;
            }
            else {
                id = 0;
            }

333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
            if (!presets.empty) {
                if (presets.type == 'user') {
                    roles = JSON.parse($('[data-user-roles]').attr('data-user-roles'));
                    defaultText = {{jstr tag=nospecialrole section=view}};
                }
                else {
                    // group
                    grouproles = JSON.parse($('[data-group-roles]').attr('data-group-roles'));
                    defaultText = {{jstr tag=everyoneingroup section=view}};
                    if (presets.grouptype == 'course') {
                        roles = grouproles.course;
                    }
                    else {
                        roles = grouproles.standard;
                    }
                }
            }
Robert Lyon's avatar
Robert Lyon committed
350
351
352
353
            data = {
                id: id,
                shareoptions: shareoptions,
                presets: presets,
354
355
356
                viewtype: viewtype,
                roles: roles,
                defaultText: defaultText
Robert Lyon's avatar
Robert Lyon committed
357
358
359
360
361
            };

            $('#accesslistitems').append(tmpl("row-template", data));

            attachEventListeners(id);
Naomi Guyer's avatar
Naomi Guyer committed
362
        }
Robert Lyon's avatar
Robert Lyon committed
363
364
365
366

        function attachEventListeners(id) {
            var newrow = $('#accesslistitems').find('[data-id="' + id + '"]');
            attachShareTypeEvent(newrow);
367
            setDatePicker($(newrow).find('.js-date-picker > div > input'));
Robert Lyon's avatar
Robert Lyon committed
368
369
370
371
372
373
374
375
376
            attachSelect2Search($(newrow).find('.js-select2-search'));
            attachCommentEvents($(newrow));
            onChange($(newrow));

            // Remove a table row when the remove button is clicked
            $('[data-bind="remove-share"]').on('click', function(e) {
                e.preventDefault();
                if (!$(this).hasClass('icon-placeholder')) {
                    clearRow(this);
377
                    formchangemanager.setFormStateById('{{$formname}}', FORM_CHANGED);
Robert Lyon's avatar
Robert Lyon committed
378
379
                }
            });
Naomi Guyer's avatar
Naomi Guyer committed
380
381
        }

Robert Lyon's avatar
Robert Lyon committed
382
383
384
385
386
387
388
389
390
391
392
393
394
        function onChange(row) {
            row.find('.js-share-type').on('change', function(e) {
                var remove = row.find('[data-bind="remove-share"]'),
                    helpText = row.find('.table-help-text');

                if (remove.hasClass('icon-placeholder')) {
                    if (helpText.length > 0) {
                        helpText.remove();
                    }
                    remove.removeClass('icon-placeholder js-empty');
                    row.find('.js-hide-empty').removeClass('hidden');
                    addNewRow(shareoptions, {empty: true});
                }
395
                formchangemanager.setFormStateById('{{$formname}}', FORM_CHANGED);
Robert Lyon's avatar
Robert Lyon committed
396
397
            });
        }
Naomi Guyer's avatar
Naomi Guyer committed
398

Robert Lyon's avatar
Robert Lyon committed
399
400
401
402
403
404
405
        function clearRow(self) {
            $(self).closest('tr').remove();

            if ($('#accesslistitems tr').length < 1) {
                addNewRow(shareoptions, {empty: true});
            }
        }
Naomi Guyer's avatar
Naomi Guyer committed
406

Robert Lyon's avatar
Robert Lyon committed
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
        /*
         * Construct data for share with dropdown
         *
         * @return Array | array of objects
         */
        function shareWithOptions() {
            var general = {{$potentialpresets|safe}},
                institutions = {{$myinstitutions|safe}},
                allGroups = {{$allgroups|safe}},
                myGroups = {{$mygroups|safe}},
                i,
                results = {
                    general: [],
                    institutions: [],
                    allGroups: [],
                    myGroups: []
                },
                item;

            for (i = 0; i < general.length; i = i + 1 ) {
                item = general[i];

                results.general[i] = {
                    'id':  item.id,
                    'name': item.name,
432
433
                    'selected': item.preset,
                    'locked': item.locked
Robert Lyon's avatar
Robert Lyon committed
434
435
                };
            }
Naomi Guyer's avatar
Naomi Guyer committed
436

Robert Lyon's avatar
Robert Lyon committed
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
            for (i = 0; i < institutions.length; i = i + 1 ) {
                item = institutions[i];

                results.institutions[i] = {
                    'id':  item.id,
                    'name': item.name,
                    'selected': item.preset
                };
            }

            item = allGroups;
            results.allGroups = {
                'id':  item.id,
                'name': item.name,
                'selected': item.preset
            };

            for (i = 0; i < myGroups.length; i = i + 1 ) {
                item = myGroups[i];

                results.myGroups[i] = {
                    'id':  item.id,
                    'name': item.name,
                    'selected': item.preset
                };
            }

            return results;
Naomi Guyer's avatar
Naomi Guyer committed
465
        }
Robert Lyon's avatar
Robert Lyon committed
466
467
468
469
470
471
472
473
474
475
476
477


        /*
         * Show the role select when a group is selected
         * @param e | event, self | select Object
         */
        function showRoleSelect(e, self) {

            var roles = JSON.parse($(self).attr('data-roles')),
                data,
                id = $(self).closest('tr').attr('data-id'),
                select = $(self).closest('.dropdown-group').find('[data-roles="grouproles"]');
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
            if ($(self).attr('data-type') == 'group') {
                var grouptype = $(self).parent().find('[data-grouptype]').attr('data-grouptype'),
                    defaultText = {{jstr tag=everyoneingroup section=view}};
                data = {
                    id: id,
                    defaultText: defaultText,
                    roles: roles[grouptype]
                };
            }
            else {
                var defaultText = {{jstr tag=nospecialrole section=view}};
                data = {
                    id: id,
                    defaultText: defaultText,
                    roles: roles
                };
            }
Robert Lyon's avatar
Robert Lyon committed
495
496
            select.html(tmpl("roles-template", data));
            select.prop('disabled', false).parent().removeClass('hidden');
Naomi Guyer's avatar
Naomi Guyer committed
497
498
        }

Robert Lyon's avatar
Robert Lyon committed
499
500
501
        function hideRoleSelect(self) {
            var roleSelect = $(self).closest('.dropdown-group').find('[data-roles="grouproles"]');
            roleSelect.prop('disabled', true).empty().parent().addClass('hidden');
Naomi Guyer's avatar
Naomi Guyer committed
502
        }
Robert Lyon's avatar
Robert Lyon committed
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

        /*
         * When a 'search option' is picked in the share with dropdown, show ID picker (search field)
         * When a 'id' option is picked, hide ID picker and set id on ID picker field
         *
         * @params Self | DOM object, isPreset | boolean (clear the search select if false)
         */
        function setIDField(self, isPreset) {

            var selected = $(self).find("option:selected"),
                val = selected.val(),
                searchoption = selected.attr("data-search-option"),
                idFieldWrapper = $(self).closest('td').find('[data-select-wrapper="true"]'),
                idField = idFieldWrapper.find('.js-select2-search');

            if (!isPreset) {
                resetIdField(idField);
            }

            // set data-type as a param on select related fields
            idFieldWrapper.find('> *').attr('data-type', val);

            if (searchoption) {
                idFieldWrapper.removeClass('hidden');
                idField.prop('required', true);
            }
            else {
                idFieldWrapper.addClass('hidden');
                idField.html(tmpl("selectoption-template", {id: val}));
                idField.prop('required', false);
            }
Naomi Guyer's avatar
Naomi Guyer committed
534
        }
Robert Lyon's avatar
Robert Lyon committed
535
536
537
538
539
540
541
542
543
544

        function resetIdField(idField) {
            idField.select2('val', '');
            idField.attr('data-roles','');

            // Reset and remove selected option if set manually
            if (idField.find('option:selected') !== undefined) {
                idField.find('option:selected').prop('selected', false);
                idField.html('');
            }
Naomi Guyer's avatar
Naomi Guyer committed
545
        }
Robert Lyon's avatar
Robert Lyon committed
546
547
548
549
550
551
552
553
554

        function setTypeField(self) {
            var value = $(self).val(),
                selectedOption =  $(self).find('option:selected');

            // Friend is a type of user, so we need a special case so we can still search for friends
            // but the backend will recieve 'user' as the type attribute
            if (value === 'friend') {
                value = 'user';
Naomi Guyer's avatar
Naomi Guyer committed
555
556
            }

Robert Lyon's avatar
Robert Lyon committed
557
558
559
            if (selectedOption.attr('data-type') !== undefined) {
                value =  selectedOption.attr('data-type');
            }
Naomi Guyer's avatar
Naomi Guyer committed
560

Robert Lyon's avatar
Robert Lyon committed
561
            $(self).siblings('[data-settype]').val(value);
Naomi Guyer's avatar
Naomi Guyer committed
562
        }
Robert Lyon's avatar
Robert Lyon committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577


        function attachShareTypeEvent(scope) {

            setIDField(scope.find(".js-share-type"), true);

            // Make search box visible only when friends, groups, or users is selected
            scope.find(".js-share-type").on('change', function() {
                setIDField(this, false);
                setTypeField(this);
                hideRoleSelect(this);
            });
        }

        function attachCommentEvents(newrow) {
578
            if ($('#{{$formname}}_allowcomments').prop('checked') === true) {
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
                // Hide the per row comment options
                newrow.find('.commentcolumn').addClass('hidden');
            }

            var allowcommentsbox = newrow.find('.allow-comments-checkbox');
            var moderatecommentsbox = newrow.find('.moderate-comments-checkbox');
            // TODO: could probably use a function to eliminate the duplicate code between
            // here and the change event handler
            if (allowcommentsbox.prop('checked') == false) {
                moderatecommentsbox.prop("disabled", true).prop("checked", false);
            }
            else {
                moderatecommentsbox.prop('disabled', false);
            }
            allowcommentsbox.on('change', function() {
Robert Lyon's avatar
Robert Lyon committed
594
                if ($(this).prop('checked') == false) {
595
                    moderatecommentsbox.prop("disabled", true).prop("checked", false);
Robert Lyon's avatar
Robert Lyon committed
596
597
                }
                else {
598
                    moderatecommentsbox.prop('disabled', false);
Robert Lyon's avatar
Robert Lyon committed
599
600
601
602
603
604
605
606
607
                }
            });
        }

        var rows = $('#accesslistitems > tr'),
            i,
            shareoptions = shareWithOptions(rows[i]);

        renderAccessList(shareoptions);
608
        setDatePicker($( ".js-date-picker > div > input" ));
Naomi Guyer's avatar
Naomi Guyer committed
609
610
611
612
    });
});

</script>