From c0851b5a7fc8604be45da6a0e202c74039b28df3 Mon Sep 17 00:00:00 2001 From: rolux Date: Tue, 22 May 2012 17:07:34 +0200 Subject: [PATCH] use common map & filter --- static/js/pandora.js | 18 ++++++------ static/js/pandora/URL.js | 24 +++++++++------- static/js/pandora/filesView.js | 4 +-- static/js/pandora/filterForm.js | 2 +- static/js/pandora/findElement.js | 9 +++--- static/js/pandora/infoView.0xdb.js | 6 ++-- static/js/pandora/infoView.js | 6 ++-- static/js/pandora/infoView.padma.js | 6 ++-- static/js/pandora/list.js | 40 +++++++++++++-------------- static/js/pandora/menu.js | 8 ++++-- static/js/pandora/sortSelect.js | 9 +++--- static/js/pandora/statisticsDialog.js | 9 +++--- static/js/pandora/usersDialog.js | 6 ++-- static/js/pandora/utils.js | 12 ++++---- static/js/pandora/videoPreview.js | 2 +- static/js/pandora/viewSelect.js | 8 +++--- 16 files changed, 90 insertions(+), 79 deletions(-) diff --git a/static/js/pandora.js b/static/js/pandora.js index 0e1f57117..000c09aeb 100644 --- a/static/js/pandora.js +++ b/static/js/pandora.js @@ -177,7 +177,7 @@ appPanel var prefix = '/static/'; if (localStorage && localStorage['pandora.debug']) { Ox.getJSON(prefix + 'json/pandora.json?' + Ox.random(1000), function(files) { - Ox.loadFiles(Ox.map(files, function(file) { + Ox.loadFiles(files.map(function(file) { return prefix + file; }), callback); }); @@ -227,13 +227,13 @@ appPanel }) ? 'manual' : data.site.layers.some(function(layer) { return layer.hasEvents; }) ? 'auto' : 'none', - clipKeys: Ox.map(data.site.clipKeys, function(key) { + clipKeys: data.site.clipKeys.map(function(key) { return Ox.extend(key, { operator: pandora.getSortOperator(key.id) }); }), - findKeys: Ox.map(data.site.itemKeys, function(key) { - return key.find ? key : null; + findKeys: data.site.itemKeys.filter(function(key) { + return key.find; }), itemsSection: pandora.site.itemName.plural.toLowerCase(), map: data.site.layers.some(function(layer) { @@ -259,14 +259,16 @@ appPanel {id: 'featured', title: 'Featured Texts', showBrowser: false} ] }, - sortKeys: Ox.map(pandora.site.itemKeys, function(key) { - return key.sort ? Ox.extend(key, { + sortKeys: pandora.site.itemKeys.filter(function(key) { + return key.sort; + }).map(function(key) { + return Ox.extend(key, { operator: pandora.getSortOperator(key.id) - }) : null; + }); }) }); pandora.site.listSettings = {}; - Ox.map(pandora.site.user.ui, function(val, key) { + Ox.forEach(pandora.site.user.ui, function(val, key) { if (/^list[A-Z]/.test(key)) { pandora.site.listSettings[key] = key[4].toLowerCase() + key.substr(5); } diff --git a/static/js/pandora/URL.js b/static/js/pandora/URL.js index 137339239..39e7898dd 100644 --- a/static/js/pandora/URL.js +++ b/static/js/pandora/URL.js @@ -176,18 +176,22 @@ pandora.URL = (function() { list: Ox.merge( // listView is the default view [pandora.user.ui.listView], - Ox.map(pandora.site.listViews, function(view) { - return view.id == pandora.user.ui.listView ? null : view.id; + pandora.site.listViews.filter(function(view) { + return view.id == pandora.user.ui.listView + }).map(function(view) { + return view.id; }) ), item: Ox.merge( // itemView is the default view, // videoView is the default view if there is a duration [pandora.user.ui.itemView, pandora.user.ui.videoView], - Ox.map(pandora.site.itemViews, function(view) { + pandora.site.itemViews.filter(function(view) { return [ pandora.user.ui.itemView, pandora.user.ui.videoView - ].indexOf(view.id) > -1 ? null : view.id; + ].indexOf(view.id) > -1; + }).map(function(view) { + return view.id; }) ) }; @@ -200,11 +204,11 @@ pandora.URL = (function() { || pandora.isClipView(view) && Ox.getObjectById(pandora.site.clipKeys, pandora.user.ui.listSort[0].key) || [], - pandora.isClipView(view) ? Ox.map(pandora.site.clipKeys, function(key) { - return key.id == pandora.user.ui.listSort[0].key ? null : key; + pandora.isClipView(view) ? pandora.site.clipKeys.filter(function(key) { + return key.id == pandora.user.ui.listSort[0].key; }) : [], - Ox.map(pandora.site.sortKeys, function(key) { - return key.id == pandora.user.ui.listSort[0].key ? null : key; + pandora.site.sortKeys.filter(function(key) { + return key.id == pandora.user.ui.listSort[0].key; }) ); }); @@ -213,8 +217,8 @@ pandora.URL = (function() { sortKeys[itemsSection].item[view] = Ox.merge( // itemSort[0].key is the default sort key [Ox.getObjectById(pandora.site.clipKeys, pandora.user.ui.itemSort[0].key)], - Ox.map(pandora.site.clipKeys, function(key) { - return key.id == pandora.user.ui.itemSort[0].key ? null : key; + pandora.site.clipKeys.filter(function(key) { + return key.id == pandora.user.ui.itemSort[0].key; }) ); } diff --git a/static/js/pandora/filesView.js b/static/js/pandora/filesView.js index 7abff02f5..10d1af9cd 100644 --- a/static/js/pandora/filesView.js +++ b/static/js/pandora/filesView.js @@ -311,9 +311,9 @@ pandora.ui.filesView = function(options, self) { }, function(result) { */ conditions = {}; - Ox.map(['id', 'title', 'director', 'year'], function(key) { + ['id', 'title', 'director', 'year'].map(function(key) { var value = self['$' + key + 'Input'].value(); - if(value.length) { + if (value.length) { conditions[key] = key == 'director' ? value.split(', ') : value; } }); diff --git a/static/js/pandora/filterForm.js b/static/js/pandora/filterForm.js index 086d8f403..3b7fd3853 100644 --- a/static/js/pandora/filterForm.js +++ b/static/js/pandora/filterForm.js @@ -15,7 +15,7 @@ pandora.ui.filterForm = function(list) { }, function(result) { that.append( that.$filter = Ox.Filter({ - findKeys: Ox.merge(Ox.map(pandora.site.itemKeys, function(itemKey) { + findKeys: Ox.merge(pandora.site.itemKeys.map(function(itemKey) { var key = Ox.clone(itemKey); key.type = key.type == 'layer' ? Ox.getObjectById(pandora.site.layers, key.id).type diff --git a/static/js/pandora/findElement.js b/static/js/pandora/findElement.js index 6fe5d0c74..a902d221d 100644 --- a/static/js/pandora/findElement.js +++ b/static/js/pandora/findElement.js @@ -28,13 +28,14 @@ pandora.ui.findElement = function() { pandora.$ui.findSelect = Ox.Select({ id: 'select', items: Ox.merge( - Ox.map(pandora.site.findKeys, function(key, i) { + pandora.site.findKeys.filter(function(key, i) { return !key.capability - || pandora.site.capabilities[key.capability][pandora.user.level] - ? { + || pandora.site.capabilities[key.capability][pandora.user.level]; + }).map(function(key) { + return { id: key.id, title: 'Find: ' + key.title, - } : null; + }; }), [{}, { id: 'advanced', diff --git a/static/js/pandora/infoView.0xdb.js b/static/js/pandora/infoView.0xdb.js index 842947728..e6c2b8d3d 100644 --- a/static/js/pandora/infoView.0xdb.js +++ b/static/js/pandora/infoView.0xdb.js @@ -643,9 +643,9 @@ pandora.ui.infoView = function(data) { Ox.Button({ tooltip: (canEdit ? Ox.toTitleCase(userLevel) : 'You') + ' ' + (hasCapability ? 'can' : 'can\'t') + ' ' - + Ox.map(Ox.toSlashes(capability.name).split('/'), function(word, i) { - return i == 0 ? null : word.toLowerCase(); - }).join(' ') + + Ox.toSlashes(capability.name) + .split('/').slice(1).join(' ') + .toLowerCase() .replace('see item', 'see the item') .replace('play video', 'play the full video') .replace('download video', 'download the video'), diff --git a/static/js/pandora/infoView.js b/static/js/pandora/infoView.js index c4a543805..9024ccec2 100644 --- a/static/js/pandora/infoView.js +++ b/static/js/pandora/infoView.js @@ -616,9 +616,9 @@ pandora.ui.infoView = function(data) { Ox.Button({ tooltip: (canEdit ? Ox.toTitleCase(userLevel) : 'You') + ' ' + (hasCapability ? 'can' : 'can\'t') + ' ' - + Ox.map(Ox.toSlashes(capability.name).split('/'), function(word, i) { - return i == 0 ? null : word.toLowerCase(); - }).join(' '), + + Ox.toSlashes(capability.name) + .split('/').slice(1).join(' ') + .toLowerCase(), title: capability.symbol, type: 'image' }) diff --git a/static/js/pandora/infoView.padma.js b/static/js/pandora/infoView.padma.js index c4a543805..9024ccec2 100644 --- a/static/js/pandora/infoView.padma.js +++ b/static/js/pandora/infoView.padma.js @@ -616,9 +616,9 @@ pandora.ui.infoView = function(data) { Ox.Button({ tooltip: (canEdit ? Ox.toTitleCase(userLevel) : 'You') + ' ' + (hasCapability ? 'can' : 'can\'t') + ' ' - + Ox.map(Ox.toSlashes(capability.name).split('/'), function(word, i) { - return i == 0 ? null : word.toLowerCase(); - }).join(' '), + + Ox.toSlashes(capability.name) + .split('/').slice(1).join(' ') + .toLowerCase(), title: capability.symbol, type: 'image' }) diff --git a/static/js/pandora/list.js b/static/js/pandora/list.js index 02654881b..2aeb9f5d8 100644 --- a/static/js/pandora/list.js +++ b/static/js/pandora/list.js @@ -52,27 +52,27 @@ pandora.ui.list = function() { titleImage: pandora.user.ui.icons == 'posters' ? 'SetPoster' : 'Icon', visible: pandora.user.ui.listColumns.indexOf('posterRatio') > -1, width: 16 - }], Ox.map(pandora.site.sortKeys, function(key) { - var position = pandora.user.ui.listColumns.indexOf(key.id); + }], pandora.site.sortKeys.filter(function(key) { return !key.capability - || pandora.site.capabilities[key.capability][pandora.user.level] - ? { - align: ['string', 'text'].indexOf( - Ox.isArray(key.type) ? key.type[0]: key.type - ) > -1 ? 'left' : key.type == 'list' ? 'center' : 'right', - defaultWidth: key.columnWidth, - format: key.format, - id: key.id, - operator: pandora.getSortOperator(key.id), - position: position, - removable: !key.columnRequired, - title: key.title, - type: key.type, - unique: key.id == 'id', - visible: position > -1, - width: pandora.user.ui.listColumnWidth[key.id] || key.columnWidth - } - : null; + || pandora.site.capabilities[key.capability][pandora.user.level]; + }).map(function(key) { + var position = pandora.user.ui.listColumns.indexOf(key.id); + return { + align: ['string', 'text'].indexOf( + Ox.isArray(key.type) ? key.type[0]: key.type + ) > -1 ? 'left' : key.type == 'list' ? 'center' : 'right', + defaultWidth: key.columnWidth, + format: key.format, + id: key.id, + operator: pandora.getSortOperator(key.id), + position: position, + removable: !key.columnRequired, + title: key.title, + type: key.type, + unique: key.id == 'id', + visible: position > -1, + width: pandora.user.ui.listColumnWidth[key.id] || key.columnWidth + }; })), columnsMovable: true, columnsRemovable: true, diff --git a/static/js/pandora/menu.js b/static/js/pandora/menu.js index 1603dcd3c..de8bca6e1 100644 --- a/static/js/pandora/menu.js +++ b/static/js/pandora/menu.js @@ -602,13 +602,15 @@ pandora.ui.mainMenu = function() { { id: 'sortitems', title: 'Sort ' + (isClipView || ui.item ? 'Clips' : pandora.site.itemName.plural) + ' by', items: [ { group: 'listsort', min: 1, max: 1, items: Ox.merge( items, - Ox.map(pandora.site.sortKeys, function(key) { + pandora.site.sortKeys.filter(function(key) { return Ox.getIndexById(items, key.id) == -1 && ( !key.capability || pandora.site.capabilities[key.capability][pandora.user.level] - ) ? Ox.extend({ + ); + }).map(function(key) { + return Ox.extend({ checked: ui.listSort[0].key == key.id - }, key) : null; + }, key); }) ) } ] }, diff --git a/static/js/pandora/sortSelect.js b/static/js/pandora/sortSelect.js index e3e30dffa..6ad056cc3 100644 --- a/static/js/pandora/sortSelect.js +++ b/static/js/pandora/sortSelect.js @@ -15,13 +15,14 @@ pandora.ui.sortSelect = function(isNavigationView) { if (!pandora.user.ui.item) { items = Ox.merge( items, - Ox.map(pandora.site.sortKeys, function(key) { + pandora.site.sortKeys.filter(function(key) { return Ox.getIndexById(items, key.id) == -1 && ( !key.capability - || pandora.site.capabilities[key.capability][pandora.user.level] - ) ? Ox.extend(Ox.clone(key), { + || pandora.site.capabilities[key.capability][pandora.user.level]; + }).map(function(key) { + return Ox.extend(Ox.clone(key), { title: 'Sort by ' + key.title - }) : null; + }); }) ); } diff --git a/static/js/pandora/statisticsDialog.js b/static/js/pandora/statisticsDialog.js index 835878fa8..2b1e4bb3a 100644 --- a/static/js/pandora/statisticsDialog.js +++ b/static/js/pandora/statisticsDialog.js @@ -353,13 +353,14 @@ pandora.ui.statisticsDialog = function() { element: '', tooltip: mode == 'all' && (key == 'continent' || key == 'region') ? Ox.wordwrap( - Ox.map(Ox.COUNTRIES, function(country) { + Ox.COUNTRIES.filter(function(country) { return country[key] == split[key == 'continent' ? 0 : 1] && country.code.length == 2 - && ['AC', 'CP', 'DG', 'EA', 'EU', 'IC', 'TA', 'UK'].indexOf(country.code) == -1 + && !country.exception && !country.disputed - && !country.dissolved - ? country.name : null; + && !country.dissolved; + }).map(function(country) { + return country.name; }).sort().join(', '), 64, '
', true ).split(', ').map(function(country) { diff --git a/static/js/pandora/usersDialog.js b/static/js/pandora/usersDialog.js index aa4745edb..907c79e13 100644 --- a/static/js/pandora/usersDialog.js +++ b/static/js/pandora/usersDialog.js @@ -659,11 +659,11 @@ pandora.ui.usersDialog = function() { }), Ox.Select({ id: 'level', - items: Ox.map(pandora.site.userLevels, function(level, i) { - return i ? { + items: pandora.site.userLevels.slice(1).map(function(level) { + return { id: level, title: Ox.toTitleCase(level) - } : null; + }; }), label: 'Level', labelWidth: 80, diff --git a/static/js/pandora/utils.js b/static/js/pandora/utils.js index 2de2ae603..30564dbe6 100644 --- a/static/js/pandora/utils.js +++ b/static/js/pandora/utils.js @@ -596,13 +596,13 @@ pandora.getItemByIdOrTitle = function(str, callback) { }, function(result) { var id = ''; if (result.data.items.length) { - var items = Ox.map(result.data.items, function(item) { + var items = Ox.filter(Ox.map(result.data.items, function(item) { // test if exact match or word match var sort = new RegExp('^' + str + '$', 'i').test(item.title) ? 2000000 : new RegExp('\\b' + str + '\\b', 'i').test(item.title) ? 1000000 : 0; return sort ? {id: item.id, sort: sort + (parseInt(item[sortKey]) || 0)} : null; // fixme: remove the (...|| 0) check once the backend sends correct data - }); + })); if (items.length) { id = items.sort(function(a, b) { return b.sort - a.sort; @@ -1170,12 +1170,12 @@ pandora.unloadWindow = function() { // If exactly one condition has the given key and operator // (including or excluding conditions where all subconditions match) // returns the corresponding index, otherwise returns -1 - var indices = Ox.map(conditions, function(condition, i) { + var indices = Ox.indicesOf(conditions, function(condition) { return ( condition.conditions ? includeSubconditions && everyCondition(condition.conditions, key, operator) : condition.key == key && condition.operator == operator - ) ? i : null; + ); }); return indices.length == 1 ? indices[0] : -1; } @@ -1245,9 +1245,9 @@ pandora.unloadWindow = function() { return filter.index > -1; }).length; // indices of non-advanced find queries - indices = Ox.map(pandora.site.findKeys, function(findKey) { + indices = Ox.indicesOf(pandora.site.findKeys, function(findKey) { var index = oneCondition(find.conditions, findKey.id, '='); - return index > -1 ? index : null; + return index > -1; }); state = conditions == 1 && indices.length == 1 ? { index: indices[0], diff --git a/static/js/pandora/videoPreview.js b/static/js/pandora/videoPreview.js index 0fdb8141d..771874b86 100644 --- a/static/js/pandora/videoPreview.js +++ b/static/js/pandora/videoPreview.js @@ -6,7 +6,7 @@ pandora.ui.videoPreview = function(data) { var that = Ox.VideoPreview({ duration: data.duration, getFrame: function(position) { - var resolutions = Ox.filter(pandora.site.video.resolutions, function(resolution, i) { + var resolutions = pandora.site.video.resolutions.filter(function(resolution, i) { return resolution >= data.height; }), resolution = resolutions.length diff --git a/static/js/pandora/viewSelect.js b/static/js/pandora/viewSelect.js index fdc01df29..b211eefa1 100644 --- a/static/js/pandora/viewSelect.js +++ b/static/js/pandora/viewSelect.js @@ -6,10 +6,10 @@ pandora.ui.viewSelect = function() { var ui = pandora.user.ui, sortKey = !ui.item ? 'listSort' : 'itemSort', viewKey = !ui.item ? 'listView' : 'itemView', - items = Ox.map(pandora.site[viewKey + 's'], function(view) { - return ['data', 'files'].indexOf(view.id) == -1 - ? {id: view.id, title: 'View ' + view.title} - : null; + items = pandora.site[viewKey + 's'].filter(function(view) { + return ['data', 'files'].indexOf(view.id) == -1; + }).map(function(view) { + return {id: view.id, title: 'View ' + view.title}; }), that; if (