use common map & filter

This commit is contained in:
rolux 2012-05-22 17:07:34 +02:00
parent 98af37696f
commit c0851b5a7f
16 changed files with 90 additions and 79 deletions

View file

@ -177,7 +177,7 @@ appPanel
var prefix = '/static/'; var prefix = '/static/';
if (localStorage && localStorage['pandora.debug']) { if (localStorage && localStorage['pandora.debug']) {
Ox.getJSON(prefix + 'json/pandora.json?' + Ox.random(1000), function(files) { 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; return prefix + file;
}), callback); }), callback);
}); });
@ -227,13 +227,13 @@ appPanel
}) ? 'manual' : data.site.layers.some(function(layer) { }) ? 'manual' : data.site.layers.some(function(layer) {
return layer.hasEvents; return layer.hasEvents;
}) ? 'auto' : 'none', }) ? 'auto' : 'none',
clipKeys: Ox.map(data.site.clipKeys, function(key) { clipKeys: data.site.clipKeys.map(function(key) {
return Ox.extend(key, { return Ox.extend(key, {
operator: pandora.getSortOperator(key.id) operator: pandora.getSortOperator(key.id)
}); });
}), }),
findKeys: Ox.map(data.site.itemKeys, function(key) { findKeys: data.site.itemKeys.filter(function(key) {
return key.find ? key : null; return key.find;
}), }),
itemsSection: pandora.site.itemName.plural.toLowerCase(), itemsSection: pandora.site.itemName.plural.toLowerCase(),
map: data.site.layers.some(function(layer) { map: data.site.layers.some(function(layer) {
@ -259,14 +259,16 @@ appPanel
{id: 'featured', title: 'Featured Texts', showBrowser: false} {id: 'featured', title: 'Featured Texts', showBrowser: false}
] ]
}, },
sortKeys: Ox.map(pandora.site.itemKeys, function(key) { sortKeys: pandora.site.itemKeys.filter(function(key) {
return key.sort ? Ox.extend(key, { return key.sort;
}).map(function(key) {
return Ox.extend(key, {
operator: pandora.getSortOperator(key.id) operator: pandora.getSortOperator(key.id)
}) : null; });
}) })
}); });
pandora.site.listSettings = {}; 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)) { if (/^list[A-Z]/.test(key)) {
pandora.site.listSettings[key] = key[4].toLowerCase() + key.substr(5); pandora.site.listSettings[key] = key[4].toLowerCase() + key.substr(5);
} }

View file

