From a901905ae583397b8b1a681283e8450203c90d9f Mon Sep 17 00:00:00 2001 From: yflory Date: Mon, 2 Mar 2020 15:03:32 +0100 Subject: [PATCH] Better move function --- www/common/sframe-app-framework.js | 2 +- www/kanban/inner.js | 33 +++++------ www/kanban/jkanban.js | 92 ++++++++++++++++++++++-------- 3 files changed, 87 insertions(+), 40 deletions(-) diff --git a/www/common/sframe-app-framework.js b/www/common/sframe-app-framework.js index 5dc358432..efd8a8b1a 100644 --- a/www/common/sframe-app-framework.js +++ b/www/common/sframe-app-framework.js @@ -536,7 +536,7 @@ define([ patchTransformer: options.patchTransformer || ChainPad.SmartJSONTransformer, // cryptpad debug logging (default is 1) - logLevel: 1, + logLevel: 2, validateContent: options.validateContent || function (content) { try { JSON.parse(content); diff --git a/www/kanban/inner.js b/www/kanban/inner.js index 6a1448009..0d3e52a35 100644 --- a/www/kanban/inner.js +++ b/www/kanban/inner.js @@ -69,31 +69,31 @@ define([ var initKanban = function (framework, boards) { var items = {}; for (var i=1; i<=6; i++) { - items['id'+i] = { - id: "id"+i, + items[i] = { + id: i, title: Messages._getKey('kanban_item', [i]) }; } var defaultBoards = { - list: ["todo", "working", "done"], + list: [11, 12, 13], data: { - "todo": { - "id": "todo", + "11": { + "id": 11, "title": Messages.kanban_todo, "color": "blue", - "item": ["id1", "id2"] + "item": [1, 2] }, - "working": { - "id": "working", + "12": { + "id": 12, "title": Messages.kanban_working, "color": "orange", - "item": ["id3", "id4"] + "item": [3, 4] }, - "done": { - "id": "done", + "13": { + "id": 13, "title": Messages.kanban_done, "color": "green", - "item": ["id5", "id6"] + "item": [5, 6] } }, items: items @@ -324,7 +324,7 @@ define([ $item.remove(); kanban.inEditMode = false; if (!$input.val()) { return; } - var id = Hash.createChannelId(); + var id = Util.createRandomInteger(); kanban.addElement(boardId, { "id": id, "title": $input.val(), @@ -363,7 +363,7 @@ define([ var boardExists = function (b) { return b.id === "board" + counter; }; while (kanban.options.boards.some(boardExists)) { counter++; } */ - var id = Hash.createChannelId(); + var id = Util.createRandomInteger(); kanban.addBoard({ "id": id, @@ -463,6 +463,7 @@ define([ } }; var restoreCursor = function (data) { + if (!data) { return; } try { var id = data.id; @@ -546,11 +547,11 @@ define([ var data = boards.data || {}; var list = boards.list || []; 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) { 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]; } }); diff --git a/www/kanban/jkanban.js b/www/kanban/jkanban.js index 0121d0edc..dcbf20c13 100644 --- a/www/kanban/jkanban.js +++ b/www/kanban/jkanban.js @@ -120,8 +120,19 @@ el.dropfn(el, target, source, sibling); } + var id = Number($(el).attr('data-id')); 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; if (sibling) { index2 = list.indexOf($(sibling).attr("data-id")); @@ -194,30 +205,27 @@ console.log("In drop"); - var sourceId = $(source).closest('.kanban-board').data('id'); - var board1 = __findBoardJSON(sourceId); - var id1 = $(el).attr('data-eid'); - var pos1 = board1.item.indexOf(id1); - - if (pos1 === -1) { return; } + var id1 = Number($(el).attr('data-eid')); + // Move to trash? if (target.classList.contains('kanban-trash')) { - board1.item.splice(pos1, 1); - delete self.options.boards.items[id1]; + self.moveItem(id1); self.onChange(); return; } - - var targetId = $(target).closest('.kanban-board').data('id'); + // Find the new board + var targetId = Number($(target).closest('.kanban-board').data('id')); + if (!targetId) { return; } var board2 = __findBoardJSON(targetId); 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; } - + // Remove the "move" effect if (el !== null) { self.options.dropEl(el, target, source, sibling); el.classList.remove('is-moving'); @@ -226,22 +234,59 @@ } } - if (board1 === board2 && pos1 < pos2) { - pos2 = pos2 - 1; - } - - // moving element to target array - - var item = board1.item.splice(pos1, 1); - board2.item.splice(pos2, 0, item[0]); + // Move the item + self.moveItem(id1, board2, pos2); // send event that board has changed 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() { var allB = document.querySelectorAll('.kanban-board'); if (allB.length > 0 && allB !== undefined) { @@ -438,8 +483,9 @@ }; this.removeBoard = function (board) { - if (typeof (board) === 'string') + if (typeof (board) === 'string' || typeof (board) === "number") { board = self.element.querySelector('[data-id="' + board + '"]'); + } if (board) { board.remove();