pandora/static/js/folderList.js

490 lines
19 KiB
JavaScript
Raw Permalink Normal View History

2011-11-05 17:04:10 +00:00
'use strict';
2013-02-16 01:20:40 +00:00
2013-07-15 11:39:23 +00:00
pandora.ui.folderList = function(id, section) {
2018-03-07 07:54:24 +00:00
section = section || pandora.user.ui.section;
2013-02-16 01:20:40 +00:00
var ui = pandora.user.ui,
2013-07-15 11:39:23 +00:00
i = Ox.getIndexById(pandora.site.sectionFolders[section], id),
2016-10-04 22:00:03 +00:00
folderItems = pandora.getFolderItems(section),
2013-02-16 01:20:40 +00:00
folderItem = folderItems.slice(0, -1),
2017-11-04 09:53:27 +00:00
canEditFeatured = pandora.hasCapability('canEditFeatured' + folderItems),
$placeholder,
2011-05-25 19:42:45 +00:00
that;
2013-02-16 01:20:40 +00:00
var columns, items;
if (id != 'volumes') {
columns = [
{
clickable: function(data) {
return data.editable
2014-02-03 05:08:05 +00:00
|| (id == 'featured' && canEditFeatured);
2011-09-01 04:46:44 +00:00
},
2013-02-16 01:20:40 +00:00
format: function(value, data) {
return $('<img>').attr({
2016-10-04 22:00:03 +00:00
src: pandora.getListIcon(section, data.id, '', data.modified)
2013-02-16 01:20:40 +00:00
}).css({
width: '14px',
height: '14px',
borderRadius: '4px',
margin: '0 0 0 -3px'
});
2011-09-01 07:00:35 +00:00
},
2013-02-16 01:20:40 +00:00
id: 'user',
operator: '+',
tooltip: function(data) {
return data.editable
2013-02-16 01:20:40 +00:00
|| (id == 'featured' && canEditFeatured)
2013-05-09 10:13:58 +00:00
? Ox._('Edit Icon')
2013-02-16 01:20:40 +00:00
: '';
2011-09-01 04:46:44 +00:00
},
2013-02-16 01:20:40 +00:00
visible: true,
width: 16
},
{
format: function(value) {
return Ox.encodeHTMLEntities(value.split(':').join(': '));
2011-09-01 04:46:44 +00:00
},
2013-02-16 01:20:40 +00:00
id: 'id',
operator: '+',
visible: id == 'favorite',
// fixme: user and name are set to the same width here,
// but resizeFolders will set them to different widths
2014-02-08 17:34:12 +00:00
width: ui.sidebarWidth - (section != 'texts' ? 96 : 48)
2013-02-16 01:20:40 +00:00
},
{
editable: function(data) {
return data.editable;
2011-05-25 19:42:45 +00:00
},
2013-02-16 01:20:40 +00:00
format: function(value) {
return Ox.encodeHTMLEntities(value);
2011-05-25 19:42:45 +00:00
},
2013-02-16 01:20:40 +00:00
id: 'name',
input: {
autovalidate: pandora.ui.autovalidateListname
2011-05-25 19:42:45 +00:00
},
2013-02-16 01:20:40 +00:00
operator: '+',
2013-05-09 10:13:58 +00:00
tooltip: id == 'personal' ? Ox._('Edit Title') : '',
2013-02-16 01:20:40 +00:00
unformat: function(value) {
return Ox.decodeHTMLEntities(value);
2011-05-25 19:42:45 +00:00
},
2013-02-16 01:20:40 +00:00
visible: id != 'favorite',
2014-02-08 17:34:12 +00:00
width: ui.sidebarWidth - (section != 'texts' ? 96 : 48)
2013-02-16 01:20:40 +00:00
},
{
align: 'right',
2017-06-09 10:25:38 +00:00
clickable: true,
2013-02-16 01:20:40 +00:00
id: 'items',
format: {type: 'number'},
operator: '-',
2014-02-08 17:34:12 +00:00
visible: section != 'texts',
2013-02-16 01:20:40 +00:00
width: 48
},
{
clickable: function(data) {
return section != 'texts' && (
data.type == 'smart' || data.editable
2013-03-03 06:10:09 +00:00
);
2011-05-25 19:42:45 +00:00
},
2013-02-16 01:20:40 +00:00
format: function(value, data) {
return $('<img>')
.attr({
2014-09-26 12:30:51 +00:00
src: Ox.UI.getImageURL(
2013-03-03 06:05:25 +00:00
value == 'static' ? 'symbolClick'
: value == 'smart' ? 'symbolFind'
: value == 'html' ? 'symbolFile'
: 'symbolBook'
)
2013-02-16 01:20:40 +00:00
})
.css({
width: '10px',
height: '10px',
padding: '3px',
opacity: section == 'texts' || data.editable ? 1 : 0.25
});
2013-02-16 01:20:40 +00:00
},
id: 'type',
operator: '+',
tooltip: function(data) {
2013-03-03 06:05:25 +00:00
return data.type == 'static'
? (data.editable ? Ox._('Edit {0}', [Ox._(folderItem)]) : '')
2013-03-03 06:05:25 +00:00
: data.type == 'smart'
? (data.editable ? Ox._('Edit Query') : Ox._('Show Query'))
2013-03-03 06:05:25 +00:00
: data.type.toUpperCase();
2013-02-16 01:20:40 +00:00
},
visible: true,
width: 16
},
2013-02-16 01:20:40 +00:00
{
clickable: id == 'personal',
format: function(value) {
var symbols = {personal: 'Publish', favorite: 'Like', featured: 'Star'};
return $('<img>')
.attr({
2014-09-26 12:30:51 +00:00
src: Ox.UI.getImageURL(
2013-02-16 01:20:40 +00:00
'symbol' + symbols[id]
)
})
.css({
width: '10px',
height: '10px',
padding: '3px',
opacity: value == 'private' ? 0.25 : 1
});
},
id: 'status',
operator: '+',
tooltip: id == 'personal' ? function(data) {
2013-05-09 10:13:58 +00:00
return data.status == 'private' ? Ox._('Make Public') : Ox._('Make Private');
2013-02-16 01:20:40 +00:00
} : null,
visible: true,
width: 16
}
];
items = function(data, callback) {
var query;
if (id == 'personal') {
query = {conditions: [
{key: 'user', value: pandora.user.username, operator: '=='},
{key: 'status', value: 'featured', operator: '!='}
], operator: '&'};
} else if (id == 'favorite') {
query = {conditions: [
{key: 'subscribed', value: true, operator: '='},
{key: 'status', value: 'featured', operator: '!='}
], operator: '&'};
} else if (id == 'featured') {
query = {conditions: [
{key: 'status', value: 'featured', operator: '='}
2013-02-16 01:20:40 +00:00
], operator: '&'};
}
2018-11-13 16:31:03 +00:00
var _updateItemsCount = false;
if (data.keys && Ox.contains(data.keys, 'items')) {
data.keys = data.keys.filter(function(key) { return key != 'items' })
_updateItemsCount = true;
}
2013-02-16 01:20:40 +00:00
return pandora.api['find' + folderItems](Ox.extend(data, {
query: query
}), function(result) {
if (Ox.isArray(result.data.items)) {
pandora.$ui.mainMenu.updateLists(id, result.data.items);
}
callback(result);
2018-11-13 16:40:12 +00:00
if (
_updateItemsCount
&& Ox.isArray(result.data.items)
&& result.data.items.length
) {
2018-11-13 16:31:03 +00:00
setTimeout(function() {
updateItemsCount(result.data.items.map(function(item) {
return item.id;
}));
}, 500)
}
});
2013-02-16 01:20:40 +00:00
};
} else {
columns = [
{
format: function() {
return $('<img>').attr({
2014-09-26 12:30:51 +00:00
src: Ox.UI.getImageURL('symbolVolume')
2013-02-16 01:20:40 +00:00
}).css({
width: '10px',
height: '10px',
padding: '3px'
});
},
id: 'user',
operator: '+',
visible: true,
width: 16
2011-05-25 19:42:45 +00:00
},
2013-02-16 01:20:40 +00:00
{
editable: true,
id: 'name',
operator: '+',
2013-05-09 10:13:58 +00:00
tooltip: Ox._('Edit Title'),
2013-02-16 01:20:40 +00:00
visible: true,
width: ui.sidebarWidth - 96
},
{
align: 'right',
2017-06-09 10:25:38 +00:00
clickable: true,
2013-02-16 01:20:40 +00:00
id: 'items',
format: {type: 'number'},
operator: '-',
visible: true,
width: 48
},
{
clickable: function(data) {
return data.mounted;
},
format: function(value, data) {
return $('<img>')
.attr({
2014-09-26 12:30:51 +00:00
src: Ox.UI.getImageURL(data.mounted ? 'symbolSync' : 'symbolEdit')
2013-02-16 01:20:40 +00:00
})
.css({
width: '10px',
height: '10px',
padding: '3px'
});
},
id: 'path',
operator: '+',
tooltip: function(data) {
2013-05-09 10:13:58 +00:00
return data.mounted ? Ox._('Scan Volume') : Ox._('Edit Path');
2013-02-16 01:20:40 +00:00
},
visible: true,
width: 16
},
{
clickable: true,
format: function(value, data) {
return $('<img>')
.attr({
2014-09-26 12:30:51 +00:00
src: Ox.UI.getImageURL('symbolMount')
2013-02-16 01:20:40 +00:00
})
.css({
width: '10px',
height: '10px',
padding: '3px 2px 1px 2px',
opacity: data.mounted ? 1 : 0.25
});
},
id: 'mounted',
operator: '+',
tooltip: function(data) {
2013-05-09 10:13:58 +00:00
return data.mounted ? Ox._('Unmount Volume') : Ox._('Mount Volume');
2013-02-16 01:20:40 +00:00
},
visible: true,
width: 16
}
];
items = function(data, callback) {
var volumes = pandora.user.volumes || [];
if (!data.keys) {
data = {items: volumes.length};
} else {
data = {items: volumes.map(function(volume) {
return Ox.extend({id: volume.name, user: pandora.user.username}, volume);
})};
}
// fixme: ridiculous (we're binding to init too late)
setTimeout(function() {
callback({data: data});
}, 1000);
};
}
that = Ox.TableList({
columns: columns,
2013-08-02 09:23:23 +00:00
droppable: id != 'volumes',
2013-02-16 01:20:40 +00:00
items: items,
keys: ['modified', 'editable', 'groups'].concat(section != 'texts'
? ['query', 'name', 'view'] : ['rightslevel']),
2013-02-16 01:20:40 +00:00
max: 1,
min: 0,
pageLength: 1000,
// works when switching back from browser, but won't work on load, since
// getListData relies on $folderList, so selectList is called in init handler
selected: pandora.getListData().folder == id
? [ui[ui.section == 'items' ? '_list'
: ui.section == 'documents' ? '_collection'
: ui.section.slice(0, -1)]]
: [],
2013-02-16 01:20:40 +00:00
sort: [{key: 'position', operator: '+'}],
sortable: id != 'featured' || canEditFeatured,
unique: id != 'volumes' ? 'id' : 'name'
})
.css({
left: 0,
top: 0,
width: ui.sidebarWidth + 'px'
})
.bindEvent({
add: function(event) {
if (id == 'personal') {
if (event.keys == '' || event.keys == 'alt') {
if (section != 'texts') {
pandora.addFolderItem(section, event.keys == 'alt', false);
} else {
pandora.addText({type: event.keys == '' ? 'html' : 'pdf'});
}
2011-05-25 19:42:45 +00:00
}
2013-02-16 01:20:40 +00:00
} else if (id == 'favorite' || (id == 'featured' && canEditFeatured)) {
// this makes the button trigger a change event,
// which is already being handled in folders.js
pandora.$ui.manageListsButton[id].options({value: true});
/*
if (!pandora.site.sectionFolders.items[i].showBrowser) {
pandora.site.sectionFolders.items[i].showBrowser = true;
pandora.$ui.manageListsButton[id].options({selected: true});
pandora.$ui.folderList[id].replaceWith(
pandora.$ui.folderBrowser[id] = pandora.ui.folderBrowser(id)
);
}
*/
}
},
click: function(data) {
//var $list = pandora.$ui.folderList[id];
2017-06-09 10:25:38 +00:00
if (data.key == 'items') {
select(section, data.id);
} else if (data.key == 'user') {
2013-02-16 01:20:40 +00:00
pandora.$ui.listDialog = pandora.ui.listDialog('icon').open();
} else if (data.key == 'type') {
if (that.value(data.id, 'type') == 'smart') {
pandora.$ui.listDialog = pandora.ui.listDialog('query').open();
} else {
pandora.$ui.listDialog = pandora.ui.listDialog('general').open();
2013-02-16 01:20:40 +00:00
}
} else if (data.key == 'status') {
var status = that.value(data.id, data.key) == 'private' ? 'public' : 'private';
pandora.changeFolderItemStatus(data.id, status, function(result) {
that.value(result.data.id, 'status', result.data.status);
2011-05-25 19:42:45 +00:00
});
2013-02-16 01:20:40 +00:00
} else if (data.key == 'path') {
2014-01-17 14:19:31 +00:00
// ...
2013-02-16 01:20:40 +00:00
} else if (data.key == 'mounted') {
2014-01-17 14:19:31 +00:00
// ...
2013-02-16 01:20:40 +00:00
}
},
'delete': function(data) {
if (id == 'personal') {
pandora.ui.deleteListDialog(data.ids[0]).open();
} else if (id == 'favorite') {
that.options({selected: []});
pandora.api['unsubscribeFrom' + folderItem]({
id: data.ids[0]
}, function(result) {
2016-10-04 22:00:03 +00:00
Ox.Request.clearCache('find' + folderItems);
2013-02-16 01:20:40 +00:00
that.reloadList();
2011-05-25 19:42:45 +00:00
});
2013-02-16 01:20:40 +00:00
} else if (id == 'featured' && canEditFeatured) {
that.options({selected: []});
pandora.api['edit' + folderItem]({
id: data.ids[0],
status: 'public'
}, function(result) {
2016-10-04 22:00:03 +00:00
Ox.Request.clearCache('find' + folderItems);
2013-02-16 01:20:40 +00:00
// fixme: duplicated
if (result.data.user == pandora.user.username || result.data.subscribed) {
pandora.$ui.folderList[
result.data.user == pandora.user.username ? 'personal' : 'favorite'
].reloadList();
}
that.reloadList();
2011-05-25 19:42:45 +00:00
});
2013-02-16 01:20:40 +00:00
}
},
init: function(data) {
2013-07-15 11:39:23 +00:00
if (pandora.site.sectionFolders[section][i]) {
pandora.site.sectionFolders[section][i].items = data.items;
pandora.$ui.folder[i].$content.css({
height: (data.items || 1) * 16 + 'px'
});
pandora.$ui.folderList[id].css({
height: data.items * 16 + 'px'
})[data.items ? 'show' : 'hide']();
pandora.$ui.folderPlaceholder[id].updateText(id)[data.items ? 'hide' : 'show']();
}
2013-02-16 01:20:40 +00:00
pandora.resizeFolders();
},
key_control_d: function() {
if (that.options('selected').length) {
2020-01-17 23:08:20 +00:00
pandora.addFolderItem(ui.section, ui.section == 'documents' ? ui._collection : ui._list);
}
},
key_control_e: function() {
if (
that.options('selected').length
&& (id == 'personal' || (id == 'featured' && canEditFeatured))
) {
pandora.ui.listDialog().open();
}
},
2013-02-16 01:20:40 +00:00
move: function(data) {
2013-02-19 16:40:26 +00:00
pandora.api['sort' + folderItems]({
2013-02-16 01:20:40 +00:00
section: id,
ids: data.ids
}, function() {
Ox.Request.clearCache('find' + folderItems);
2013-02-16 01:20:40 +00:00
});
},
2013-07-13 21:00:17 +00:00
paste: function() {
pandora.$ui.list.triggerEvent('paste');
2013-02-16 01:20:40 +00:00
},
select: function(data) {
var list = data.ids.length ? data.ids[0] : '';
if (list) {
Ox.forEach(pandora.$ui.folderList, function($list, id_) {
id != id_ && $list.options('selected', []);
});
}
2017-06-09 10:25:38 +00:00
select(section, list);
2013-02-16 01:20:40 +00:00
},
submit: function(data) {
var data_ = {id: data.id};
data_[data.key] = data.value;
pandora.api['edit' + folderItem](data_, function(result) {
if (result.data.id != data.id) {
Ox.Request.clearCache();
2013-02-16 01:20:40 +00:00
pandora.renameList(data.id, result.data.id, result.data.name, id);
pandora.$ui.info.updateListInfo();
}
});
}
});
2017-06-09 10:25:38 +00:00
function select(section, list) {
if (section == 'items') {
pandora.UI.set({
find: {
conditions: list ? [
{key: 'list', value: list, operator: '=='}
] : [],
operator: '&'
},
listView: list
? pandora.user.ui.lists[list]
? pandora.user.ui.lists[list].view
: that.value(list).view
: void 0
});
} else if (section == 'documents') {
pandora.UI.set({
findDocuments: {
conditions: list ? [
{key: 'collection', value: list, operator: '=='}
] : [],
operator: '&'
},
collectionView: list
? pandora.user.ui.collections[list]
? pandora.user.ui.collections[list].view
: that.value(list).view
: void 0
});
} else {
pandora.UI.set(section.slice(0, -1), list);
}
}
2018-11-13 16:31:03 +00:00
function updateItemsCount(ids) {
Ox.serialForEach(ids, function(item, index, items, callback) {
pandora.api['find' + folderItems]({
keys: ['items'],
query: {
conditions: [{key: 'id', value: item, operator: '=='}]
},
range: [0, 1]
}, function(result) {
that.value(item, 'items', result.data.items[0].items);
callback();
})
})
}
2011-05-25 19:42:45 +00:00
return that;
};