support drag and drop from (item view) list browser

This commit is contained in:
rolux 2011-09-04 12:43:59 +00:00
parent 8b9088481d
commit 1eef7e7fd1
3 changed files with 337 additions and 321 deletions

View file

@ -1,5 +1,197 @@
// vim: et:ts=4:sw=4:sts=4:ft=javascript // vim: et:ts=4:sw=4:sts=4:ft=javascript
pandora.enableDragAndDrop = function($list, canMove) {
var drag = {},
$tooltip = Ox.Tooltip({
animate: false
});
$list.bindEvent({
draganddropstart: function(data) {
Ox.print('DRAGSTART', pandora.getListData());
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(data._event)
}).show(data._event);
canMove && Ox.UI.$window.bind({
keydown: keydown,
keyup: keyup
});
},
draganddrop: function(data) {
$tooltip.options({
title: getTitle(data._event)
}).show(data._event);
},
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.print(data, drag, '------------');
canMove && Ox.UI.$window.unbind({
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.print(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 = {};
setTimeout(function() {
$('.OxDroppable').removeClass('OxDroppable');
$('.OxDrop').removeClass('OxDrop');
$tooltip.hide();
}, ms);
}
}
});
function getTitle(e) {
var image, text;
if (drag.action == 'move' && drag.source.user != pandora.user.username) {
image = 'symbolClose'
text = 'You can only remove ' + pandora.site.itemName.plural.toLowerCase()
+ '<br/>from your own lists.';
} else if (drag.action == 'move' && drag.source.type == 'smart') {
image = 'symbolClose';
text = 'You can\'t remove ' + pandora.site.itemName.plural.toLowerCase()
+ '<br/>from smart lists.';
} else if (drag.target && drag.target.user != pandora.user.username) {
image = 'symbolClose'
text = 'You can only ' + drag.action + ' ' + pandora.site.itemName.plural.toLowerCase()
+ '<br/>to your own lists';
} else if (drag.target && drag.target.type == 'smart') {
image = 'symbolClose'
text = 'You can\'t ' + drag.action + ' ' + pandora.site.itemName.plural.toLowerCase()
+ '<br/>to smart lists';
} else {
image = drag.action == 'copy' ? 'symbolAdd' : 'symbolRemove';
text = Ox.toTitleCase(drag.action) + ' ' + (
Ox.isString(drag.item)
? '"' + drag.item + '"'
: drag.item + ' ' + pandora.site.itemName[
drag.item == 1 ? 'singular' : 'plural'
].toLowerCase()
) + '</br> to ' + (
drag.target && !drag.target.selected
? 'the list "' + drag.target.name + '"'
: 'another list'
);
}
return $('<div>')
.append(
$('<div>')
.css({
float: 'left',
width: '16px',
height: '16px',
padding: '2px',
border: '2px solid rgb(192, 192, 192)',
borderRadius: '12px',
margin: '3px 2px 2px 2px'
})
.append(
$('<img>')
.attr({src: Ox.UI.getImageURL(image)})
.css({width: '16px', height: '16px'})
)
)
.append(
$('<div>')
.css({
float: 'left',
margin: '1px 2px 2px 2px',
fontSize: '11px',
whiteSpace: 'nowrap'
})
.html(text)
)
}
function keydown(e) {
if (e.metaKey) {
drag.action = 'move';
$tooltip.options({title: getTitle()}).show();
}
}
function keyup(e) {
if (drag.action == 'move') {
drag.action = 'copy';
$tooltip.options({title: getTitle()}).show();
}
}
};
pandora.enterFullscreen = function() { pandora.enterFullscreen = function() {
pandora.$ui.appPanel.size(0, 0); pandora.$ui.appPanel.size(0, 0);
pandora.user.ui.showSidebar && pandora.$ui.mainPanel.size(0, 0); pandora.user.ui.showSidebar && pandora.$ui.mainPanel.size(0, 0);

View file

@ -41,6 +41,7 @@ pandora.ui.browser = function() {
borderRadius: pandora.user.ui.icons == 'posters' ? 0 : 8, borderRadius: pandora.user.ui.icons == 'posters' ? 0 : 8,
centered: true, centered: true,
defaultRatio: pandora.user.ui.icons == 'posters' ? 5/8 : 1, defaultRatio: pandora.user.ui.icons == 'posters' ? 5/8 : 1,
draggable: true,
id: 'list', id: 'list',
item: function(data, sort, size) { item: function(data, sort, size) {
var icons = pandora.user.ui.icons, var icons = pandora.user.ui.icons,
@ -88,6 +89,7 @@ pandora.ui.browser = function() {
} }
} }
}); });
pandora.enableDragAndDrop(that, false);
} }
that.update = function() { that.update = function() {
pandora.$ui.contentPanel.replaceElement(0, pandora.$ui.browser = pandora.ui.browser()); pandora.$ui.contentPanel.replaceElement(0, pandora.$ui.browser = pandora.ui.browser());

View file

@ -517,337 +517,159 @@ pandora.ui.list = function() { // fixme: remove view argument
}); });
} }
var drag = {}; if (['list', 'icons'].indexOf(view) > -1) {
var $tooltip = Ox.Tooltip({ pandora.enableDragAndDrop(that, true);
animate: false
});
function getTitle(e) { that.bindEvent({
var image, text; closepreview: function(data) {
if (drag.action == 'move' && drag.source.user != pandora.user.username) { pandora.$ui.previewDialog.close();
image = 'symbolClose' preview = false;
text = 'You can only remove ' + pandora.site.itemName.plural.toLowerCase() //delete pandora.$ui.previewDialog;
+ '<br/>from your own lists.'; },
} else if (drag.action == 'move' && drag.source.type == 'smart') { copy: function(data) {
image = 'symbolClose'; Ox.Clipboard.copy({
text = 'You can\'t remove ' + pandora.site.itemName.plural.toLowerCase() items: data.ids,
+ '<br/>from smart lists.'; text: $.map(data.ids, function(id) {
} else if (drag.target && drag.target.user != pandora.user.username) { return pandora.$ui.list.value(id, 'title');
image = 'symbolClose' }).join('\n')
text = 'You can only ' + drag.action + ' ' + pandora.site.itemName.plural.toLowerCase() });
+ '<br/>to your own lists'; },
} else if (drag.target && drag.target.type == 'smart') { 'delete': function(data) {
image = 'symbolClose' pandora.getListData().editable && pandora.api.removeListItems({
text = 'You can\'t ' + drag.action + ' ' + pandora.site.itemName.plural.toLowerCase() list: pandora.user.ui.list,
+ '<br/>to smart lists'; items: data.ids
} else { }, pandora.reloadList);
image = drag.action == 'copy' ? 'symbolAdd' : 'symbolRemove'; },
text = Ox.toTitleCase(drag.action) + ' ' + ( init: function(data) {
Ox.isString(drag.item) pandora.$ui.total.html(pandora.ui.status('total', data));
? '"' + drag.item + '"' data = [];
: drag.item + ' ' + pandora.site.itemName[ $.each(pandora.site.totals, function(i, v) {
drag.item == 1 ? 'singular' : 'plural' data[v.id] = 0;
].toLowerCase() });
) + '</br> to ' + ( pandora.$ui.selected.html(pandora.ui.status('selected', data));
drag.target && !drag.target.selected },
? 'the list "' + drag.target.name + '"' open: function(data) {
: 'another list' var id = data.ids[0],
); title = that.value(id, 'title');
} pandora.URL.set(title, id);
return $('<div>') },
.append( openpreview: function(data) {
$('<div>') pandora.requests.preview && pandora.api.cancel(pandora.requests.preview);
.css({ pandora.requests.preview = pandora.api.find({
float: 'left', keys: ['director', 'id', 'posterRatio', 'title'],
width: '16px', query: {
height: '16px', conditions: $.map(data.ids, function(id, i) {
padding: '2px', return {
border: '2px solid rgb(192, 192, 192)', key: 'id',
borderRadius: '12px', value: id,
margin: '3px 2px 2px 2px' operator: '='
}) }
.append( }),
$('<img>') operator: '|'
.attr({src: Ox.UI.getImageURL(image)}) }
.css({width: '16px', height: '16px'}) }, function(result) {
) var item = result.data.items[0],
) title = item.title + ' (' + item.director + ')'
.append( ratio = item.posterRatio,
$('<div>') windowWidth = window.innerWidth * 0.8,
.css({ windowHeight = window.innerHeight * 0.8,
float: 'left', windowRatio = windowWidth / windowHeight,
margin: '1px 2px 2px 2px', width = Math.round(ratio > windowRatio ? windowWidth : windowHeight * ratio),
fontSize: '11px', height = Math.round(ratio < windowRatio ? windowHeight : windowWidth / ratio);
whiteSpace: 'nowrap' pandora.$ui.previewImage = $('<img>')
}) .attr({src: '/' + item.id + '/poster128.jpg'})
.html(text) .css({width: width + 'px', height: height + 'px'})
) $('<img>').load(function() {
} pandora.$ui.previewImage.attr({src: $(this).attr('src')});
})
function keydown(e) { .attr({src: '/' + item.id + '/poster1024.jpg'});
if (e.metaKey) { if (!preview) {
drag.action = 'move'; if (!pandora.$ui.previewDialog) {
$tooltip.options({title: getTitle()}).show(); pandora.$ui.previewDialog = Ox.Dialog({
} closeButton: true,
} content: pandora.$ui.previewImage,
function keyup(e) { fixedRatio: true,
if (drag.action == 'move') { focus: false,
drag.action = 'copy'; height: height,
$tooltip.options({title: getTitle()}).show(); maximizeButton: true,
} title: title,
} width: width
})
['list', 'icons'].indexOf(view) > -1 && that.bindEvent({ .bindEvent({
closepreview: function(data) { close: function() {
pandora.$ui.previewDialog.close(); that.closePreview();
preview = false; preview = false;
//delete pandora.$ui.previewDialog; },
}, resize: function(event) {
copy: function(data) { pandora.$ui.previewImage.css({
Ox.Clipboard.copy({ width: event.width + 'px',
items: data.ids, height: event.height + 'px'
text: $.map(data.ids, function(id) { });
return pandora.$ui.list.value(id, 'title'); }
}).join('\n') })
}); .open();
}, } else {
'delete': function(data) { pandora.$ui.previewDialog.options({
pandora.getListData().editable && pandora.api.removeListItems({ content: pandora.$ui.previewImage,
list: pandora.user.ui.list, height: height,
items: data.ids title: title,
}, pandora.reloadList); width: width
}, })
draganddropstart: function(data) { .open();
Ox.print('DRAGSTART', pandora.getListData()); }
drag.action = 'copy'; preview = true;
drag.ids = that.options('selected'), } else {
drag.item = drag.ids.length == 1 pandora.$ui.previewDialog.options({
? that.value(drag.ids[0], 'title') content: pandora.$ui.previewImage,
: drag.ids.length; title: title,
drag.source = pandora.getListData(), })
drag.targets = {}; .setSize(width, height);
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);
Ox.print('DATA', data)
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({ paste: function(data) {
title: getTitle(data._event) data.items && pandora.getListData().editable && pandora.api.addListItems({
}).show(data._event); list: pandora.user.ui.list,
Ox.UI.$window.bind({ items: data.items
keydown: keydown, }, pandora.reloadList);
keyup: keyup },
}); select: function(data) {
}, var $still, $timeline;
draganddrop: function(data) { pandora.UI.set(['lists', pandora.user.ui.list, 'selected'].join('|'), data.ids);
$tooltip.options({ //pandora.user.ui.lists[pandora.user.ui.list].selected = data.ids;
title: getTitle(data._event) if (data.ids.length) {
}).show(data._event); pandora.$ui.mainMenu.enableItem('copy');
}, pandora.$ui.mainMenu.enableItem('openmovie');
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.print(data, drag, '------------');
Ox.UI.$window.unbind({
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.print(drag.source.status, '//////', drag.target.status)
pandora.$ui.folderList[folder].value(
drag.target.id, 'items',
result.data.items
);
cleanup(500);
});
});
}
} else {
cleanup(0);
}
function cleanup(ms) {
drag = {};
setTimeout(function() {
$('.OxDroppable').removeClass('OxDroppable');
$('.OxDrop').removeClass('OxDrop');
$tooltip.hide();
}, ms);
}
},
init: function(data) {
pandora.$ui.total.html(pandora.ui.status('total', data));
data = [];
$.each(pandora.site.totals, function(i, v) {
data[v.id] = 0;
});
pandora.$ui.selected.html(pandora.ui.status('selected', data));
},
open: function(data) {
var id = data.ids[0],
title = that.value(id, 'title');
pandora.URL.set(title, id);
},
openpreview: function(data) {
pandora.requests.preview && pandora.api.cancel(pandora.requests.preview);
pandora.requests.preview = pandora.api.find({
keys: ['director', 'id', 'posterRatio', 'title'],
query: {
conditions: $.map(data.ids, function(id, i) {
return {
key: 'id',
value: id,
operator: '='
}
}),
operator: '|'
}
}, function(result) {
var item = result.data.items[0],
title = item.title + ' (' + item.director + ')'
ratio = item.posterRatio,
windowWidth = window.innerWidth * 0.8,
windowHeight = window.innerHeight * 0.8,
windowRatio = windowWidth / windowHeight,
width = Math.round(ratio > windowRatio ? windowWidth : windowHeight * ratio),
height = Math.round(ratio < windowRatio ? windowHeight : windowWidth / ratio);
pandora.$ui.previewImage = $('<img>')
.attr({src: '/' + item.id + '/poster128.jpg'})
.css({width: width + 'px', height: height + 'px'})
$('<img>').load(function() {
pandora.$ui.previewImage.attr({src: $(this).attr('src')});
})
.attr({src: '/' + item.id + '/poster1024.jpg'});
if (!preview) {
if (!pandora.$ui.previewDialog) {
pandora.$ui.previewDialog = Ox.Dialog({
closeButton: true,
content: pandora.$ui.previewImage,
fixedRatio: true,
focus: false,
height: height,
maximizeButton: true,
title: title,
width: width
})
.bindEvent({
close: function() {
that.closePreview();
preview = false;
},
resize: function(event) {
pandora.$ui.previewImage.css({
width: event.width + 'px',
height: event.height + 'px'
});
}
})
.open();
} else {
pandora.$ui.previewDialog.options({
content: pandora.$ui.previewImage,
height: height,
title: title,
width: width
})
.open();
}
preview = true;
} else { } else {
pandora.$ui.previewDialog.options({ pandora.$ui.mainMenu.disableItem('copy');
content: pandora.$ui.previewImage, pandora.$ui.mainMenu.disableItem('openmovie');
title: title,
})
.setSize(width, height);
} }
}); pandora.$ui.leftPanel.replaceElement(2, pandora.$ui.info = pandora.ui.info(data.ids[0]));
}, pandora.api.find({
paste: function(data) { query: {
data.items && pandora.getListData().editable && pandora.api.addListItems({ conditions: $.map(data.ids, function(id, i) {
list: pandora.user.ui.list, return {
items: data.items key: 'id',
}, pandora.reloadList); value: id,
}, operator: '='
select: function(data) { }
var $still, $timeline; }),
pandora.UI.set(['lists', pandora.user.ui.list, 'selected'].join('|'), data.ids); operator: '|'
//pandora.user.ui.lists[pandora.user.ui.list].selected = data.ids; }
if (data.ids.length) { }, function(result) {
pandora.$ui.mainMenu.enableItem('copy'); pandora.$ui.selected.html(pandora.ui.status('selected', result.data));
pandora.$ui.mainMenu.enableItem('openmovie'); });
} else {
pandora.$ui.mainMenu.disableItem('copy');
pandora.$ui.mainMenu.disableItem('openmovie');
} }
pandora.$ui.leftPanel.replaceElement(2, pandora.$ui.info = pandora.ui.info(data.ids[0])); });
pandora.api.find({
query: { }
conditions: $.map(data.ids, function(id, i) {
return {
key: 'id',
value: id,
operator: '='
}
}),
operator: '|'
}
}, function(result) {
pandora.$ui.selected.html(pandora.ui.status('selected', result.data));
});
}
});
that.display = function() { // fixme: used? that.display = function() { // fixme: used?
pandora.$ui.rightPanel.replaceElement(1, pandora.$ui.contentPanel = pandora.ui.contentPanel()); pandora.$ui.rightPanel.replaceElement(1, pandora.$ui.contentPanel = pandora.ui.contentPanel());
}; };
return that; return that;
}; };