openmedialibrary/static/js/folders.js

436 lines
16 KiB
JavaScript
Raw Normal View History

2014-05-04 17:26:43 +00:00
'use strict';
oml.ui.folders = function() {
var ui = oml.user.ui,
2014-05-17 11:45:57 +00:00
userIndex,
2014-05-18 23:24:04 +00:00
users,
2014-05-04 17:26:43 +00:00
2014-05-17 11:45:57 +00:00
$lists,
2014-05-04 17:26:43 +00:00
that = Ox.Element()
.css({
2014-05-17 08:17:34 +00:00
overflowX: 'hidden',
2014-05-04 17:26:43 +00:00
})
.bindEvent({
2016-01-08 06:12:43 +00:00
oml_find: function() {
selectList(false);
},
2014-05-19 15:00:33 +00:00
oml_showfolder: function() {
oml.resizeListFolders();
2016-01-10 09:39:21 +00:00
},
2019-02-09 15:25:23 +00:00
oml_showpeers: function() {
that.updateElement();
},
2016-01-10 09:39:21 +00:00
oml_showinfo: function() {
oml.resizeListFolders();
2014-05-19 15:00:33 +00:00
}
2014-05-04 17:26:43 +00:00
});
function getFind(list) {
return {
conditions: list ? [{
key: 'list',
operator: '==',
value: list
}] : [],
operator: '&'
};
}
2014-05-18 23:24:04 +00:00
function getFolderList(list) {
2016-06-06 17:34:43 +00:00
var index = users ? users.map(function(user) {
2014-05-25 12:16:04 +00:00
return user.name;
2016-06-06 17:34:43 +00:00
}).indexOf(list.user) : null;
2014-05-18 23:24:04 +00:00
return list.id == '' ? oml.$ui.librariesList
: Ox.endsWith(list.id, ':') ? oml.$ui.libraryList[index]
2019-01-28 10:09:00 +00:00
: Ox.endsWith(list.id, ':Inbox') ? oml.$ui.inboxList[index]
2014-05-18 23:24:04 +00:00
: oml.$ui.folderList[index];
}
function getUsersAndLists(callback) {
2014-05-25 12:16:04 +00:00
oml.getUsers(function(users_) {
2019-02-09 15:25:23 +00:00
if (ui.showPeers) {
users = users_.filter(function(user) {
return user.id == oml.user.id || user.peered;
});
} else {
users = users_.filter(function(user) {
return user.id == oml.user.id;
});
}
2014-05-18 23:24:04 +00:00
oml.getLists(function(lists) {
callback(users, lists);
});
});
}
2016-01-08 06:12:43 +00:00
function selectList(gainFocus) {
2014-05-04 17:26:43 +00:00
var split = ui._list.split(':'),
2014-05-25 12:16:04 +00:00
index = userIndex[split[0]],
2014-05-04 17:26:43 +00:00
list = split[1],
$selectedList = !ui._list ? oml.$ui.librariesList
2019-01-19 13:09:34 +00:00
: !list ? oml.$ui[
!list ? 'libraryList'
: 'folderList'
][index]
2019-01-28 10:09:00 +00:00
: list == 'Inbox' ? oml.$ui.inboxList[index]
2014-05-04 17:26:43 +00:00
: oml.$ui.folderList[index];
$lists.forEach(function($list) {
if ($list == $selectedList) {
2016-01-08 06:12:43 +00:00
$list.options({selected: [ui._list]});
gainFocus && $list.gainFocus();
2014-05-04 17:26:43 +00:00
} else {
$list.options({selected: []})
}
});
}
2014-05-17 11:45:57 +00:00
that.updateElement = function() {
that.empty();
$lists = [];
oml.$ui.folder = [];
oml.$ui.libraryList = [];
oml.$ui.folderList = [];
2019-01-28 10:09:00 +00:00
oml.$ui.inboxList = [];
2014-05-17 11:45:57 +00:00
2014-05-18 23:24:04 +00:00
getUsersAndLists(function(users, lists) {
2014-05-17 11:45:57 +00:00
userIndex = {};
2014-05-18 23:24:04 +00:00
$lists.push(
oml.$ui.librariesList = oml.ui.folderList({
2014-05-25 12:16:04 +00:00
items: [lists[0]]
2014-05-18 23:24:04 +00:00
})
.bindEvent({
select: function() {
oml.UI.set({find: getFind('')});
oml.$ui.librariesList.options({selected: ['']});
},
selectnext: function() {
oml.UI.set(Ox.extend(
{find: getFind(':')},
2014-05-25 12:16:04 +00:00
'showFolder.',
2014-05-18 23:24:04 +00:00
true
));
},
})
.css({height: '16px'})
.appendTo(that)
);
oml.$ui.librariesList.$body.css({height: '16px'}); // FIXME!
2014-05-17 11:45:57 +00:00
users.forEach(function(user, index) {
var $content,
items = lists.filter(function(list) {
2014-05-25 12:16:04 +00:00
return list.user === user.name
2019-01-28 10:09:00 +00:00
&& list.type != 'library' && list.name != 'Inbox';
2014-05-17 11:45:57 +00:00
}),
2019-01-19 13:09:34 +00:00
libraryId = user.name + ':',
2019-01-28 10:09:00 +00:00
inboxId = user.name + ':Inbox',
offset = 16;
2014-05-17 11:45:57 +00:00
2014-05-25 12:16:04 +00:00
userIndex[user.name] = index;
2014-05-17 11:45:57 +00:00
oml.$ui.folder[index] = Ox.CollapsePanel({
2014-05-25 12:16:04 +00:00
collapsed: !ui.showFolder[user.name],
2014-05-17 11:45:57 +00:00
extras: [
2014-05-18 23:24:04 +00:00
oml.ui.statusIcon(user, index),
2014-05-17 11:45:57 +00:00
{},
Ox.Button({
style: 'symbol',
title: 'info',
2014-05-21 00:02:39 +00:00
tooltip: Ox._(!index ? 'Preferences...' : 'Profile...'),
2014-05-17 11:45:57 +00:00
type: 'image'
})
.bindEvent({
click: function() {
if (!index) {
oml.UI.set({
page: 'preferences',
'part.preferences': 'account'
});
} else {
2016-01-13 11:34:55 +00:00
oml.UI.set({page: 'peers'});
2014-05-17 11:45:57 +00:00
}
}
})
],
2014-05-25 12:16:04 +00:00
title: Ox.encodeHTMLEntities(
!index
? oml.user.preferences.username || 'anonymous'
: user.name
)
2014-05-17 11:45:57 +00:00
})
.css({
width: ui.sidebarSize
})
.bindEvent({
toggle: function(data) {
2016-01-05 15:13:42 +00:00
oml.UI.set('showFolder.' + oml.UI.encode(user.name), !data.collapsed);
2014-05-17 11:45:57 +00:00
}
})
.bindEvent(
2014-05-25 12:16:04 +00:00
'oml_showfolder.' + user.name.toLowerCase(),
2014-05-17 11:45:57 +00:00
function(data) {
oml.$ui.folder[index].options({collapsed: !data.value});
}
)
.appendTo(that);
$content = oml.$ui.folder[index].$content
.css({
2019-01-28 17:08:31 +00:00
// user also has inbox
height: ((index ? 1 : 2) + items.length) * 16 + 'px'
2014-05-17 11:45:57 +00:00
});
$lists.push(
oml.$ui.libraryList[index] = oml.ui.folderList({
2014-05-25 12:16:04 +00:00
items: lists.filter(function(list) {
return list.user == user.name
&& list.type == 'library';
})
2014-05-17 11:45:57 +00:00
})
.bindEvent({
add: function() {
!index && oml.addList();
},
select: function(data) {
oml.UI.set({find: getFind(data.ids[0])});
},
selectnext: function() {
2019-01-28 10:09:00 +00:00
oml.UI.set({find: getFind(inboxId)});
2014-05-17 11:45:57 +00:00
},
selectprevious: function() {
2014-05-18 23:24:04 +00:00
// FIXME: ugly
var set, user, userLists;
if (!index) {
set = {find: getFind('')};
} else {
2014-05-25 12:16:04 +00:00
user = users[index - 1].name;
2014-05-18 23:24:04 +00:00
userLists = lists.filter(function(list) {
return list.user == user;
});
set = {find: getFind(
2014-05-25 12:16:04 +00:00
!userLists.length ? user + ':'
2014-05-18 23:24:04 +00:00
: Ox.last(userLists).id
)};
2016-01-05 15:13:42 +00:00
Ox.extend(set, 'showFolder.' + oml.UI.encode(user), true);
2014-05-17 11:45:57 +00:00
}
oml.UI.set(set);
}
})
.appendTo($content)
);
2019-01-28 10:09:00 +00:00
if (user.name == '') {
$lists.push(
oml.$ui.inboxList[index] = oml.ui.folderList({
items: lists.filter(function(list) {
return list.user == user.name && list.name == 'Inbox';
})
2019-01-19 13:09:34 +00:00
})
2019-01-28 10:09:00 +00:00
.bindEvent({
select: function(data) {
oml.UI.set({find: getFind(data.ids[0])});
},
selectnext: function() {
oml.UI.set({find: getFind(items[0].id)});
},
selectprevious: function() {
oml.UI.set({find: getFind(libraryId)});
}
})
.appendTo($content)
);
oml.$ui.inboxList[index].$body.css({top: offset + 'px'});
offset += 16;
}
2019-01-19 13:09:34 +00:00
2014-05-17 11:45:57 +00:00
$lists.push(
oml.$ui.folderList[index] = oml.ui.folderList({
draggable: !!index,
items: items,
sortable: !index
})
.bindEvent({
2016-01-17 10:13:36 +00:00
add: function(data) {
var keys = data.keys || '',
listData = oml.getListData();
if (listData.user === '' && (
keys != 'shift' || ui.listSelection
)) {
oml.addList(
Ox.contains(keys, 'alt'),
Ox.contains(keys, 'shift')
);
}
2014-05-17 11:45:57 +00:00
},
'delete': function(data) {
2019-01-28 10:09:00 +00:00
!index && !Ox.contains(data.ids, ':Inbox') && oml.ui.deleteListDialog().open();
2014-05-17 11:45:57 +00:00
},
key_control_d: function() {
oml.addList(ui._list);
},
move: function(data) {
lists[user.id] = data.ids.map(function(listId) {
return Ox.getObjectById(items, listId);
});
oml.api.sortLists({
ids: data.ids,
user: user.id
}, function(result) {
// ...
});
},
2019-01-19 12:29:34 +00:00
open: function(data) {
2019-01-28 10:09:00 +00:00
!index && !Ox.contains(data.ids, ':Inbox') && oml.ui.listDialog().open();
2014-05-17 11:45:57 +00:00
},
select: function(data) {
oml.UI.set({find: getFind(data.ids[0])});
},
selectnext: function() {
if (index < users.length - 1) {
oml.UI.set(Ox.extend(
2014-05-25 12:16:04 +00:00
{find: getFind(users[index + 1].name + ':')},
2016-01-05 15:13:42 +00:00
'showFolder.' + oml.UI.encode(users[index + 1].name),
2014-05-17 11:45:57 +00:00
true
));
}
},
selectprevious: function() {
2019-01-28 10:09:00 +00:00
oml.UI.set({find: getFind(inboxId)});
2014-05-17 11:45:57 +00:00
}
})
.css({height: items.length * 16 + 'px'})
.appendTo($content)
);
2019-01-28 10:09:00 +00:00
oml.$ui.folderList[index].$body.css({top: offset + 'px'});
2014-05-17 11:45:57 +00:00
});
2014-05-19 15:00:33 +00:00
oml.resizeListFolders(); // FIXME: DOESN'T WORK
2016-01-08 06:12:43 +00:00
selectList(true);
2014-05-17 11:45:57 +00:00
});
return that;
};
2019-01-31 08:52:20 +00:00
that.updateItems = function(list, items) {
2014-05-18 23:24:04 +00:00
var $list;
if (arguments.length == 0) {
oml.getLists(function(lists) {
lists.forEach(function(list) {
$list = getFolderList(list);
if ($list) {
$list.value(list.id, 'items', list.items);
}
});
2014-05-17 14:42:46 +00:00
});
2014-05-18 23:24:04 +00:00
} else {
2019-01-31 08:52:20 +00:00
$lists.forEach(function($list) {
if (!Ox.isEmpty($list.value(list))) {
$list.value(list, 'items', items);
}
})
2014-05-18 23:24:04 +00:00
}
2014-05-17 14:42:46 +00:00
};
that.updateOwnLists = function(callback) {
2014-05-18 23:24:04 +00:00
oml.getLists(function(lists) {
2014-05-17 14:42:46 +00:00
var items = lists.filter(function(list) {
2019-01-29 10:20:03 +00:00
return list.user == '' && list.type != 'library' && list.name != 'Inbox';
2014-05-17 14:42:46 +00:00
});
2019-01-30 12:46:01 +00:00
// Library + Inbox + lists
2014-05-18 23:24:04 +00:00
oml.$ui.folder[0].$content
2019-01-30 12:46:01 +00:00
.css({height: 16 + 16 + items.length * 16 + 'px'});
2014-05-17 14:42:46 +00:00
oml.$ui.folderList[0].options({
2014-10-31 13:27:45 +00:00
items: Ox.clone(items, true)
2014-05-17 14:42:46 +00:00
})
.css({height: items.length * 16 + 'px'})
.size();
oml.resizeListFolders();
2014-05-17 14:42:46 +00:00
callback && callback();
});
};
2014-05-25 18:06:12 +00:00
that.updateUser = function(index, callback) {
var name = ui._users[index].name;
oml.$ui.folder[index].options({title: Ox.encodeHTMLEntities(name)});
oml.getLists(function(lists) {
var items = lists.filter(function(list) {
2019-01-28 10:09:00 +00:00
return list.user == name && list.type != 'library' && list.name != 'Inbox';
}),
library = lists.filter(function(list) {
return list.user == name && list.type == 'library';
});
2016-04-06 09:21:51 +00:00
if (oml.$ui.libraryList[index]) {
oml.$ui.libraryList[index].options({
items: library
});
2019-01-28 17:08:31 +00:00
// library + inbox + lists
2016-04-06 09:21:51 +00:00
oml.$ui.folder[index].$content
2019-01-28 17:08:31 +00:00
.css({height: (index ? 16 : 32) + items.length * 16 + 'px'});
2016-04-06 09:21:51 +00:00
oml.$ui.folderList[index].options({
items: items
})
.css({height: items.length * 16 + 'px'})
.size();
}
2014-05-25 18:06:12 +00:00
oml.resizeListFolders();
callback && callback();
});
2014-05-25 14:05:26 +00:00
return that;
2014-05-25 12:44:07 +00:00
};
2014-05-18 23:24:04 +00:00
oml.bindEvent({
activity: function(data) {
if (data.activity == 'import') {
that.updateItems();
}
},
2016-01-13 16:16:31 +00:00
addlist: function(data) {
var index = Ox.getIndexById(ui._users, data.user);
if (index > -1) {
that.updateUser(index);
}
},
2014-05-19 15:00:33 +00:00
change: function(data) {
2015-03-07 16:24:07 +00:00
Ox.Request.clearCache();
2014-05-19 15:00:33 +00:00
},
2016-01-13 16:16:31 +00:00
editlist: function(data) {
var index = Ox.getIndexById(ui._users, data.user);
if (index > -1) {
that.updateUser(index);
}
},
orderlists: function(data) {
var index = Ox.getIndexById(ui._users, data.user);
if (index > -1) {
that.updateUser(index);
}
},
2016-01-13 09:52:25 +00:00
peering: function(data, event) {
2014-05-19 15:00:33 +00:00
Ox.Request.clearCache('getUsers');
2016-01-13 09:52:25 +00:00
if (Ox.contains(['peering.accept', 'peering.remove'], event)) {
that.updateElement();
}
2015-12-02 21:05:23 +00:00
},
status: function(data) {
if (data.id == oml.user.id) {
oml.user.online = data.online;
}
},
2014-05-18 23:24:04 +00:00
});
2014-05-17 11:45:57 +00:00
return that.updateElement();
2014-05-04 17:26:43 +00:00
2014-05-18 23:24:04 +00:00
};