@ -176,18 +176,22 @@ pandora.URL = (function() {
list: Ox.merge( list: Ox.merge(
// listView is the default view // listView is the default view
[pandora.user.ui.listView], [pandora.user.ui.listView],
Ox.map(pandora.site.listViews, function(view) { pandora.site.listViews.filter(function(view) {
return view.id == pandora.user.ui.listView ? null : view.id; return view.id == pandora.user.ui.listView
}).map(function(view) {
return view.id;
}) })
), ),
item: Ox.merge( item: Ox.merge(
// itemView is the default view, // itemView is the default view,
// videoView is the default view if there is a duration // videoView is the default view if there is a duration
[pandora.user.ui.itemView, pandora.user.ui.videoView], [pandora.user.ui.itemView, pandora.user.ui.videoView],
Ox.map(pandora.site.itemViews, function(view) { pandora.site.itemViews.filter(function(view) {
return [ return [
pandora.user.ui.itemView, pandora.user.ui.videoView 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) || pandora.isClipView(view)
&& Ox.getObjectById(pandora.site.clipKeys, pandora.user.ui.listSort[0].key) && Ox.getObjectById(pandora.site.clipKeys, pandora.user.ui.listSort[0].key)
|| [], || [],
pandora.isClipView(view) ? Ox.map(pandora.site.clipKeys, function(key) { pandora.isClipView(view) ? pandora.site.clipKeys.filter(function(key) {
return key.id == pandora.user.ui.listSort[0].key ? null : key; return key.id == pandora.user.ui.listSort[0].key;
}) : [], }) : [],
Ox.map(pandora.site.sortKeys, function(key) { pandora.site.sortKeys.filter(function(key) {
return key.id == pandora.user.ui.listSort[0].key ? null : key; return key.id == pandora.user.ui.listSort[0].key;
}) })
); );
}); });
@ -213,8 +217,8 @@ pandora.URL = (function() {
sortKeys[itemsSection].item[view] = Ox.merge( sortKeys[itemsSection].item[view] = Ox.merge(
// itemSort[0].key is the default sort key // itemSort[0].key is the default sort key
[Ox.getObjectById(pandora.site.clipKeys, pandora.user.ui.itemSort[0].key)], [Ox.getObjectById(pandora.site.clipKeys, pandora.user.ui.itemSort[0].key)],
Ox.map(pandora.site.clipKeys, function(key) { pandora.site.clipKeys.filter(function(key) {
return key.id == pandora.user.ui.itemSort[0].key ? null : key; return key.id == pandora.user.ui.itemSort[0].key;
}) })
); );
} }

View file

@ -311,9 +311,9 @@ pandora.ui.filesView = function(options, self) {
}, function(result) { }, function(result) {
*/ */
conditions = {}; conditions = {};
Ox.map(['id', 'title', 'director', 'year'], function(key) { ['id', 'title', 'director', 'year'].map(function(key) {
var value = self['$' + key + 'Input'].value(); var value = self['$' + key + 'Input'].value();
if(value.length) { if (value.length) {
conditions[key] = key == 'director' ? value.split(', ') : value; conditions[key] = key == 'director' ? value.split(', ') : value;
} }
}); });

View file

@ -15,7 +15,7 @@ pandora.ui.filterForm = function(list) {
}, function(result) { }, function(result) {
that.append( that.append(
that.$filter = Ox.Filter({ 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); var key = Ox.clone(itemKey);
key.type = key.type == 'layer' key.type = key.type == 'layer'
? Ox.getObjectById(pandora.site.layers, key.id).type ? Ox.getObjectById(pandora.site.layers, key.id).type

View file

@ -28,13 +28,14 @@ pandora.ui.findElement = function() {
pandora.$ui.findSelect = Ox.Select({ pandora.$ui.findSelect = Ox.Select({
id: 'select', id: 'select',
items: Ox.merge( items: Ox.merge(
Ox.map(pandora.site.findKeys, function(key, i) { pandora.site.findKeys.filter(function(key, i) {
return !key.capability 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, id: key.id,
title: 'Find: ' + key.title, title: 'Find: ' + key.title,
} : null; };
}), }),
[{}, { [{}, {
id: 'advanced', id: 'advanced',

View file

@ -643,9 +643,9 @@ pandora.ui.infoView = function(data) {
Ox.Button({ Ox.Button({
tooltip: (canEdit ? Ox.toTitleCase(userLevel) : 'You') + ' ' tooltip: (canEdit ? Ox.toTitleCase(userLevel) : 'You') + ' '
+ (hasCapability ? 'can' : 'can\'t') + ' ' + (hasCapability ? 'can' : 'can\'t') + ' '
+ Ox.map(Ox.toSlashes(capability.name).split('/'), function(word, i) { + Ox.toSlashes(capability.name)
return i == 0 ? null : word.toLowerCase(); .split('/').slice(1).join(' ')
}).join(' ') .toLowerCase()
.replace('see item', 'see the item') .replace('see item', 'see the item')
.replace('play video', 'play the full video') .replace('play video', 'play the full video')
.replace('download video', 'download the video'), .replace('download video', 'download the video'),

View file

@ -616,9 +616,9 @@ pandora.ui.infoView = function(data) {
Ox.Button({ Ox.Button({
tooltip: (canEdit ? Ox.toTitleCase(userLevel) : 'You') + ' ' tooltip: (canEdit ? Ox.toTitleCase(userLevel) : 'You') + ' '
+ (hasCapability ? 'can' : 'can\'t') + ' ' + (hasCapability ? 'can' : 'can\'t') + ' '
+ Ox.map(Ox.toSlashes(capability.name).split('/'), function(word, i) { + Ox.toSlashes(capability.name)
return i == 0 ? null : word.toLowerCase(); .split('/').slice(1).join(' ')
}).join(' '), .toLowerCase(),
title: capability.symbol, title: capability.symbol,
type: 'image' type: 'image'
}) })

View file

@ -616,9 +616,9 @@ pandora.ui.infoView = function(data) {
Ox.Button({ Ox.Button({
tooltip: (canEdit ? Ox.toTitleCase(userLevel) : 'You') + ' ' tooltip: (canEdit ? Ox.toTitleCase(userLevel) : 'You') + ' '
+ (hasCapability ? 'can' : 'can\'t') + ' ' + (hasCapability ? 'can' : 'can\'t') + ' '
+ Ox.map(Ox.toSlashes(capability.name).split('/'), function(word, i) { + Ox.toSlashes(capability.name)
return i == 0 ? null : word.toLowerCase(); .split('/').slice(1).join(' ')
}).join(' '), .toLowerCase(),
title: capability.symbol, title: capability.symbol,
type: 'image' type: 'image'
}) })

View file

@ -52,27 +52,27 @@ pandora.ui.list = function() {
titleImage: pandora.user.ui.icons == 'posters' ? 'SetPoster' : 'Icon', titleImage: pandora.user.ui.icons == 'posters' ? 'SetPoster' : 'Icon',
visible: pandora.user.ui.listColumns.indexOf('posterRatio') > -1, visible: pandora.user.ui.listColumns.indexOf('posterRatio') > -1,
width: 16 width: 16
}], Ox.map(pandora.site.sortKeys, function(key) { }], pandora.site.sortKeys.filter(function(key) {
var position = pandora.user.ui.listColumns.indexOf(key.id);
return !key.capability return !key.capability
|| pandora.site.capabilities[key.capability][pandora.user.level] || pandora.site.capabilities[key.capability][pandora.user.level];
? { }).map(function(key) {
align: ['string', 'text'].indexOf( var position = pandora.user.ui.listColumns.indexOf(key.id);
Ox.isArray(key.type) ? key.type[0]: key.type return {
) > -1 ? 'left' : key.type == 'list' ? 'center' : 'right', align: ['string', 'text'].indexOf(
defaultWidth: key.columnWidth, Ox.isArray(key.type) ? key.type[0]: key.type
format: key.format, ) > -1 ? 'left' : key.type == 'list' ? 'center' : 'right',
id: key.id, defaultWidth: key.columnWidth,
operator: pandora.getSortOperator(key.id), format: key.format,
position: position, id: key.id,
removable: !key.columnRequired, operator: pandora.getSortOperator(key.id),
title: key.title, position: position,
type: key.type, removable: !key.columnRequired,
unique: key.id == 'id', title: key.title,
visible: position > -1, type: key.type,
width: pandora.user.ui.listColumnWidth[key.id] || key.columnWidth unique: key.id == 'id',
} visible: position > -1,
: null; width: pandora.user.ui.listColumnWidth[key.id] || key.columnWidth
};
})), })),
columnsMovable: true, columnsMovable: true,
columnsRemovable: true, columnsRemovable: true,

View file

@ -602,13 +602,15 @@ pandora.ui.mainMenu = function() {
{ id: 'sortitems', title: 'Sort ' + (isClipView || ui.item ? 'Clips' : pandora.site.itemName.plural) + ' by', items: [ { id: 'sortitems', title: 'Sort ' + (isClipView || ui.item ? 'Clips' : pandora.site.itemName.plural) + ' by', items: [
{ group: 'listsort', min: 1, max: 1, items: Ox.merge( { group: 'listsort', min: 1, max: 1, items: Ox.merge(
items, items,
Ox.map(pandora.site.sortKeys, function(key) { pandora.site.sortKeys.filter(function(key) {
return Ox.getIndexById(items, key.id) == -1 && ( return Ox.getIndexById(items, key.id) == -1 && (
!key.capability !key.capability
|| pandora.site.capabilities[key.capability][pandora.user.level] || pandora.site.capabilities[key.capability][pandora.user.level]
) ? Ox.extend({ );
}).map(function(key) {
return Ox.extend({
checked: ui.listSort[0].key == key.id checked: ui.listSort[0].key == key.id
}, key) : null; }, key);
}) })
) } ) }
] }, ] },

View file

@ -15,13 +15,14 @@ pandora.ui.sortSelect = function(isNavigationView) {
if (!pandora.user.ui.item) { if (!pandora.user.ui.item) {
items = Ox.merge( items = Ox.merge(
items, items,
Ox.map(pandora.site.sortKeys, function(key) { pandora.site.sortKeys.filter(function(key) {
return Ox.getIndexById(items, key.id) == -1 && ( return Ox.getIndexById(items, key.id) == -1 && (
!key.capability !key.capability
|| pandora.site.capabilities[key.capability][pandora.user.level] || pandora.site.capabilities[key.capability][pandora.user.level];
) ? Ox.extend(Ox.clone(key), { }).map(function(key) {
return Ox.extend(Ox.clone(key), {
title: 'Sort by ' + key.title title: 'Sort by ' + key.title
}) : null; });
}) })
); );
} }

View file

@ -353,13 +353,14 @@ pandora.ui.statisticsDialog = function() {
element: '<img>', element: '<img>',
tooltip: mode == 'all' && (key == 'continent' || key == 'region') tooltip: mode == 'all' && (key == 'continent' || key == 'region')
? Ox.wordwrap( ? Ox.wordwrap(
Ox.map(Ox.COUNTRIES, function(country) { Ox.COUNTRIES.filter(function(country) {
return country[key] == split[key == 'continent' ? 0 : 1] return country[key] == split[key == 'continent' ? 0 : 1]
&& country.code.length == 2 && country.code.length == 2
&& ['AC', 'CP', 'DG', 'EA', 'EU', 'IC', 'TA', 'UK'].indexOf(country.code) == -1 && !country.exception
&& !country.disputed && !country.disputed
&& !country.dissolved && !country.dissolved;
? country.name : null; }).map(function(country) {
return country.name;
}).sort().join(', '), }).sort().join(', '),
64, '<br>', true 64, '<br>', true
).split(', ').map(function(country) { ).split(', ').map(function(country) {

View file

@ -659,11 +659,11 @@ pandora.ui.usersDialog = function() {
}), }),
Ox.Select({ Ox.Select({
id: 'level', id: 'level',
items: Ox.map(pandora.site.userLevels, function(level, i) { items: pandora.site.userLevels.slice(1).map(function(level) {
return i ? { return {
id: level, id: level,
title: Ox.toTitleCase(level) title: Ox.toTitleCase(level)
} : null; };
}), }),
label: 'Level', label: 'Level',
labelWidth: 80, labelWidth: 80,

View file

@ -596,13 +596,13 @@ pandora.getItemByIdOrTitle = function(str, callback) {
}, function(result) { }, function(result) {
var id = ''; var id = '';
if (result.data.items.length) { 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 // test if exact match or word match
var sort = new RegExp('^' + str + '$', 'i').test(item.title) ? 2000000 var sort = new RegExp('^' + str + '$', 'i').test(item.title) ? 2000000
: new RegExp('\\b' + str + '\\b', 'i').test(item.title) ? 1000000 : 0; : new RegExp('\\b' + str + '\\b', 'i').test(item.title) ? 1000000 : 0;
return sort ? {id: item.id, sort: sort + (parseInt(item[sortKey]) || 0)} : null; return sort ? {id: item.id, sort: sort + (parseInt(item[sortKey]) || 0)} : null;
// fixme: remove the (...|| 0) check once the backend sends correct data // fixme: remove the (...|| 0) check once the backend sends correct data
}); }));
if (items.length) { if (items.length) {
id = items.sort(function(a, b) { id = items.sort(function(a, b) {
return b.sort - a.sort; return b.sort - a.sort;
@ -1170,12 +1170,12 @@ pandora.unloadWindow = function() {
// If exactly one condition has the given key and operator // If exactly one condition has the given key and operator
// (including or excluding conditions where all subconditions match) // (including or excluding conditions where all subconditions match)
// returns the corresponding index, otherwise returns -1 // returns the corresponding index, otherwise returns -1
var indices = Ox.map(conditions, function(condition, i) { var indices = Ox.indicesOf(conditions, function(condition) {
return ( return (
condition.conditions condition.conditions
? includeSubconditions && everyCondition(condition.conditions, key, operator) ? includeSubconditions && everyCondition(condition.conditions, key, operator)
: condition.key == key && condition.operator == operator : condition.key == key && condition.operator == operator
) ? i : null; );
}); });
return indices.length == 1 ? indices[0] : -1; return indices.length == 1 ? indices[0] : -1;
} }
@ -1245,9 +1245,9 @@ pandora.unloadWindow = function() {
return filter.index > -1; return filter.index > -1;
}).length; }).length;
// indices of non-advanced find queries // 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, '='); var index = oneCondition(find.conditions, findKey.id, '=');
return index > -1 ? index : null; return index > -1;
}); });
state = conditions == 1 && indices.length == 1 ? { state = conditions == 1 && indices.length == 1 ? {
index: indices[0], index: indices[0],

View file

@ -6,7 +6,7 @@ pandora.ui.videoPreview = function(data) {
var that = Ox.VideoPreview({ var that = Ox.VideoPreview({
duration: data.duration, duration: data.duration,
getFrame: function(position) { 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; return resolution >= data.height;
}), }),
resolution = resolutions.length resolution = resolutions.length

View file

@ -6,10 +6,10 @@ pandora.ui.viewSelect = function() {
var ui = pandora.user.ui, var ui = pandora.user.ui,
sortKey = !ui.item ? 'listSort' : 'itemSort', sortKey = !ui.item ? 'listSort' : 'itemSort',
viewKey = !ui.item ? 'listView' : 'itemView', viewKey = !ui.item ? 'listView' : 'itemView',
items = Ox.map(pandora.site[viewKey + 's'], function(view) { items = pandora.site[viewKey + 's'].filter(function(view) {
return ['data', 'files'].indexOf(view.id) == -1 return ['data', 'files'].indexOf(view.id) == -1;
? {id: view.id, title: 'View ' + view.title} }).map(function(view) {
: null; return {id: view.id, title: 'View ' + view.title};
}), }),
that; that;
if ( if (