You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cryptpad/www/kanban/inner.js

329 lines
11 KiB
JavaScript

define([
'jquery',
'/bower_components/nthen/index.js',
'/common/sframe-common.js',
'/common/sframe-app-framework.js',
'/common/common-util.js',
'/common/common-hash.js',
7 years ago
'/common/common-interface.js',
'/common/modes.js',
'/customize/messages.js',
'/kanban/jkanban.js',
'css!/kanban/jkanban.css',
], function (
$,
nThen,
SFCommon,
Framework,
Util,
Hash,
7 years ago
UI,
Modes,
7 years ago
Messages)
{
var verbose = function (x) { console.log(x); };
verbose = function () {}; // comment out to enable verbose logging
// Kanban code
var initKanban = function (framework, boards) {
var defaultBoards = [{
7 years ago
"id": "todo",
"title": Messages.kanban_todo,
7 years ago
"color": "blue",
"item": [{
"title": Messages._getKey('kanban_item', [1])
}, {
"title": Messages._getKey('kanban_item', [2])
}]
7 years ago
}, {
"id": "working",
"title": Messages.kanban_working,
7 years ago
"color": "orange",
"item": [{
"title": Messages._getKey('kanban_item', [3])
7 years ago
}, {
"title": Messages._getKey('kanban_item', [4])
7 years ago
}]
}, {
"id": "done",
"title": Messages.kanban_done,
7 years ago
"color": "green",
"item": [{
"title": Messages._getKey('kanban_item', [5])
7 years ago
}, {
"title": Messages._getKey('kanban_item', [6])
7 years ago
}]
}];
7 years ago
if (!boards) {
verbose("Initializing with default boards content");
boards = defaultBoards;
} else {
verbose("Initializing with boards content " + boards);
}
// Remove any existing elements
$(".kanban-container-outer").remove();
var getInput = function () {
return $('<input>', {
'type': 'text',
'id': 'kanban-edit',
'size': '30'
});
};
7 years ago
var kanban = new window.jKanban({
element: '#cp-app-kanban-content',
gutter: '15px',
widthBoard: '300px',
onChange: function () {
verbose("Board object has changed");
framework.localChange();
},
click: function (el) {
if (kanban.inEditMode) {
verbose("An edit is already active");
return;
}
kanban.inEditMode = true;
var name = $(el).text();
$(el).html('');
var $input = getInput().val(name).appendTo(el).focus();
$input[0].select();
var save = function () {
// Store the value
var name = $input.val();
// Remove the input
$(el).text(name);
// Save the value for the correct board
var board = $(el.parentNode.parentNode).attr("data-id");
var pos = kanban.findElementPosition(el);
kanban.getBoardJSON(board).item[pos].title = name;
kanban.onChange();
// Unlock edit mode
kanban.inEditMode = false;
};
$input.blur(save);
$input.keydown(function (e) {
if (e.which === 13) {
e.preventDefault();
e.stopPropagation();
save();
return;
}
if (e.which === 27) {
e.preventDefault();
e.stopPropagation();
$(el).text(name);
kanban.inEditMode = false;
return;
}
});
},
boardTitleClick: function (el, e) {
e.stopPropagation();
if (kanban.inEditMode) {
verbose("An edit is already active");
return;
}
kanban.inEditMode = true;
var name = $(el).text();
$(el).html('');
var $input = getInput().val(name).appendTo(el).focus();
$input[0].select();
var save = function () {
// Store the value
var name = $input.val();
// Remove the input
$(el).text(name);
// Save the value for the correct board
var board = $(el.parentNode.parentNode).attr("data-id");
kanban.getBoardJSON(board).title = name;
kanban.onChange();
// Unlock edit mode
kanban.inEditMode = false;
};
$input.blur(save);
$input.keydown(function (e) {
if (e.which === 13) {
e.preventDefault();
e.stopPropagation();
save();
return;
}
if (e.which === 27) {
e.preventDefault();
e.stopPropagation();
$(el).text(name);
kanban.inEditMode = false;
return;
}
});
},
7 years ago
colorClick: function (el) {
verbose("in color click");
var board = $(el.parentNode).attr("data-id");
var boardJSON = kanban.getBoardJSON(board);
var currentColor = boardJSON.color;
verbose("Current color " + currentColor);
var index = kanban.options.colors.findIndex(function (element) {
7 years ago
return (element === currentColor);
}) + 1;
verbose("Next index " + index);
7 years ago
if (index >= kanban.options.colors.length) { index = 0; }
var nextColor = kanban.options.colors[index];
verbose("Next color " + nextColor);
boardJSON.color = nextColor;
$(el).removeClass("kanban-header-" + currentColor);
$(el).addClass("kanban-header-" + nextColor);
kanban.onChange();
},
removeClick: function (el) {
UI.confirm(Messages.kanban_deleteBoard, function (yes) {
7 years ago
if (!yes) { return; }
verbose("Delete board");
var boardName = $(el.parentNode.parentNode).attr("data-id");
7 years ago
for (var index in kanban.options.boards) {
if (kanban.options.boards[index].id === boardName) {
break;
}
index++;
}
kanban.options.boards.splice(index, 1);
kanban.removeBoard(boardName);
kanban.onChange();
7 years ago
});
},
buttonClick: function (el, boardId, e) {
e.stopPropagation();
if (kanban.inEditMode) {
verbose("An edit is already active");
return;
}
kanban.inEditMode = true;
// create a form to enter element
var $item = $('<div>', {'class': 'kanban-item'});
var $input = getInput().val(name).appendTo($item);
kanban.addForm(boardId, $item[0]);
$input.focus();
var save = function () {
$item.remove();
kanban.addElement(boardId, {
"title": $input.val(),
7 years ago
});
kanban.inEditMode = false;
7 years ago
};
$input.blur(save);
$input.keydown(function (e) {
if (e.which === 13) {
e.preventDefault();
e.stopPropagation();
save();
return;
}
if (e.which === 27) {
e.preventDefault();
e.stopPropagation();
$(el).text(name);
kanban.inEditMode = false;
return;
}
});
},
addItemButton: true,
boards: boards,
dragcancelEl: function (el, boardId) {
var pos = kanban.findElementPosition(el);
UI.confirm(Messages.kanban_deleteItem, function (yes) {
if (!yes) { return; }
var board;
kanban.options.boards.some(function (b) {
if (b.id === boardId) {
return (board = b);
}
});
if (!board) { return; }
board.item.splice(pos, 1);
$(el).remove();
kanban.onChange();
});
}
});
var addBoardDefault = document.getElementById('kanban-addboard');
addBoardDefault.addEventListener('click', function () {
var counter = 1;
7 years ago
// Get the new board id
while (kanban.options.boards.indexOf("board" + counter) !== -1) {
counter++;
}
7 years ago
kanban.addBoards([{
"id": "board" + counter,
"title": Messages.kanban_newBoard,
7 years ago
"color": "yellow",
"item": [{
"title": Messages._getKey('kanban_item', [1]),
7 years ago
}]
}]);
kanban.onChange();
});
return kanban;
};
// Start of the main loop
var andThen2 = function (framework) {
7 years ago
var kanban = initKanban(framework);
framework.onContentUpdate(function (newContent) {
// Need to update the content
verbose("Content should be updated to " + newContent);
var currentContent = kanban.getBoardsJSON();
var remoteContent = newContent.content;
7 years ago
if (currentContent !== remoteContent) {
// reinit kanban (TODO: optimize to diff only)
verbose("Content is different.. Applying content");
kanban.setBoards(remoteContent);
}
});
framework.setContentGetter(function () {
// var content = $("#cp-app-kanban-content").val();
var content = kanban.getBoardsJSON();
verbose("Content current value is " + content);
return {
content: content
};
});
7 years ago
framework.onReady(function () {
$("#cp-app-kanban-content").focus();
});
framework.start();
};
var main = function () {
// var framework;
nThen(function (waitFor) {
// Framework initialization
Framework.create({
toolbarContainer: '#cme_toolbox',
contentContainer: '#cp-app-kanban-editor',
7 years ago
}, waitFor(function (framework) {
andThen2(framework);
}));
});
};
main();
});