From 273fdb7b642b79b1d1b0d6abb31d684b6f2ed66f Mon Sep 17 00:00:00 2001 From: Thomas Tanghus Date: Mon, 22 Oct 2012 15:40:08 +0200 Subject: [PATCH] Added type and callback arguments to most methods. --- core/js/oc-vcategories.js | 141 +++++++++++++++++++++++++------------- 1 file changed, 95 insertions(+), 46 deletions(-) diff --git a/core/js/oc-vcategories.js b/core/js/oc-vcategories.js index f08df9c069..53065933be 100644 --- a/core/js/oc-vcategories.js +++ b/core/js/oc-vcategories.js @@ -1,12 +1,13 @@ var OCCategories= { - edit:function() { - if(OCCategories.type == undefined) { - OC.dialogs.alert('OCCategories.type is not set!'); - return; + category_favorites:'_$!!$_', + edit:function(type, cb) { + if(!type && !this.type) { + throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') }; } + type = type ? type : this.type; $('body').append('
'); $('#category_dialog').load( - OC.filePath('core', 'ajax', 'vcategories/edit.php') + '?type=' + OCCategories.type, function(response) { + OC.filePath('core', 'ajax', 'vcategories/edit.php') + '?type=' + type, function(response) { try { var jsondata = jQuery.parseJSON(response); if(response.status == 'error') { @@ -27,8 +28,8 @@ var OCCategories= { modal: true, height: 350, minHeight:200, width: 250, minWidth: 200, buttons: { - 'Close': function() { - $(this).dialog('close'); + 'Close': function() { + $(this).dialog('close'); }, 'Delete':function() { var categories = $('#categorylist').find('input:checkbox').serialize(); @@ -72,83 +73,131 @@ var OCCategories= { } }); }, - _processDeleteResult:function(jsondata, cb) { + _processDeleteResult:function(jsondata) { if(jsondata.status == 'success') { OCCategories._update(jsondata.data.categories); } else { OC.dialogs.alert(jsondata.data.message, 'Error'); } - if(typeof cb == 'function') { - cb(); - } }, favorites:function(type, cb) { + if(!type && !this.type) { + throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') }; + } + type = type ? type : this.type; $.getJSON(OC.filePath('core', 'ajax', 'categories/favorites.php'), {type: type},function(jsondata) { - if(jsondata.status === 'success') { - OCCategories._update(jsondata.data.categories); + if(typeof cb == 'function') { + cb(jsondata); } else { - OC.dialogs.alert(jsondata.data.message, t('core', 'Error')); + if(jsondata.status === 'success') { + OCCategories._update(jsondata.data.categories); + } else { + OC.dialogs.alert(jsondata.data.message, t('core', 'Error')); + } } }); }, - addToFavorites:function(id, type) { + addToFavorites:function(id, type, cb) { + if(!type && !this.type) { + throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') }; + } + type = type ? type : this.type; $.post(OC.filePath('core', 'ajax', 'vcategories/addToFavorites.php'), {id:id, type:type}, function(jsondata) { - if(jsondata.status !== 'success') { - OC.dialogs.alert(jsondata.data.message, 'Error'); + if(typeof cb == 'function') { + cb(jsondata); + } else { + if(jsondata.status !== 'success') { + OC.dialogs.alert(jsondata.data.message, 'Error'); + } } }); }, - removeFromFavorites:function(id, type) { + removeFromFavorites:function(id, type, cb) { + if(!type && !this.type) { + throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') }; + } + type = type ? type : this.type; $.post(OC.filePath('core', 'ajax', 'vcategories/removeFromFavorites.php'), {id:id, type:type}, function(jsondata) { - if(jsondata.status !== 'success') { - OC.dialogs.alert(jsondata.data.message, t('core', 'Error')); + if(typeof cb == 'function') { + cb(jsondata); + } else { + if(jsondata.status !== 'success') { + OC.dialogs.alert(jsondata.data.message, t('core', 'Error')); + } } }); }, - doDelete:function(categories, cb) { + doDelete:function(categories, type, cb) { + if(!type && !this.type) { + throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') }; + } + type = type ? type : this.type; if(categories == '' || categories == undefined) { OC.dialogs.alert(t('core', 'No categories selected for deletion.'), t('core', 'Error')); return false; } - var q = categories + '&type=' + OCCategories.type; - if(OCCategories.app) { - q += '&app=' + OCCategories.app; - $.post(OC.filePath(OCCategories.app, 'ajax', 'categories/delete.php'), q, function(jsondata) { - OCCategories._processDeleteResult(jsondata, cb) + var self = this; + var q = categories + '&type=' + type; + if(this.app) { + q += '&app=' + this.app; + $.post(OC.filePath(this.app, 'ajax', 'categories/delete.php'), q, function(jsondata) { + if(typeof cb == 'function') { + cb(jsondata); + } else { + self._processDeleteResult(jsondata); + } }); } else { $.post(OC.filePath('core', 'ajax', 'vcategories/delete.php'), q, function(jsondata) { - OCCategories._processDeleteResult(jsondata, cb) + if(typeof cb == 'function') { + cb(jsondata); + } else { + self._processDeleteResult(jsondata); + } }); } }, - add:function(category) { - $.post(OC.filePath('core', 'ajax', 'vcategories/add.php'),{'category':category, 'app':OCCategories.app},function(jsondata) { - if(jsondata.status === 'success') { - OCCategories._update(jsondata.data.categories); + add:function(category, type, cb) { + if(!type && !this.type) { + throw { name: 'MissingParameter', message: t('core', 'The object type is not specified.') }; + } + type = type ? type : this.type; + $.post(OC.filePath('core', 'ajax', 'vcategories/add.php'),{'category':category, 'type':type},function(jsondata) { + if(typeof cb == 'function') { + cb(jsondata); } else { - OC.dialogs.alert(jsondata.data.message, 'Error'); + if(jsondata.status === 'success') { + OCCategories._update(jsondata.data.categories); + } else { + OC.dialogs.alert(jsondata.data.message, 'Error'); + } } }); }, - rescan:function(cb) { - $.getJSON(OC.filePath(OCCategories.app, 'ajax', 'categories/rescan.php'),function(jsondata, status, xhr) { - if(jsondata.status === 'success') { - OCCategories._update(jsondata.data.categories); - } else { - OC.dialogs.alert(jsondata.data.message, 'Error'); - } + rescan:function(app, cb) { + if(!app && !this.app) { + throw { name: 'MissingParameter', message: t('core', 'The app name is not specified.') }; + } + app = app ? app : this.app; + $.getJSON(OC.filePath(app, 'ajax', 'categories/rescan.php'),function(jsondata, status, xhr) { if(typeof cb == 'function') { - cb(); + cb(jsondata); + } else { + if(jsondata.status === 'success') { + OCCategories._update(jsondata.data.categories); + } else { + OC.dialogs.alert(jsondata.data.message, 'Error'); + } } }).error(function(xhr){ if (xhr.status == 404) { - OC.dialogs.alert( - t('core', 'The required file {file} is not installed!', - {file: OC.filePath(OCCategories.app, 'ajax', 'categories/rescan.php')}, t('core', 'Error'))); - } - if(typeof cb == 'function') { - cb(); + var errormessage = t('core', 'The required file {file} is not installed!', + {file: OC.filePath(app, 'ajax', 'categories/rescan.php')}, t('core', 'Error')); + if(typeof cb == 'function') { + cb({status:'error', data:{message:errormessage}}); + } else { + OC.dialogs.alert(errormessage); + } } }); },