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

7
    <table id="accesslisttable" class="fullwidth accesslists table form-inline">
8
        <thead>
Robert Lyon's avatar
Robert Lyon committed
9
10
11
12
13
14
            <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" }}
15
16
                <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
17
                {{/if}}
18
19
            </tr>
        </thead>
Robert Lyon's avatar
Robert Lyon committed
20
        <tbody id ="accesslistitems" data-id="accesslistitems">
21
22
        </tbody>
    </table>
Naomi Guyer's avatar
Naomi Guyer committed
23
</div>
Robert Lyon's avatar
Robert Lyon committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37

<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%}">
38
    <td class="text-center with-icon tiny">
39
        <a class="{% if (o.presets.locked || o.presets.empty) { %}icon-placeholder{% } %} text-block" data-bind="remove-share" href="#" id="remove-share{%=o.id%}">
40
            <span class="text-danger icon icon-lg icon-trash" role="presentation" aria-hidden="true"></span>
Robert Lyon's avatar
Robert Lyon committed
41
42
43
44
45
            <span class="sr-only">{%={{jstr tag=remove section=view}}%}</span>
        </a>
    </td>
    <td>
        <div class="dropdown-group dropdown-single-option">
46
            <span class="picker input-short">
Robert Lyon's avatar
Robert Lyon committed
47
48
49
50
51
52
53
54
55
56
57
58
                <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++) { %}
59
                            <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
60
61
62
63
64
                        {% } %}
                    </optgroup>

                    <optgroup label="{%={{jstr tag=institutions section=view}}%}" id="potentialpresetitemsinstitutions">
                        {% for (var i=0; i<o.shareoptions.institutions.length; i++) { %}
65
                            <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
66
67
68
69
                        {% } %}
                    </optgroup>
                    <optgroup label="{%={{jstr tag=groups section=view}}%}" id="potentialpresetitemsgroups">
                        {% for (var i=0; i<o.shareoptions.myGroups.length; i++) { %}
70
                            <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
71
72
73
74
75
76
77
                            {%=o.shareoptions.myGroups[i].name%}
                            </option>
                        {% } %}
                    </optgroup>
                </select>
            </span>
            {% if(o.presets.empty) { %}<p class="table-help-text">{%={{jstr tag=whosharewith section=view}}%}</p>{% } %}
78
            <div class="hidden picker input-short" data-select-wrapper="true">
Robert Lyon's avatar
Robert Lyon committed
79
80
81
82
                <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>
83
            <span class="picker input-short{% if (!o.presets.role) { %} hidden{% } %}">
84
                <select data-roles="grouproles" name="accesslist[{%=o.id%}][role]" class="form-control input-small select">
Robert Lyon's avatar
Robert Lyon committed
85
86
87
88
89
90
91
                    {% if (o.presets.role) { %}<option value="{%=o.presets.role%}" selected>{%=o.presets.roledisplay%}</option>{% } %}
                </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{% } %}">
92
            <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="Date from (Edit the field using the following format, year / month / day, hours colon minutes, a m or p m.)" {% if (o.presets.locked) { %}disabled{% } %}></div>
Robert Lyon's avatar
Robert Lyon committed
93
94
95
96
        </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{% } %}">
97
            <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="Date to (Edit the field using the following format, year / month / day, hours colon minutes, a m or p m.)" value="{%=o.presets.stopdate%}" {% if (o.presets.locked) { %}disabled{% } %}></div>
Robert Lyon's avatar
Robert Lyon committed
98
99
100
        </div>
    </td>
    {% if (o.viewtype !== "profile") { %}
101
102
        <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
103
        </td>
104
105
        <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
106
107
108
109
110
111
        </td>
    {% } %}

</tr>
</script>

112
<script type="application/javascript">
Naomi Guyer's avatar
Naomi Guyer committed
113
114
var count = 0;

