From 9eae0a076207d218fff077b61ca97e71e6a8d5b7 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sun, 4 Oct 2015 16:09:51 +0100 Subject: [PATCH] pass index to split/join to keep position --- static/js/editPanel.js | 23 +++++++++++++++++++---- static/js/utils.js | 17 +++++++++++------ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/static/js/editPanel.js b/static/js/editPanel.js index 32b27dc63..79017e3c3 100644 --- a/static/js/editPanel.js +++ b/static/js/editPanel.js @@ -215,8 +215,9 @@ pandora.ui.editPanel = function(isEmbed) { }); }, join: function(data) { - var clips = [serializeClips(data.ids), serializeClips(data.join)]; - pandora.doHistory('join', clips, ui.edit, function(result) { + var clips = [serializeClips(data.ids), serializeClips(data.join)], + index = pandora.$ui.editPanel.getPasteIndex() - 1; + pandora.doHistory('join', clips, ui.edit, index, function(result) { edit.clips = edit.clips.filter(function(clip) { return !Ox.contains(data.ids, clip.id); }).concat(result.data.clips); @@ -314,11 +315,25 @@ pandora.ui.editPanel = function(isEmbed) { sortClips(updateClips); }, split: function(data) { - var clips = [serializeClips(data.ids), serializeClips(data.split)]; - pandora.doHistory('split', clips, ui.edit, function(result) { + var clips = [serializeClips(data.ids), serializeClips(data.split)], + index = pandora.$ui.editPanel.getPasteIndex() - 1; + pandora.doHistory('split', clips, ui.edit, index, function(result) { updateClips(edit.clips.filter(function(clip) { return !Ox.contains(data.ids, clip.id); + }).map(function(clip) { + if (clip.index >= result.data.clips[0].index) { + clip.index += result.data.clips.length + } + return clip; }).concat(result.data.clips)); + //fixme: what resets the position here? + var position = that.options('position'); + that.options({ + selected: result.data.clips.map(function(clip) { return clip.id}), + }); + setTimeout(function() { + that.options('position', position); + }, 500); }); }, subtitles: function(data) { diff --git a/static/js/utils.js b/static/js/utils.js index 73b582176..4387cab40 100644 --- a/static/js/utils.js +++ b/static/js/utils.js @@ -353,9 +353,10 @@ pandora.createLinks = function($element) { (function() { - pandora.doHistory = function(action, items, targets, callback) { + pandora.doHistory = function(action, items, targets, index, callback) { items = Ox.makeArray(items); targets = Ox.makeArray(targets); + callback = Ox.last(arguments); if (action == 'copy' || action == 'paste') { addItems(items, targets[0], addToHistory); } else if (action == 'cut' || action == 'delete') { @@ -364,7 +365,7 @@ pandora.createLinks = function($element) { editItem(items[1], addToHistory); } else if (action == 'join' || action == 'split') { removeItems(items[0], targets[0], function() { - addItems(items[1], targets[0], addToHistory); + addItems(items[1], targets[0], index, addToHistory); }); } else if (action == 'move') { removeItems(items, targets[0], function() { @@ -456,8 +457,14 @@ pandora.createLinks = function($element) { } }; - function addItems(items, target, callback) { + function addItems(items, target, index, callback) { var clips, type = getType(items); + if (Ox.isUndefined(callback)) { + callback = index; + index = pandora.$ui.editPanel + ? pandora.$ui.editPanel.getPasteIndex() + : void 0; + } if (type == 'item') { pandora.api.find({ query: { @@ -485,9 +492,7 @@ pandora.createLinks = function($element) { pandora.api.addClips({ clips: pandora.getClipData(items), edit: target, - index: pandora.$ui.editPanel - ? pandora.$ui.editPanel.getPasteIndex() - : void 0 + index: index }, function(result) { // adding clips creates new ids, so mutate items in history items.splice.apply(items, [0, items.length].concat(