cleanup variable names and duplicate jQuery selectors

This commit is contained in:
Jörn Friedrich Dreyer 2014-10-13 18:40:57 +02:00
parent 7f1ba86789
commit 7535b09851
1 changed files with 80 additions and 76 deletions

View File

@ -19,28 +19,28 @@ function getSelection($row) {
return values; return values;
} }
function highlightBorder(element, highlight) { function highlightBorder($element, highlight) {
$(element).toggleClass('warning-input', highlight); $element.toggleClass('warning-input', highlight);
return highlight; return highlight;
} }
function highlightInput(input) { function highlightInput($input) {
if ($(input).attr('type') === 'text' || $(input).attr('type') === 'password') { if ($input.attr('type') === 'text' || $input.attr('type') === 'password') {
return highlightBorder(input, return highlightBorder($input,
($(input).val() === '' && !$(input).hasClass('optional'))); ($input.val() === '' && !$input.hasClass('optional')));
} }
} }
OC.MountConfig={ OC.MountConfig={
saveStorage:function(tr, callback) { saveStorage:function($tr, callback) {
var mountPoint = $(tr).find('.mountPoint input').val(); var mountPoint = $tr.find('.mountPoint input').val();
var oldMountPoint = $(tr).find('.mountPoint input').data('mountpoint'); var oldMountPoint = $tr.find('.mountPoint input').data('mountpoint');
if (mountPoint === '') { if (mountPoint === '') {
return false; return false;
} }
var statusSpan = $(tr).closest('tr').find('.status span'); var statusSpan = $tr.find('.status span');
var backendClass = $(tr).find('.backend').data('class'); var backendClass = $tr.find('.backend').data('class');
var configuration = $(tr).find('.configuration input'); var configuration = $tr.find('.configuration input');
var addMountPoint = true; var addMountPoint = true;
if (configuration.length < 1) { if (configuration.length < 1) {
return false; return false;
@ -62,14 +62,14 @@ OC.MountConfig={
} }
}); });
if ($('#externalStorage').data('admin') === true) { if ($('#externalStorage').data('admin') === true) {
var multiselect = getSelection($(tr)); var multiselect = getSelection($tr);
} }
if (addMountPoint) { if (addMountPoint) {
var status = false; var status = false;
if ($('#externalStorage').data('admin') === true) { if ($('#externalStorage').data('admin') === true) {
var isPersonal = false; var isPersonal = false;
var oldGroups = $(tr).find('.applicable').data('applicable-groups'); var oldGroups = $tr.find('.applicable').data('applicable-groups');
var oldUsers = $(tr).find('.applicable').data('applicable-users'); var oldUsers = $tr.find('.applicable').data('applicable-users');
var groups = []; var groups = [];
var users = []; var users = [];
$.each(multiselect, function(index, value) { $.each(multiselect, function(index, value) {
@ -102,7 +102,7 @@ OC.MountConfig={
oldMountPoint: oldMountPoint oldMountPoint: oldMountPoint
}, },
success: function(result) { success: function(result) {
$(tr).find('.mountPoint input').data('mountpoint', mountPoint); $tr.find('.mountPoint input').data('mountpoint', mountPoint);
status = updateStatus(statusSpan, result); status = updateStatus(statusSpan, result);
if (callback) { if (callback) {
callback(status); callback(status);
@ -116,8 +116,8 @@ OC.MountConfig={
} }
}); });
}); });
$(tr).find('.applicable').data('applicable-groups', groups); $tr.find('.applicable').data('applicable-groups', groups);
$(tr).find('.applicable').data('applicable-users', users); $tr.find('.applicable').data('applicable-users', users);
var mountType = 'group'; var mountType = 'group';
$.each(oldGroups, function(index, applicable) { $.each(oldGroups, function(index, applicable) {
$.ajax({type: 'POST', $.ajax({type: 'POST',
@ -163,7 +163,7 @@ OC.MountConfig={
oldMountPoint: oldMountPoint oldMountPoint: oldMountPoint
}, },
success: function(result) { success: function(result) {
$(tr).find('.mountPoint input').data('mountpoint', mountPoint); $tr.find('.mountPoint input').data('mountpoint', mountPoint);
status = updateStatus(statusSpan, result); status = updateStatus(statusSpan, result);
if (callback) { if (callback) {
callback(status); callback(status);
@ -183,20 +183,23 @@ OC.MountConfig={
}; };
$(document).ready(function() { $(document).ready(function() {
var $externalStorage = $('#externalStorage');
//initialize hidden input field with list of users and groups //initialize hidden input field with list of users and groups
$('#externalStorage').find('tr:not(#addMountPoint)').each(function(i,tr) { $externalStorage.find('tr:not(#addMountPoint)').each(function(i,tr) {
var applicable = $(tr).find('.applicable'); var $tr = $(tr);
if (applicable.length > 0) { var $applicable = $tr.find('.applicable');
var groups = applicable.data('applicable-groups'); if ($applicable.length > 0) {
var groups = $applicable.data('applicable-groups');
var groupsId = []; var groupsId = [];
$.each(groups, function () { $.each(groups, function () {
groupsId.push(this+"(group)"); groupsId.push(this + '(group)');
}); });
var users = applicable.data('applicable-users'); var users = $applicable.data('applicable-users');
if (users.indexOf('all') > -1) { if (users.indexOf('all') > -1) {
$(tr).find('.applicableUsers').val(''); $tr.find('.applicableUsers').val('');
} else { } else {
$(tr).find('.applicableUsers').val(groupsId.concat(users).join(',')); $tr.find('.applicableUsers').val(groupsId.concat(users).join(','));
} }
} }
}); });
@ -221,7 +224,7 @@ $(document).ready(function() {
}; };
}, },
results: function (data, page) { results: function (data, page) {
if (data.status === "success") { if (data.status === 'success') {
var results = []; var results = [];
var userCount = 0; // users is an object var userCount = 0; // users is an object
@ -256,10 +259,10 @@ $(document).ready(function() {
type: 'POST', type: 'POST',
contentType: 'application/json', contentType: 'application/json',
data: JSON.stringify(users), data: JSON.stringify(users),
dataType: "json" dataType: 'json'
}).done(function(data) { }).done(function(data) {
var results = []; var results = [];
if (data.status === "success") { if (data.status === 'success') {
$.each(data.users, function(user, displayname) { $.each(data.users, function(user, displayname) {
if (displayname !== false) { if (displayname !== false) {
results.push({name:user, displayname:displayname, type:'user'}); results.push({name:user, displayname:displayname, type:'user'});
@ -294,9 +297,9 @@ $(document).ready(function() {
} }
}, },
escapeMarkup: function (m) { return m; } // we escape the markup in formatResult and formatSelection escapeMarkup: function (m) { return m; } // we escape the markup in formatResult and formatSelection
}).on("select2-loaded", function() { }).on('select2-loaded', function() {
$.each($(".avatardiv"), function(i, div) { $.each($('.avatardiv'), function(i, div) {
$div = $(div); var $div = $(div);
if ($div.data('type') === 'user') { if ($div.data('type') === 'user') {
$div.avatar($div.data('name'),32); $div.avatar($div.data('name'),32);
} }
@ -306,21 +309,21 @@ $(document).ready(function() {
} }
addSelect2($('tr:not(#addMountPoint) .applicableUsers')); addSelect2($('tr:not(#addMountPoint) .applicableUsers'));
$('#externalStorage').on('change', '#selectBackend', function() { $externalStorage.on('change', '#selectBackend', function() {
var tr = $(this).closest("tr"); var $tr = $(this).closest('tr');
$('#externalStorage tbody').append($(tr).clone()); $externalStorage.find('tbody').append($tr.clone());
$('#externalStorage tbody tr').last().find('.mountPoint input').val(''); $externalStorage.find('tbody tr').last().find('.mountPoint input').val('');
var selected = $(this).find('option:selected').text(); var selected = $(this).find('option:selected').text();
var backendClass = $(this).val(); var backendClass = $(this).val();
$(tr).find('.backend').text(selected); $tr.find('.backend').text(selected);
if ($(tr).find('.mountPoint input').val() === '') { if ($tr.find('.mountPoint input').val() === '') {
$(tr).find('.mountPoint input').val(suggestMountPoint(selected)); $tr.find('.mountPoint input').val(suggestMountPoint(selected));
} }
$(tr).addClass(backendClass); $tr.addClass(backendClass);
$(tr).find('.status').append('<span></span>'); $tr.find('.status').append('<span></span>');
$(tr).find('.backend').data('class', backendClass); $tr.find('.backend').data('class', backendClass);
var configurations = $(this).data('configurations'); var configurations = $(this).data('configurations');
var td = $(tr).find('td.configuration'); var $td = $tr.find('td.configuration');
$.each(configurations, function(backend, parameters) { $.each(configurations, function(backend, parameters) {
if (backend === backendClass) { if (backend === backendClass) {
$.each(parameters['configuration'], function(parameter, placeholder) { $.each(parameters['configuration'], function(parameter, placeholder) {
@ -342,20 +345,20 @@ $(document).ready(function() {
newElement = $('<input type="text" class="added' + class_string + '" data-parameter="'+parameter+'" placeholder="'+placeholder+'" />'); newElement = $('<input type="text" class="added' + class_string + '" data-parameter="'+parameter+'" placeholder="'+placeholder+'" />');
} }
highlightInput(newElement); highlightInput(newElement);
td.append(newElement); $td.append(newElement);
}); });
if (parameters['custom'] && $('#externalStorage tbody tr.'+backendClass.replace(/\\/g, '\\\\')).length === 1) { if (parameters['custom'] && $externalStorage.find('tbody tr.'+backendClass.replace(/\\/g, '\\\\')).length === 1) {
OC.addScript('files_external', parameters['custom']); OC.addScript('files_external', parameters['custom']);
} }
td.children().not('[type=hidden]').first().focus(); $td.children().not('[type=hidden]').first().focus();
return false; return false;
} }
}); });
$(tr).find('td').last().attr('class', 'remove'); $tr.find('td').last().attr('class', 'remove');
$(tr).find('td').last().removeAttr('style'); $tr.find('td').last().removeAttr('style');
$(tr).removeAttr('id'); $tr.removeAttr('id');
$(this).remove(); $(this).remove();
addSelect2(tr.find('.applicableUsers')); addSelect2($tr.find('.applicableUsers'));
}); });
function suggestMountPoint(defaultMountPoint) { function suggestMountPoint(defaultMountPoint) {
@ -369,7 +372,7 @@ $(document).ready(function() {
var match = true; var match = true;
while (match && i < 20) { while (match && i < 20) {
match = false; match = false;
$('#externalStorage tbody td.mountPoint input').each(function(index, mountPoint) { $externalStorage.find('tbody td.mountPoint input').each(function(index, mountPoint) {
if ($(mountPoint).val() === defaultMountPoint+append) { if ($(mountPoint).val() === defaultMountPoint+append) {
match = true; match = true;
return false; return false;
@ -385,54 +388,54 @@ $(document).ready(function() {
return defaultMountPoint+append; return defaultMountPoint+append;
} }
$('#externalStorage').on('paste', 'td input', function() { $externalStorage.on('paste', 'td input', function() {
var tr = $(this).closest("tr"); var $me = $(this);
var me = this; var $tr = $me.closest('tr');
setTimeout(function() { setTimeout(function() {
highlightInput($(me)); highlightInput($me);
OC.MountConfig.saveStorage(tr); OC.MountConfig.saveStorage($tr);
}, 20); }, 20);
}); });
var timer; var timer;
$('#externalStorage').on('keyup', 'td input', function() { $externalStorage.on('keyup', 'td input', function() {
clearTimeout(timer); clearTimeout(timer);
var tr = $(this).closest("tr"); var $tr = $(this).closest('tr');
highlightInput($(this)); highlightInput($(this));
if ($(this).val) { if ($(this).val) {
timer = setTimeout(function() { timer = setTimeout(function() {
OC.MountConfig.saveStorage(tr); OC.MountConfig.saveStorage($tr);
}, 2000); }, 2000);
} }
}); });
$('#externalStorage').on('change', 'td input:checkbox', function() { $externalStorage.on('change', 'td input:checkbox', function() {
OC.MountConfig.saveStorage($(this).closest("tr")); OC.MountConfig.saveStorage($(this).closest('tr'));
}); });
$('#externalStorage').on('change', '.applicable', function() { $externalStorage.on('change', '.applicable', function() {
OC.MountConfig.saveStorage($(this).closest("tr")); OC.MountConfig.saveStorage($(this).closest('tr'));
}); });
$('#externalStorage').on('click', '.status>span', function() { $externalStorage.on('click', '.status>span', function() {
OC.MountConfig.saveStorage($(this).closest("tr")); OC.MountConfig.saveStorage($(this).closest('tr'));
}); });
$('#sslCertificate').on('click', 'td.remove>img', function() { $('#sslCertificate').on('click', 'td.remove>img', function() {
var $tr = $(this).closest("tr"); var $tr = $(this).closest('tr');
$.post(OC.filePath('files_external', 'ajax', 'removeRootCertificate.php'), {cert: $tr.attr('id')}); $.post(OC.filePath('files_external', 'ajax', 'removeRootCertificate.php'), {cert: $tr.attr('id')});
$tr.remove(); $tr.remove();
return true; return true;
}); });
$('#externalStorage').on('click', 'td.remove>img', function() { $externalStorage.on('click', 'td.remove>img', function() {
var tr = $(this).closest('tr'); var $tr = $(this).closest('tr');
var mountPoint = $(tr).find('.mountPoint input').val(); var mountPoint = $tr.find('.mountPoint input').val();
if ($('#externalStorage').data('admin') === true) { if ($externalStorage.data('admin') === true) {
var isPersonal = false; var isPersonal = false;
var multiselect = getSelection($(tr)); var multiselect = getSelection($tr);
$.each(multiselect, function(index, value) { $.each(multiselect, function(index, value) {
var pos = value.indexOf('(group)'); var pos = value.indexOf('(group)');
if (pos != -1) { if (pos != -1) {
@ -450,10 +453,11 @@ $(document).ready(function() {
var isPersonal = true; var isPersonal = true;
$.post(OC.filePath('files_external', 'ajax', 'removeMountPoint.php'), { mountPoint: mountPoint, mountType: mountType, applicable: applicable, isPersonal: isPersonal }); $.post(OC.filePath('files_external', 'ajax', 'removeMountPoint.php'), { mountPoint: mountPoint, mountType: mountType, applicable: applicable, isPersonal: isPersonal });
} }
$(tr).remove(); $tr.remove();
}); });
$('#allowUserMounting').bind('change', function() { var $allowUserMounting = $('#allowUserMounting');
$allowUserMounting.bind('change', function() {
OC.msg.startSaving('#userMountingMsg'); OC.msg.startSaving('#userMountingMsg');
if (this.checked) { if (this.checked) {
OC.AppConfig.setValue('files_external', 'allow_user_mounting', 'yes'); OC.AppConfig.setValue('files_external', 'allow_user_mounting', 'yes');
@ -475,8 +479,8 @@ $(document).ready(function() {
// disable allowUserMounting // disable allowUserMounting
if(userMountingBackends.length === 0) { if(userMountingBackends.length === 0) {
$('#allowUserMounting').prop('checked', false); $allowUserMounting.prop('checked', false);
$('#allowUserMounting').trigger('change'); $allowUserMounting.trigger('change');
} }
}); });