Robert Lyon's avatar
Robert Lyon committed
115
116
117
118
119
jQuery(function($) {
"use strict";

    $(document).ready(function() {

120
121
122
123
        // 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'
124
125
        $j('#{{$formname}}_submit').click(function(e) {
            $j('#{{$formname}} select:required').each(function() {
126
127
                if ($j(this).val()) {
                    $j(this).prop("required", false);
128
                    $j(this).parent().parent().find('div.errmsg').remove();
129
130
                }
                else {
131
132
133
134
135
                    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>');
                    }
136
137
138
139
140
                    e.preventDefault();
                    e.stopPropagation();
                }
            });
        });
141
142
143
144
145
146
        // Remove 'required' on cancel
        $j('#cancel_{{$formname}}_submit').click(function(e) {
            $j('#{{$formname}} select:required').each(function() {
                $j(this).prop("required", false);
            });
        });
147

Robert Lyon's avatar
Robert Lyon committed
148
        function setDatePicker(target) {
149
            var loc = '{{strstr(current_language(), '.', true)}}'; // Get current langauge to use for locale
Robert Lyon's avatar
Robert Lyon committed
150
            target.datetimepicker({
151
152
153
154
155
156
157
158
159
                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",
160
                },
161
162
163
            }).on('dp.hide', function(selectedDate) {
                if (selectedDate !== "") {
                    formchangemanager.setFormStateById('{{$formname}}', FORM_CHANGED);
Robert Lyon's avatar
Robert Lyon committed
164
                }
Naomi Guyer's avatar
Naomi Guyer committed
165
            });
Robert Lyon's avatar
Robert Lyon committed
166
167
168
169
170
        }

        function formatSelect2Results (data) {
            if (data.loading) {
                return data.text;
Naomi Guyer's avatar
Naomi Guyer committed
171
            }
Robert Lyon's avatar
Robert Lyon committed
172
173
174
175
176
177
178
179
180

            var markup;

            // Need to know which row
            if (data.grouptype !== undefined) {
                markup = data.name;
            }
            else {
                markup =
181
                '<img class="select2-user-icon" src="' + config.wwwroot + 'thumb.php?type=profileicon&maxwidth=40&maxheight=40&id=' + data.id + '" />' +
182
                '<span>' + data.name + '</span>';
Robert Lyon's avatar
Robert Lyon committed
183
184
            }
            return markup;
Naomi Guyer's avatar
Naomi Guyer committed
185
        }
Robert Lyon's avatar
Robert Lyon committed
186
187

        function formatSelect2Selected (data) {
188
            if (data._resultId !== undefined) {
189
                formchangemanager.setFormStateById('{{$formname}}', FORM_CHANGED);
190
            }
Robert Lyon's avatar
Robert Lyon committed
191
192
193
194
            if (data.grouptype !== undefined) {
                return '<span data-grouptype="'+ data.grouptype + '">'+ data.name + '</span>';
            }
            else {
195
                return data.name || data.text;
Robert Lyon's avatar
Robert Lyon committed
196
            }
Naomi Guyer's avatar
Naomi Guyer committed
197
        }
Robert Lyon's avatar
Robert Lyon committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

        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,
215
                            'page': params.page || 0,
Robert Lyon's avatar
Robert Lyon committed
216
217
218
219
220
221
222
223
224
225
226
227
                            '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 {
228
229
230
231
                            results: (data.message.count > 0) ? data.message.data : [],
                            pagination: {
                                more: data.message.more
                            }
Robert Lyon's avatar
Robert Lyon committed
232
233
234
235
236
237
238
                        };
                    },
                    cache: true
                  },
                escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                templateResult: formatSelect2Results,
                templateSelection: formatSelect2Selected,
239
                maximumSelectionLength: 20,
Robert Lyon's avatar
Robert Lyon committed
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
                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);
                }
269
                formchangemanager.setFormStateById('{{$formname}}', FORM_CHANGED);
Robert Lyon's avatar
Robert Lyon committed
270
            });
Naomi Guyer's avatar
Naomi Guyer committed
271
        }
