Better move function

pull/1/head
yflory 5 years ago
parent 1b2bb5b693
commit a901905ae5

@ -536,7 +536,7 @@ define([
patchTransformer: options.patchTransformer || ChainPad.SmartJSONTransformer, patchTransformer: options.patchTransformer || ChainPad.SmartJSONTransformer,
// cryptpad debug logging (default is 1) // cryptpad debug logging (default is 1)
logLevel: 1, logLevel: 2,
validateContent: options.validateContent || function (content) { validateContent: options.validateContent || function (content) {
try { try {
JSON.parse(content); JSON.parse(content);

@ -69,31 +69,31 @@ define([
var initKanban = function (framework, boards) { var initKanban = function (framework, boards) {
var items = {}; var items = {};
for (var i=1; i<=6; i++) { for (var i=1; i<=6; i++) {
items['id'+i] = { items[i] = {
id: "id"+i, id: i,
title: Messages._getKey('kanban_item', [i]) title: Messages._getKey('kanban_item', [i])
}; };
} }
var defaultBoards = { var defaultBoards = {
list: ["todo", "working", "done"], list: [11, 12, 13],
data: { data: {
"todo": { "11": {
"id": "todo", "id": 11,
"title": Messages.kanban_todo, "title": Messages.kanban_todo,
"color": "blue", "color": "blue",
"item": ["id1", "id2"] "item": [1, 2]
}, },
"working": { "12": {
"id": "working", "id": 12,
"title": Messages.kanban_working, "title": Messages.kanban_working,
"color": "orange", "color": "orange",
"item": ["id3", "id4"] "item": [3, 4]
}, },
"done": { "13": {
"id": "done", "id": 13,
"title": Messages.kanban_done, "title": Messages.kanban_done,
"color": "green", "color": "green",
"item": ["id5", "id6"] "item": [5, 6]
} }
}, },
items: items items: items
@ -324,7 +324,7 @@ define([
$item.remove(); $item.remove();
kanban.inEditMode = false; kanban.inEditMode = false;
if (!$input.val()) { return; } if (!$input.val()) { return; }
var id = Hash.createChannelId(); var id = Util.createRandomInteger();
kanban.addElement(boardId, { kanban.addElement(boardId, {
"id": id, "id": id,
"title": $input.val(), "title": $input.val(),
@ -363,7 +363,7 @@ define([
var boardExists = function (b) { return b.id === "board" + counter; }; var boardExists = function (b) { return b.id === "board" + counter; };
while (kanban.options.boards.some(boardExists)) { counter++; } while (kanban.options.boards.some(boardExists)) { counter++; }
*/ */
var id = Hash.createChannelId(); var id = Util.createRandomInteger();
kanban.addBoard({ kanban.addBoard({
"id": id, "id": id,
@ -463,6 +463,7 @@ define([
} }
}; };
var restoreCursor = function (data) { var restoreCursor = function (data) {
if (!data) { return; }
try { try {
var id = data.id; var id = data.id;
@ -546,11 +547,11 @@ define([
var data = boards.data || {}; var data = boards.data || {};
var list = boards.list || []; var list = boards.list || [];
Object.keys(data).forEach(function (id) { Object.keys(data).forEach(function (id) {
if (list.indexOf(id) === -1) { delete data[id]; } if (list.indexOf(Number(id)) === -1) { delete data[id]; }
}); });
Object.keys(items).forEach(function (eid) { Object.keys(items).forEach(function (eid) {
var exists = Object.keys(data).some(function (id) { var exists = Object.keys(data).some(function (id) {
return (data[id].item || []).indexOf(eid) !== -1; return (data[id].item || []).indexOf(Number(eid)) !== -1;
}); });
if (!exists) { delete items[eid]; } if (!exists) { delete items[eid]; }
}); });

@ -120,8 +120,19 @@
el.dropfn(el, target, source, sibling); el.dropfn(el, target, source, sibling);
} }
var id = Number($(el).attr('data-id'));
var list = self.options.boards.list || []; var list = self.options.boards.list || [];
var index1 = list.indexOf($(el).attr("data-id"));
// Move to trash?
if (target.classList.contains('kanban-trash')) {
var index1 = list.indexOf(id);
list.splice(index1, 1);
delete self.options.boards.data[id];
self.onChange();
return;
}
var index1 = list.indexOf(id);
var index2; var index2;
if (sibling) { if (sibling) {
index2 = list.indexOf($(sibling).attr("data-id")); index2 = list.indexOf($(sibling).attr("data-id"));
@ -194,30 +205,27 @@
console.log("In drop"); console.log("In drop");
var sourceId = $(source).closest('.kanban-board').data('id'); var id1 = Number($(el).attr('data-eid'));
var board1 = __findBoardJSON(sourceId);
var id1 = $(el).attr('data-eid');
var pos1 = board1.item.indexOf(id1);
if (pos1 === -1) { return; }
// Move to trash?
if (target.classList.contains('kanban-trash')) { if (target.classList.contains('kanban-trash')) {
board1.item.splice(pos1, 1); self.moveItem(id1);
delete self.options.boards.items[id1];
self.onChange(); self.onChange();
return; return;
} }
// Find the new board
var targetId = $(target).closest('.kanban-board').data('id'); var targetId = Number($(target).closest('.kanban-board').data('id'));
if (!targetId) { return; }
var board2 = __findBoardJSON(targetId); var board2 = __findBoardJSON(targetId);
var id2 = $(sibling).attr('data-eid'); var id2 = $(sibling).attr('data-eid');
var pos2 = id2 ? board2.item.indexOf(id2) : (board2.item.length) if (id2) { id2 = Number(id2); }
var pos2 = id2 ? board2.item.indexOf(id2) : board2.item.length;
if (pos2 === -1) { pos2 = board2.item.length; } if (pos2 === -1) { pos2 = board2.item.length; }
// Remove the "move" effect
if (el !== null) { if (el !== null) {
self.options.dropEl(el, target, source, sibling); self.options.dropEl(el, target, source, sibling);
el.classList.remove('is-moving'); el.classList.remove('is-moving');
@ -226,22 +234,59 @@
} }
} }
if (board1 === board2 && pos1 < pos2) { // Move the item
pos2 = pos2 - 1; self.moveItem(id1, board2, pos2);
}
// moving element to target array
var item = board1.item.splice(pos1, 1);
board2.item.splice(pos2, 0, item[0]);
// send event that board has changed // send event that board has changed
self.onChange(); self.onChange();
}) })
} }
}; };
var findItem = function (eid) {
var boards = self.options.boards;
var list = boards.list || [];
var res = [];
list.forEach(function (id) {
var b = boards.data[id];
if (!b) { return; }
var items = b.item || [];
var idx = items.indexOf(eid);
if (idx === -1) { return; }
// This board contains our item...
res.push({
board: b,
pos: idx
});
});
return res;
};
this.moveItem = function (eid, board, pos) {
var boards = self.options.boards;
var list = boards.list || [];
var from = [];
var same = -1;
var from = findItem(eid);
// Remove the item from its board
from.forEach(function (obj) {
console.warn(obj.board.item[obj.pos]);
obj.board.item.splice(obj.pos, 1);
if (obj.board === board) { same = obj.pos; }
});
// If it's a deletion, remove the item data
if (!board) {
delete boards.items[eid];
return;
}
// If it's moved to the same board at a bigger index, decrement the index by one
// (we just removed one element)
if (same !== -1 && same < pos) {
pos = pos - 1;
}
board.item.splice(pos, 0, eid);
console.error(JSON.stringify(boards, 0, 2));
};
this.enableAllBoards = function() { this.enableAllBoards = function() {
var allB = document.querySelectorAll('.kanban-board'); var allB = document.querySelectorAll('.kanban-board');
if (allB.length > 0 && allB !== undefined) { if (allB.length > 0 && allB !== undefined) {
@ -438,8 +483,9 @@
}; };
this.removeBoard = function (board) { this.removeBoard = function (board) {
if (typeof (board) === 'string') if (typeof (board) === 'string' || typeof (board) === "number") {
board = self.element.querySelector('[data-id="' + board + '"]'); board = self.element.querySelector('[data-id="' + board + '"]');
}
if (board) { if (board) {
board.remove(); board.remove();

Loading…
Cancel
Save