// vim: et:ts=4:sw=4:sts=4:ft=javascript
'use strict';
pandora.addItem = function() {
pandora.api.add(function(result) {
Ox.Request.clearCache('find');
pandora.UI.set({
item: result.data.id,
itemView: 'info'
});
});
};
pandora.addList = function() {
// addList(isSmart, isFrom) or addList(list) [=duplicate]
var $folderList = pandora.$ui.folderList.personal,
isDuplicate = arguments.length == 1,
isSmart, isFrom, list, listData, data;
if (!isDuplicate) {
isSmart = arguments[0];
isFrom = arguments[1];
data = {
name: Ox._('Untitled'),
status: 'private',
type: !isSmart ? 'static' : 'smart'
};
if (isFrom) {
if (!isSmart) {
data.items = pandora.user.ui.listSelection;
} else {
data.query = pandora.user.ui.find;
}
}
addList();
} else {
list = arguments[0];
listData = pandora.getListData();
data = {
name: listData.name,
status: listData.status,
type: listData.type
};
if (data.type == 'smart') {
data.query = listData.query;
}
pandora.api.findLists({
query: {conditions: [{key: 'id', value: list, operator: '=='}]},
keys: ['description']
}, function(result) {
data.description = result.data.items[0].description;
if (data.type == 'static') {
var query = {
conditions: [{key: 'list', value: list, operator: '=='}],
operator: '&'
};
pandora.api.find({
query: query
}, function(result) {
if (result.data.items) {
pandora.api.find({
query: query,
keys: ['id'],
sort: [{key: 'id', operator: ''}],
range: [0, result.data.items]
}, function(result) {
var items = result.data.items.map(function(item) {
return item.id;
});
addList(items);
});
} else {
addList();
}
});
} else {
addList();
}
});
}
function addList(items) {
pandora.api.addList(data, function(result) {
var newList = result.data.id;
if (items) {
pandora.api.addListItems({
list: newList,
items: items
}, function() {
getPosterFrames(newList);
});
} else {
getPosterFrames(newList);
}
});
}
function getPosterFrames(newList) {
var sortKey = Ox.getObjectById(pandora.site.itemKeys, 'votes')
? 'votes'
: 'timesaccessed';
if (!isDuplicate) {
pandora.api.find({
query: {
conditions: [{key: 'list', value: newList, operator: '=='}],
operator: '&'
},
keys: ['id', 'posterFrame'],
sort: [{key: sortKey, operator: ''}],
range: [0, 4]
}, function(result) {
var posterFrames = result.data.items.map(function(item) {
return {item: item.id, position: item.posterFrame};
});
posterFrames = posterFrames.length == 1
? Ox.repeat([posterFrames[0]], 4)
: posterFrames.length == 2
? [posterFrames[0], posterFrames[1], posterFrames[1], posterFrames[0]]
: posterFrames.length == 3
? [posterFrames[0], posterFrames[1], posterFrames[2], posterFrames[0]]
: posterFrames;
setPosterFrames(newList, posterFrames);
})
} else {
pandora.api.findLists({
query: {
conditions: [{key: 'id', value: list, operator: '=='}],
operator: '&'
},
keys: ['posterFrames']
}, function(result) {
setPosterFrames(newList, result.data.items[0].posterFrames);
});
}
}
function setPosterFrames(newList, posterFrames) {
pandora.api.editList({
id: newList,
posterFrames: posterFrames
}, function() {
reloadFolder(newList);
});
}
function reloadFolder(newList) {
pandora.$ui.folder[0].options({collapsed: false});
Ox.Request.clearCache('findLists');
$folderList.bindEventOnce({
load: function(data) {
$folderList.gainFocus()
.options({selected: [newList]})
.editCell(newList, 'name', true);
pandora.UI.set({
find: {
conditions: [{key: 'list', value: newList, operator: '=='}],
operator: '&'
}
});
}
}).reloadList();
}
};
pandora.addText = function(options) {
var $folderList = pandora.$ui.folderList.personal;
options = options || {};
pandora.api.addText(options, function(result) {
reloadFolder(result.data.id);
});
function reloadFolder(newId) {
pandora.$ui.folder[0].options({collapsed: false});
Ox.Request.clearCache('findTexts');
$folderList.bindEventOnce({
load: function(data) {
$folderList.gainFocus()
.options({selected: [newId]})
.editCell(newId, 'name', true);
pandora.UI.set(pandora.user.ui.section.slice(0, -1), newId);
}
}).reloadList();
}
}
pandora.beforeUnloadWindow = function() {
if (pandora.firefogg)
return Ox._("Encoding is currently running\nDo you want to leave this page?");
//prevent error dialogs on unload
pandora.isUnloading = true;
};
pandora.changeFolderItemStatus = function(id, status, callback) {
var ui = pandora.user.ui,
folderItems = ui.section == 'items' ? 'Lists' : Ox.toTitleCase(ui.section),
folderItem = folderItems.slice(0, -1);
if (status == 'private') {
pandora.api['find' + folderItems]({
query: {conditions: [{key: 'id', value: id, operator: '=='}]},
keys: ['name', 'subscribers']
}, function(result) {
var name = result.data.items[0].name,
subscribers = result.data.items[0].subscribers;
if (subscribers) {
pandora.ui.makeListPrivateDialog(name, subscribers, function(makePrivate) {
if (makePrivate) {
changeListStatus();
} else {
callback({data: {
id: id,
status: 'public'
}});
}
}).open();
} else {
changeFolderItemStatus();
}
});
} else {
changeFolderItemStatus();
}
function changeFolderItemStatus() {
pandora.api['edit' + folderItem]({
id: id,
status: status
}, callback);
}
};
pandora.clickLink = function(e) {
var match = e.target.id.match(/^embed(\d+)$/)
if (match) {
pandora.$ui.textPanel.selectEmbed(parseInt(match[1]));
} else if (
e.target.hostname == document.location.hostname
&& !Ox.startsWith(e.target.pathname, '/static')
) {
if (pandora.$ui.home && e.target.pathname != '/home') {
pandora.$ui.home.fadeOutScreen();
}
pandora.URL.push(e.target.pathname);
} else {
window.open('/url=' + encodeURIComponent(e.target.href), '_blank');
}
};
pandora.createLinks = function($element) {
function isExternalLink(target) {
return target.hostname != document.location.hostname
|| Ox.startsWith(target.pathname, '/static');
}
$element
.on({
click: function(e) {
if ($(e.target).is('a') && isExternalLink(e.target)) {
e.preventDefault();
window.open('/url=' + encodeURIComponent(e.target.href), '_blank');
}
return false;
}
})
.bindEvent({
singleclick: function(e) {
if ($(e.target).is('a') && !isExternalLink(e.target)) {
pandora.clickLink(e);
}
}
});
};
pandora.enableDragAndDrop = function($list, canMove) {
var $tooltip = Ox.Tooltip({
animate: false
}),
drag = {},
scrollInterval;
$list.bindEvent({
draganddropstart: function(data) {
drag.action = 'copy';
drag.ids = $list.options('selected'),
drag.item = drag.ids.length == 1
? $list.value(drag.ids[0], 'title')
: drag.ids.length;
drag.source = pandora.getListData(),
drag.targets = {};
Ox.forEach(pandora.$ui.folderList, function($list) {
$list.addClass('OxDroppable').find('.OxItem').each(function() {
var $item = $(this),
id = $item.data('id'),
data = $list.value(id);
drag.targets[id] = Ox.extend({
editable: data.user == pandora.user.username
&& data.type == 'static',
selected: $item.is('.OxSelected')
}, data);
if (!drag.targets[id].selected && drag.targets[id].editable) {
$item.addClass('OxDroppable');
}
});
});
$tooltip.options({title: getTitle()}).show(data.event);
canMove && Ox.UI.$window.on({
keydown: keydown,
keyup: keyup
});
},
draganddrop: function(data) {
var event = data.event;
$tooltip.options({
title: getTitle(event)
}).show(event);
if (scrollInterval && !isAtListsTop(event) && !isAtListsBottom(event)) {
clearInterval(scrollInterval);
scrollInterval = 0;
}
},
draganddroppause: function(data) {
var event = data.event, scroll,
$parent, $grandparent, $panel, title;
// fixme: should be named showLists in the user ui prefs!
if (!pandora.user.ui.showSidebar) {
if (event.clientX < 16 && event.clientY >= 44
&& event.clientY < window.innerHeight - 16
) {
pandora.$ui.mainPanel.toggle(0);
}
} else {
$parent = $(event.target).parent();
$grandparent = $parent.parent();
$panel = $parent.is('.OxCollapsePanel') ? $parent
: $grandparent.is('.OxCollapsePanel') ? $grandparent : null;
if ($panel) {
title = $panel.children('.OxBar').children('.OxTitle')
.html().split(' ')[0].toLowerCase();
if (!pandora.user.ui.showFolder.items[title]) {
Ox.UI.elements[$panel.data('oxid')].options({collapsed: false});
}
}
if (!scrollInterval) {
scroll = isAtListsTop(event) ? -16
: isAtListsBottom(event) ? 16 : 0
if (scroll) {
scrollInterval = setInterval(function() {
pandora.$ui.folders.scrollTop(
pandora.$ui.folders.scrollTop() + scroll
);
}, 100);
}
}
}
},
draganddropenter: function(data) {
var $parent = $(data.event.target).parent(),
$item = $parent.is('.OxItem') ? $parent : $parent.parent(),
$list = $item.parent().parent().parent().parent();
if ($list.is('.OxDroppable')) {
$item.addClass('OxDrop');
drag.target = drag.targets[$item.data('id')];
} else {
drag.target = null;
}
},
draganddropleave: function(data) {
var $parent = $(data.event.target).parent(),
$item = $parent.is('.OxItem') ? $parent : $parent.parent();
if ($item.is('.OxDroppable')) {
$item.removeClass('OxDrop');
drag.target = null;
}
},
draganddropend: function(data) {
Ox.Log('', data, drag, '------------');
canMove && Ox.UI.$window.off({
keydown: keydown,
keyup: keyup
});
if (drag.target && drag.target.editable && !drag.target.selected) {
if (drag.action == 'copy' || (
drag.action == 'move' && drag.source.editable
)) {
if (drag.action == 'move') {
pandora.api.removeListItems({
list: pandora.user.ui._list,
items: data.ids
}, pandora.reloadList);
}
pandora.api.addListItems({
list: drag.target.id,
items: data.ids
}, function() {
Ox.Request.clearCache(); // fixme: remove
pandora.api.find({
query: {
conditions: [{key: 'list', value: drag.target.id, operator: '='}],
operator: ''
}
}, function(result) {
var folder = drag.target.status != 'featured' ? 'personal' : 'featured';
//Ox.Log('', drag.source.status, '//////', drag.target.status)
pandora.$ui.folderList[folder].value(
drag.target.id, 'items',
result.data.items
);
cleanup(250);
});
});
}
} else {
cleanup(0);
}
function cleanup(ms) {
drag = {};
clearInterval(scrollInterval);
scrollInterval = 0;
setTimeout(function() {
$('.OxDroppable').removeClass('OxDroppable');
$('.OxDrop').removeClass('OxDrop');
$tooltip.hide();
}, ms);
}
}
});
function getTitle() {
var image, text;
if (drag.action == 'move' && drag.source.user != pandora.user.username) {
image = 'symbolClose';
text = Ox._('You can only remove {0}
from your own lists.',
[pandora.site.itemName.plural.toLowerCase()]);
} else if (drag.action == 'move' && drag.source.type == 'smart') {
image = 'symbolClose';
text = Ox._('You can\'t remove {0}
from smart lists.',
[pandora.site.itemName.plural.toLowerCase()]);
} else if (drag.target && drag.target.user != pandora.user.username) {
image = 'symbolClose';
text = Ox._('You can only {0} {1}
to your own lists',
[drag.action, pandora.site.itemName.plural.toLowerCase()]);
} else if (drag.target && drag.target.type == 'smart') {
image = 'symbolClose';
text = Ox._('You can\'t {0} {1}
to smart lists',
[drag.action, pandora.site.itemName.plural.toLowerCase()]);
} else {
image = drag.action == 'copy' ? 'symbolAdd' : 'symbolRemove';
text = Ox._(Ox.toTitleCase(drag.action)) + ' ' + (
Ox.isString(drag.item)
? '"' + drag.item + '"'
: drag.item + ' ' + pandora.site.itemName[
drag.item == 1 ? 'singular' : 'plural'
].toLowerCase()
) + '
' + (
drag.target && !drag.target.selected
? Ox._('to the list "{0}"', [Ox.encodeHTMLEntities(drag.target.name)])
: Ox._('to ' + (pandora.user.ui._list ? 'another' : 'a') + ' list')
);
}
return $('