Robert Lyon's avatar
Robert Lyon committed
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

        /*
         * 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
294
            addNewRow(shareoptions, {empty: true});
Naomi Guyer's avatar
Naomi Guyer committed
295
        }
Robert Lyon's avatar
Robert Lyon committed
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

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

            var data,
                lastrow,
                id,
                viewtype = $('[data-viewtype]').attr('data-viewtype');

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

            data = {
                id: id,
                shareoptions: shareoptions,
                presets: presets,
                viewtype: viewtype
            };

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

            attachEventListeners(id);
Naomi Guyer's avatar
Naomi Guyer committed
325
        }
Robert Lyon's avatar
Robert Lyon committed
326
327
328
329

        function attachEventListeners(id) {
            var newrow = $('#accesslistitems').find('[data-id="' + id + '"]');
            attachShareTypeEvent(newrow);
330
            setDatePicker($(newrow).find('.js-date-picker > div > input'));
Robert Lyon's avatar
Robert Lyon committed
331
332
333
334
335
336
337
338
339
            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);
340
                    formchangemanager.setFormStateById('{{$formname}}', FORM_CHANGED);
Robert Lyon's avatar
Robert Lyon committed
341
342
                }
            });
Naomi Guyer's avatar
Naomi Guyer committed
343
344
        }

Robert Lyon's avatar
Robert Lyon committed
345
346
347
348
349
350
351
352
353
354
355
356
357
        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});
                }
358
                formchangemanager.setFormStateById('{{$formname}}', FORM_CHANGED);
Robert Lyon's avatar
Robert Lyon committed
359
360
            });
        }
Naomi Guyer's avatar
Naomi Guyer committed
361

Robert Lyon's avatar
Robert Lyon committed
362
363
364
365
366
367
368
        function clearRow(self) {
            $(self).closest('tr').remove();

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

Robert Lyon's avatar
Robert Lyon committed
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
        /*
         * 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,
395
396
                    'selected': item.preset,
                    'locked': item.locked
Robert Lyon's avatar
Robert Lyon committed
397
398
                };
            }
Naomi Guyer's avatar
Naomi Guyer committed
399

Robert Lyon's avatar
Robert Lyon committed
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
            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
428
        }
Robert Lyon's avatar
Robert Lyon committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451


        /*
         * 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')),
                grouptype = $(self).parent().find('[data-grouptype]').attr('data-grouptype'),
                data,
                defaultText = {{jstr tag=everyoneingroup section=view}},
                id = $(self).closest('tr').attr('data-id'),
                select = $(self).closest('.dropdown-group').find('[data-roles="grouproles"]');

            data = {
                id: id,
                defaultText: defaultText,
                roles: roles[grouptype]
            };

            select.html(tmpl("roles-template", data));
            select.prop('disabled', false).parent().removeClass('hidden');
Naomi Guyer's avatar
Naomi Guyer committed
452
453
        }

Robert Lyon's avatar
Robert Lyon committed
454
455
456
        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
457
        }
Robert Lyon's avatar
Robert Lyon committed
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

        /*
         * 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
489
        }
Robert Lyon's avatar
Robert Lyon committed
490
491
492
493
494
495
496
497
498
499

        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
500
        }
Robert Lyon's avatar
Robert Lyon committed
501
502
503
504
505
506
507
508
509

        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
510
511
            }

Robert Lyon's avatar
Robert Lyon committed
512
513
514
            if (selectedOption.attr('data-type') !== undefined) {
                value =  selectedOption.attr('data-type');
            }
Naomi Guyer's avatar
Naomi Guyer committed
515

Robert Lyon's avatar
Robert Lyon committed
516
            $(self).siblings('[data-settype]').val(value);
Naomi Guyer's avatar
Naomi Guyer committed
517
        }
Robert Lyon's avatar
Robert Lyon committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532


        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) {
533
            if ($('#{{$formname}}_allowcomments').prop('checked') === true) {
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
                // 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
549
                if ($(this).prop('checked') == false) {
550
                    moderatecommentsbox.prop("disabled", true).prop("checked", false);
Robert Lyon's avatar
Robert Lyon committed
551
552
                }
                else {
553
                    moderatecommentsbox.prop('disabled', false);
Robert Lyon's avatar
Robert Lyon committed
554
555
556
557
558
559
560
561
562
                }
            });
        }

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

        renderAccessList(shareoptions);
563
        setDatePicker($( ".js-date-picker > div > input" ));
Naomi Guyer's avatar
Naomi Guyer committed
564
565
566
567
    });
});

</script>