cryptpad/www/todo/main.js

225 lines
6.5 KiB
JavaScript
Raw Normal View History

2017-07-23 12:27:47 +00:00
define([
'jquery',
'/bower_components/chainpad-crypto/crypto.js',
2017-07-23 13:28:49 +00:00
'/bower_components/chainpad-listmap/chainpad-listmap.js',
2017-07-23 12:27:47 +00:00
'/common/toolbar2.js',
'/common/cryptpad-common.js',
2017-07-24 13:23:53 +00:00
'/todo/todo.js',
2017-07-23 12:27:47 +00:00
//'/common/media-tag.js',
//'/bower_components/file-saver/FileSaver.min.js',
2017-07-23 13:08:49 +00:00
'css!/bower_components/components-font-awesome/css/font-awesome.min.css',
2017-07-23 12:27:47 +00:00
'less!/customize/src/less/cryptpad.less',
2017-07-24 13:23:53 +00:00
], function ($, Crypto, Listmap, Toolbar, Cryptpad, Todo) {
2017-07-23 12:27:47 +00:00
var Messages = Cryptpad.Messages;
var APP = window.APP = {};
$(function () {
2017-07-24 12:35:29 +00:00
var $iframe = $('#pad-iframe').contents();
var $body = $iframe.find('body');
var ifrw = $('#pad-iframe')[0].contentWindow;
var $list = $iframe.find('#tasksList');
2017-07-23 12:27:47 +00:00
2017-07-26 11:42:12 +00:00
var removeTips = function () {
$('.tippy-popper').remove();
};
2017-07-24 12:35:29 +00:00
var onReady = function () {
2017-07-24 13:23:53 +00:00
var todo = Todo.init(APP.lm.proxy, Cryptpad);
var deleteTask = function(id) {
todo.remove(id);
var $els = $list.find('.cp-task').filter(function (i, el) {
return $(el).data('id') === id;
});
$els.fadeOut(null, function () {
$els.remove();
2017-07-26 11:42:12 +00:00
removeTips();
});
//APP.display();
2017-07-24 13:23:53 +00:00
};
// TODO make this actually work, and scroll to bottom...
var scrollTo = function (t) {
var $list = $iframe.find('#tasksList');
$list.animate({
scrollTop: t,
});
};
scrollTo = scrollTo;
2017-07-26 08:06:24 +00:00
var makeCheckbox = function (id, cb) {
var entry = APP.lm.proxy.data[id];
var checked = entry.state === 1? 'cp-task-checkbox-checked fa-check-square-o': 'cp-task-checkbox-unchecked fa-square-o';
2017-07-26 08:06:24 +00:00
2017-07-26 11:03:48 +00:00
var title = entry.state === 1?
Messages.todo_markAsIncompleteTitle:
Messages.todo_markAsCompleteTitle;
2017-07-26 11:46:07 +00:00
title = title;
2017-07-26 11:03:48 +00:00
2017-07-26 11:42:12 +00:00
removeTips();
2017-07-26 08:06:24 +00:00
return $('<span>', {
'class': 'cp-task-checkbox fa ' + checked,
2017-07-26 11:46:07 +00:00
//title: title,
2017-07-26 08:06:24 +00:00
}).on('click', function () {
entry.state = (entry.state + 1) % 2;
if (typeof(cb) === 'function') {
cb(entry.state);
}
});
};
var addTaskUI = function (el, animate) {
var $taskDiv = $('<div>', {
'class': 'cp-task'
}).appendTo($list);
$taskDiv.data('id', el);
2017-07-24 13:23:53 +00:00
2017-07-26 11:30:39 +00:00
makeCheckbox(el, function (/*state*/) {
APP.display();
})
.appendTo($taskDiv);
2017-07-24 13:23:53 +00:00
var entry = APP.lm.proxy.data[el];
2017-07-26 07:37:02 +00:00
if (entry.state) {
$taskDiv.addClass('cp-task-complete');
}
2017-07-26 08:43:37 +00:00
$('<span>', { 'class': 'cp-task-text' })
.text(entry.task)
.appendTo($taskDiv);
2017-07-26 10:41:13 +00:00
/*$('<span>', { 'class': 'cp-task-date' })
.text(new Date(entry.ctime).toLocaleString())
2017-07-26 10:41:13 +00:00
.appendTo($taskDiv);*/
$('<button>', {
2017-07-26 11:26:35 +00:00
'class': 'fa fa-times cp-task-remove btn btn-danger',
title: Messages.todo_removeTaskTitle,
}).appendTo($taskDiv).on('click', function() {
deleteTask(el);
});
2017-07-26 08:43:37 +00:00
if (animate) {
$taskDiv.hide();
window.setTimeout(function () {
// ???
$taskDiv.fadeIn();
}, 0);
}
2017-07-26 11:46:07 +00:00
removeTips();
};
var display = APP.display = function () {
$list.empty();
2017-07-26 11:42:12 +00:00
removeTips();
APP.lm.proxy.order.forEach(function (el) {
addTaskUI(el);
2017-07-24 13:23:53 +00:00
});
//scrollTo('300px');
2017-07-24 13:23:53 +00:00
};
2017-07-24 12:35:29 +00:00
var addTask = function () {
2017-07-24 13:23:53 +00:00
var $input = $iframe.find('#newTodoName');
2017-07-26 13:22:25 +00:00
// if the input is empty after removing leading and trailing spaces
// don't create a new entry
if (!$input.val().trim()) { return; }
2017-07-24 13:23:53 +00:00
var obj = {
"state": 0,
"task": $input.val(),
"ctime": +new Date(),
"mtime": +new Date()
};
var id = Cryptpad.createChannelId();
todo.add(id, obj);
2017-07-24 13:23:53 +00:00
2017-07-24 14:14:49 +00:00
$input.val("");
addTaskUI(id, true);
//display();
2017-07-24 12:35:29 +00:00
};
2017-07-24 12:13:47 +00:00
var $formSubmit = $iframe.find('.cp-create-form button').on('click', addTask);
$iframe.find('#newTodoName').on('keypress', function (e) {
switch (e.which) {
case 13:
$formSubmit.click();
break;
default:
console.log(e.which);
}
}).focus();
2017-07-24 13:23:53 +00:00
var editTask = function () {
2017-07-23 12:27:47 +00:00
2017-07-24 12:13:47 +00:00
};
editTask = editTask;
2017-07-24 12:35:29 +00:00
2017-07-24 13:23:53 +00:00
display();
2017-07-24 12:35:29 +00:00
Cryptpad.removeLoadingScreen();
2017-07-24 12:13:47 +00:00
};
var onInit = function () {
2017-07-23 12:27:47 +00:00
Cryptpad.addLoadingScreen();
2017-07-24 12:13:47 +00:00
$body.on('dragover', function (e) { e.preventDefault(); });
$body.on('drop', function (e) { e.preventDefault(); });
2017-07-23 12:27:47 +00:00
var Title;
var $bar = $iframe.find('.toolbar-container');
Title = Cryptpad.createTitle({}, function(){}, Cryptpad);
var configTb = {
2017-07-23 13:08:49 +00:00
displayed: ['useradmin', 'newpad', 'limit', 'upgrade', 'pageTitle'],
2017-07-23 12:27:47 +00:00
ifrw: ifrw,
common: Cryptpad,
//hideDisplayName: true,
$container: $bar,
2017-07-23 13:08:49 +00:00
pageTitle: Messages.todo_title
2017-07-23 12:27:47 +00:00
};
2017-07-23 12:36:41 +00:00
APP.toolbar = Toolbar.create(configTb);
2017-07-23 13:28:49 +00:00
APP.toolbar.$rightside.html(''); // Remove the drawer if we don't use it to hide the toolbar
2017-07-23 12:27:47 +00:00
};
2017-07-23 13:28:49 +00:00
var createTodo = function() {
var obj = Cryptpad.getProxy();
var hash = Cryptpad.createRandomHash();
if(obj.todo) {
hash = obj.todo;
2017-07-24 13:23:53 +00:00
} else {
obj.todo = hash;
2017-07-23 13:28:49 +00:00
}
2017-07-24 12:13:47 +00:00
var secret = Cryptpad.getSecrets('todo', hash);
2017-07-23 13:28:49 +00:00
var listmapConfig = {
data: {},
websocketURL: Cryptpad.getWebsocketURL(),
channel: secret.channel,
validateKey: secret.keys.validateKey || undefined,
crypto: Crypto.createEncryptor(secret.keys),
userName: 'todo',
logLevel: 1,
};
2017-07-24 12:35:29 +00:00
2017-07-24 12:13:47 +00:00
var lm = APP.lm = Listmap.create(listmapConfig);
2017-07-24 12:35:29 +00:00
lm.proxy.on('create', onInit)
.on('ready', onReady);
};
2017-07-23 13:28:49 +00:00
2017-07-23 12:27:47 +00:00
Cryptpad.ready(function () {
2017-07-24 12:13:47 +00:00
createTodo();
Cryptpad.reportAppUsage();
2017-07-23 12:27:47 +00:00
});
});
});