openmedialibrary/static/js/folders.js

353 lines
13 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({
2014-05-19 15:00:33 +00:00
oml_find: selectList,
oml_showfolder: function() {
oml.resizeListFolders();
}
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) {
var index = users.map(function(user) {
2014-05-25 12:16:04 +00:00
return user.name;
2014-05-18 23:24:04 +00:00
}).indexOf(list.user);
return list.id == '' ? oml.$ui.librariesList
: Ox.endsWith(list.id, ':') ? oml.$ui.libraryList[index]
: oml.$ui.folderList[index];
}
function getUsersAndLists(callback) {
2014-05-25 12:16:04 +00:00
oml.getUsers(function(users_) {
users = users_.filter(function(user) {
return user.id == oml.user.id || user.peered;
});
2014-05-18 23:24:04 +00:00
oml.getLists(function(lists) {
callback(users, lists);
});
});
}
2014-05-04 17:26:43 +00:00
function selectList() {
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
: !list ? oml.$ui[!list ? 'libraryList' : 'folderList'][index]
: oml.$ui.folderList[index];
$lists.forEach(function($list) {
if ($list == $selectedList) {
$list.options({selected: [ui._list]}).gainFocus();
} 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 = [];
2014-05-18 23:24:04 +00:00
getUsersAndLists(function(users, lists) {
2014-05-17 11:45:57 +00:00
Ox.print('GOT USERS AND LISTS', users, lists);
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
2014-05-17 11:45:57 +00:00
&& list.type != 'library';
}),
2014-05-25 12:16:04 +00:00
libraryId = user.name + ':';
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 {
oml.UI.set({page: 'users'})
}
}
})
],
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) {
2014-05-25 12:16:04 +00:00
oml.UI.set('showFolder.' + 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({
height: (1 + items.length) * 16 + 'px'
});
$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() {
oml.UI.set({find: getFind(items[0].id)});
},
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
)};
Ox.extend(set, 'showFolder.' + user, true);
2014-05-17 11:45:57 +00:00
}
oml.UI.set(set);
}
})
.appendTo($content)
);
$lists.push(
oml.$ui.folderList[index] = oml.ui.folderList({
draggable: !!index,
items: items,
sortable: !index
})
.bindEvent({
add: function() {
!index && oml.addList();
},
'delete': function() {
!index && oml.ui.deleteListDialog().open();
},
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) {
// ...
});
},
open: function() {
!index && oml.ui.listDialog().open();
},
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 + ':')},
'showFolder.' + users[index + 1].name,
2014-05-17 11:45:57 +00:00
true
));
}
},
selectprevious: function() {
oml.UI.set({find: getFind(libraryId)});
}
})
.css({height: items.length * 16 + 'px'})
.appendTo($content)
);
oml.$ui.folderList[index].$body.css({top: '16px'});
});
2014-05-19 15:00:33 +00:00
oml.resizeListFolders(); // FIXME: DOESN'T WORK
2014-05-17 11:45:57 +00:00
selectList();
});
return that;
};
2014-05-18 23:24:04 +00:00
that.updateItems = function(items) {
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 {
$list = $lists.filter(function($list) {
return $list.options('selected').length;
})[0];
if ($list && !Ox.isEmpty($list.value(ui._list))) {
$list.value(ui._list, 'items', items);
}
}
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) {
2014-05-25 12:16:04 +00:00
return list.user == '' && list.type != 'library';
2014-05-17 14:42:46 +00:00
});
2014-05-18 23:24:04 +00:00
oml.$ui.folder[0].$content
.css({height: 16 + items.length * 16 + 'px'});
2014-05-17 14:42:46 +00:00
oml.$ui.folderList[0].options({
items: items
})
.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) {
return list.user == name && list.type != 'library';
});
oml.$ui.folder[index].$content
.css({height: 16 + items.length * 16 + 'px'});
oml.$ui.folderList[index].options({
items: items
})
.css({height: items.length * 16 + 'px'})
.size();
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();
}
},
2014-05-19 15:00:33 +00:00
change: function(data) {
Ox.print('got change event')
},
'peering.accept': function(data) {
2014-05-19 15:00:33 +00:00
Ox.print('peering.accept reload list')
Ox.Request.clearCache('getUsers');
that.updateElement();
},
'peering.remove': function(data) {
2014-05-19 15:00:33 +00:00
Ox.print('peering.remove reload list')
Ox.Request.clearCache('getUsers');
that.updateElement();
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
};