diff options
Diffstat (limited to 'apps/files_external/js/settings.js')
-rw-r--r-- | apps/files_external/js/settings.js | 172 |
1 files changed, 146 insertions, 26 deletions
diff --git a/apps/files_external/js/settings.js b/apps/files_external/js/settings.js index 8a1bb2bec6f..6c287d6a760 100644 --- a/apps/files_external/js/settings.js +++ b/apps/files_external/js/settings.js @@ -11,6 +11,14 @@ function updateStatus(statusEl, result){ } } +function getSelection($row) { + var values = $row.find('.applicableUsers').select2('val'); + if (!values || values.length === 0) { + values = ['all']; + } + return values; +} + OC.MountConfig={ saveStorage:function(tr, callback) { var mountPoint = $(tr).find('.mountPoint input').val(); @@ -42,10 +50,7 @@ OC.MountConfig={ } }); if ($('#externalStorage').data('admin') === true) { - var multiselect = $(tr).find('.chzn-select').val(); - if (multiselect == null) { - return false; - } + var multiselect = getSelection($(tr)); } if (addMountPoint) { var status = false; @@ -166,8 +171,129 @@ OC.MountConfig={ }; $(document).ready(function() { - $('.chzn-select').chosen(); + //initialize hidden input field with list of users and groups + $('#externalStorage').find('tr:not(#addMountPoint)').each(function(i,tr) { + var applicable = $(tr).find('.applicable'); + if (applicable.length > 0) { + var groups = applicable.data('applicable-groups'); + var groupsId = []; + $.each(groups, function () { + groupsId.push(this+"(group)"); + }); + var users = applicable.data('applicable-users'); + if (users.indexOf('all') > -1) { + $(tr).find('.applicableUsers').val(''); + } else { + $(tr).find('.applicableUsers').val(groupsId.concat(users).join(',')); + } + } + }); + var userListLimit = 30; + function addSelect2 ($elements) { + if ($elements.length > 0) { + $elements.select2({ + placeholder: t('files_external', 'All users. Type to select user or group.'), + allowClear: true, + multiple: true, + //minimumInputLength: 1, + ajax: { + url: OC.generateUrl('apps/files_external/applicable'), + dataType: 'json', + quietMillis: 100, + data: function (term, page) { // page is the one-based page number tracked by Select2 + return { + pattern: term, //search term + limit: userListLimit, // page size + offset: userListLimit*(page-1) // page number starts with 0 + }; + }, + results: function (data, page) { + if (data.status === "success") { + + var results = []; + var userCount = 0; // users is an object + + // add groups + $.each(data.groups, function(i, group) { + results.push({name:group+'(group)', displayname:group, type:'group' }); + }); + // add users + $.each(data.users, function(id, user) { + userCount++; + results.push({name:id, displayname:user, type:'user' }); + }); + + + var more = (userCount >= userListLimit) || (data.groups.length >= userListLimit); + return {results: results, more: more}; + } else { + //FIXME add error handling + } + } + }, + initSelection: function(element, callback) { + var users = {}; + users['users'] = []; + var toSplit = element.val().split(","); + for (var i = 0; i < toSplit.length; i++) { + users['users'].push(toSplit[i]); + } + + $.ajax(OC.generateUrl('displaynames'), { + type: 'POST', + contentType: 'application/json', + data: JSON.stringify(users), + dataType: "json" + }).done(function(data) { + var results = []; + if (data.status === "success") { + $.each(data.users, function(user, displayname) { + if (displayname !== false) { + results.push({name:user, displayname:displayname, type:'user'}); + } + }); + callback(results); + } else { + //FIXME add error handling + } + }); + }, + id: function(element) { + return element.name; + }, + formatResult: function (element) { + var $result = $('<span><div class="avatardiv"/><span>'+escapeHTML(element.displayname)+'</span></span>'); + var $div = $result.find('.avatardiv') + .attr('data-type', element.type) + .attr('data-name', element.name) + .attr('data-displayname', element.displayname); + if (element.type === 'group') { + var url = OC.imagePath('core','places/contacts-dark'); // TODO better group icon + $div.html('<img width="32" height="32" src="'+url+'">'); + } + return $result.get(0).outerHTML; + }, + formatSelection: function (element) { + if (element.type === 'group') { + return '<span title="'+escapeHTML(element.name)+'" class="group">'+escapeHTML(element.displayname+' '+t('files_external', '(group)'))+'</span>'; + } else { + return '<span title="'+escapeHTML(element.name)+'" class="user">'+escapeHTML(element.displayname)+'</span>'; + } + }, + escapeMarkup: function (m) { return m; } // we escape the markup in formatResult and formatSelection + }).on("select2-loaded", function() { + $.each($(".avatardiv"), function(i, div) { + $div = $(div); + if ($div.data('type') === 'user') { + $div.avatar($div.data('name'),32); + } + }) + }); + } + } + addSelect2($('tr:not(#addMountPoint) .applicableUsers')); + $('#externalStorage').on('change', '#selectBackend', function() { var tr = $(this).parent().parent(); $('#externalStorage tbody').append($(tr).clone()); @@ -209,15 +335,11 @@ $(document).ready(function() { return false; } }); - // Reset chosen - var chosen = $(tr).find('.applicable select'); - chosen.parent().find('div').remove(); - chosen.removeAttr('id').removeClass('chzn-done').css({display:'inline-block'}); - chosen.chosen(); $(tr).find('td').last().attr('class', 'remove'); $(tr).find('td').last().removeAttr('style'); $(tr).removeAttr('id'); $(this).remove(); + addSelect2($('tr:not(#addMountPoint) .applicableUsers')); }); function suggestMountPoint(defaultMountPoint) { @@ -270,8 +392,8 @@ $(document).ready(function() { OC.MountConfig.saveStorage($(this).parent().parent().parent()); }); - $('#externalStorage').on('change', '.applicable .chzn-select', function() { - OC.MountConfig.saveStorage($(this).parent().parent()); + $('#externalStorage').on('change', '.applicable', function() { + OC.MountConfig.saveStorage($(this).parent()); }); $('#sslCertificate').on('click', 'td.remove>img', function() { @@ -288,20 +410,18 @@ $(document).ready(function() { if ($('#externalStorage').data('admin') === true) { var isPersonal = false; - var multiselect = $(tr).find('.chzn-select').val(); - if (multiselect != null) { - $.each(multiselect, function(index, value) { - var pos = value.indexOf('(group)'); - if (pos != -1) { - var mountType = 'group'; - var applicable = value.substr(0, pos); - } else { - var mountType = 'user'; - var applicable = value; - } - $.post(OC.filePath('files_external', 'ajax', 'removeMountPoint.php'), { mountPoint: mountPoint, mountType: mountType, applicable: applicable, isPersonal: isPersonal }); - }); - } + var multiselect = getSelection($(tr)); + $.each(multiselect, function(index, value) { + var pos = value.indexOf('(group)'); + if (pos != -1) { + var mountType = 'group'; + var applicable = value.substr(0, pos); + } else { + var mountType = 'user'; + var applicable = value; + } + $.post(OC.filePath('files_external', 'ajax', 'removeMountPoint.php'), { mountPoint: mountPoint, mountType: mountType, applicable: applicable, isPersonal: isPersonal }); + }); } else { var mountType = 'user'; var applicable = OC.currentUser; |