define([ 'jquery', '/api/config', '/common/common-util.js', '/common/common-hash.js', '/common/common-language.js', '/common/common-interface.js', '/common/common-constants.js', '/common/common-feedback.js', '/common/hyperscript.js', '/common/media-tag.js', '/common/clipboard.js', '/customize/messages.js', '/customize/application_config.js', '/customize/pages.js', '/bower_components/nthen/index.js', 'css!/customize/fonts/cptools/style.css' ], function ($, Config, Util, Hash, Language, UI, Constants, Feedback, h, MediaTag, Clipboard, Messages, AppConfig, Pages, NThen) { var UIElements = {}; // Configure MediaTags to use our local viewer if (MediaTag) { MediaTag.setDefaultConfig('pdf', { viewer: '/common/pdfjs/web/viewer.html' }); } UIElements.updateTags = function (common, href) { var existing, tags; NThen(function(waitFor) { common.getSframeChannel().query("Q_GET_ALL_TAGS", null, waitFor(function(err, res) { if (err || res.error) { return void console.error(err || res.error); } existing = Object.keys(res.tags).sort(); })); }).nThen(function (waitFor) { common.getPadAttribute('tags', waitFor(function (err, res) { if (err) { if (err === 'NO_ENTRY') { UI.alert(Messages.tags_noentry); } waitFor.abort(); return void console.error(err); } tags = res || []; }), href); }).nThen(function () { UI.dialog.tagPrompt(tags, existing, function (newTags) { if (!Array.isArray(newTags)) { return; } common.setPadAttribute('tags', newTags, null, href); }); }); }; var importContent = function (type, f, cfg) { return function () { var $files = $('', {type:"file"}); if (cfg && cfg.accept) { $files.attr('accept', cfg.accept); } $files.click(); $files.on('change', function (e) { var file = e.target.files[0]; var reader = new FileReader(); reader.onload = function (e) { f(e.target.result, file); }; reader.readAsText(file, type); }); }; }; var getPropertiesData = function (common, cb) { var data = {}; NThen(function (waitFor) { common.getPadAttribute('password', waitFor(function (err, val) { data.password = val; })); }).nThen(function (waitFor) { var base = common.getMetadataMgr().getPrivateData().origin; // XXX getFileData? // XXX getPadMetadata common.getPadAttribute('href', waitFor(function (err, val) { if (!val) { return; } data.href = base + val; })); common.getPadAttribute('roHref', waitFor(function (err, val) { if (!val) { return; } data.roHref = base + val; })); common.getPadAttribute('channel', waitFor(function (err, val) { data.channel = val; })); common.getPadAttribute('rtChannel', waitFor(function (err, val) { data.rtChannel = val; })); common.getPadAttribute('lastVersion', waitFor(function (err, val) { data.lastVersion = val; })); common.getPadAttribute('atime', waitFor(function (err, val) { data.atime = val; })); common.getPadAttribute('ctime', waitFor(function (err, val) { data.ctime = val; })); common.getPadAttribute('title', waitFor(function (err, val) { data.title = val; })); common.getPadAttribute('tags', waitFor(function (err, val) { data.tags = val; })); common.getSframeChannel().query('Q_GET_PAD_METADATA', null, waitFor(function (err, val) { if (err) { return; } data.owners = val.owners; data.expire = val.expire; data.pending_owners = val.pending_owners; })); /* common.getPadAttribute('owners', waitFor(function (err, val) { data.owners = val; })); common.getPadAttribute('expire', waitFor(function (err, val) { data.expire = val; }));*/ }).nThen(function () { cb(void 0, data); }); }; var createOwnerModal = function (common, data) { var friends = common.getFriends(true); var sframeChan = common.getSframeChannel(); var priv = common.getMetadataMgr().getPrivateData(); var user = common.getMetadataMgr().getUserData(); var edPublic = priv.edPublic; var channel = data.channel; var owners = data.owners; var $div1, $div2; var redrawAll = function () {}; // Remove owner column var drawRemove = function () { var _owners = {}; owners.forEach(function (ed) { var f; Object.keys(friends).some(function (c) { if (friends[c].edPublic === ed) { f = friends[c]; return true; } }); _owners[ed] = f || { displayName: 'Unknown user: '+ ed, // XXX notifications: true, edPublic: ed, }; }); var removeCol = UIElements.getFriendsList('Remove an existing owner instantly', { common: common, friends: _owners, noFilter: true }, function () { console.log(arguments); }); $div1 = $(removeCol.div); var others1 = removeCol.others; $div1.append(h('div.cp-share-grid', others1)); $div1.find('.cp-share-friend').click(function () { var sel = $(this).hasClass('cp-selected'); if (!sel) { $(this).addClass('cp-selected'); } else { var order = $(this).attr('data-order'); order = order ? 'order:'+order : ''; $(this).removeClass('cp-selected').attr('style', order); } }); // When clicking on the remove button, we check the selected users. // If you try to remove yourself, we'll display an additional warning message var removeButton = h('button.no-margin', 'Remove owners'); // XXX $(removeButton).click(function () { // Check selection var $sel = $div1.find('.cp-share-friend.cp-selected'); var sel = $sel.toArray(); var me = false; var toRemove = sel.map(function (el) { var ed = $(el).attr('data-ed'); if (!ed) { return; } if (ed === edPublic) { me = true; } return ed; }).filter(function (x) { return x; }); // Send the command var send = function () { sframeChan.query('Q_SET_PAD_METADATA', { channel: channel, command: 'RM_OWNERS', value: toRemove }, function (err, res) { err = err || (res && res.error); if (err) { return void UI.warn('ERROR' + err); } // XXX owners = res.owners; redrawAll(); UI.log('DONE'); // XXX }); }; var msg = me ? "Are you sure? You're going to give up on your rights, this can't be undone!" : "Are you sure?"; // XXX UI.confirm(msg, function (yes) { if (!yes) { return; } send(); }); }); $div1.append(h('p', removeButton)); return $div1; }; // Add owners column var drawAdd = function () { var _friends = JSON.parse(JSON.stringify(friends)); Object.keys(_friends).forEach(function (curve) { if (owners.indexOf(_friends[curve].edPublic) !== -1) { delete _friends[curve]; } }); var addCol = UIElements.getFriendsList('Ask a friend to be an owner.', { common: common, friends: _friends }, function () { // XXX onSelect... console.log(arguments); }); $div2 = $(addCol.div); var others2 = addCol.others; $div2.append(h('div.cp-share-grid', others2)); $div2.find('.cp-share-friend').click(function () { var sel = $(this).hasClass('cp-selected'); if (!sel) { $(this).addClass('cp-selected'); } else { var order = $(this).attr('data-order'); order = order ? 'order:'+order : ''; $(this).removeClass('cp-selected').attr('style', order); } // XXX onSelect... }); // When clicking on the add button, we get the selected users. var addButton = h('button.no-margin', 'Add owners'); // XXX $(addButton).click(function () { // Check selection var $sel = $div2.find('.cp-share-friend.cp-selected'); var sel = $sel.toArray(); var toAdd = sel.map(function (el) { return friends[$(el).attr('data-curve')].edPublic; }).filter(function (x) { return x; }); NThen(function (waitFor) { var msg = "Are you sure?"; // XXX UI.confirm(msg, waitFor(function (yes) { if (!yes) { waitFor.abort(); return; } })); }).nThen(function (waitFor) { // Send the command sframeChan.query('Q_SET_PAD_METADATA', { channel: channel, command: 'ADD_PENDING_OWNERS', value: toAdd }, waitFor(function (err, res) { err = err || (res && res.error); if (err) { waitFor.abort(); return void UI.warn('ERROR' + err); } // XXX owners = res.owners; })); }).nThen(function (waitFor) { // TODO send notifications sel.forEach(function (el) { var friend = friends[$(el).attr('data-curve')]; if (!friend) { return; } common.mailbox.sendTo("ADD_OWNER", { channel: channel, href: data.href, password: data.password, title: data.title, user: { displayName: user.name, avatar: user.avatar, profile: user.profile, notifications: user.notifications, curvePublic: user.curvePublic, edPublic: priv.edPublic } }, { channel: friend.notifications, curvePublic: friend.curvePublic }, waitFor()); }); }).nThen(function () { redrawAll(); UI.log('DONE'); // XXX }); }); $div2.append(h('p', addButton)); return $div2; }; redrawAll = function () { var $d1 = $div1; var $d2 = $div2; drawRemove().insertBefore($d1); $d1.remove(); drawAdd().insertBefore($d2); $d2.remove(); }; // Create modal var link = h('div.cp-share-columns', [ drawRemove()[0], drawAdd()[0] ]); var linkButtons = [{ className: 'cancel', name: 'CLOSE', // XXX existing key? onClick: function () {}, keys: [27] }]; return UI.dialog.customModal(link, {buttons: linkButtons}); }; var getRightsProperties = function (common, data, cb) { var $d = $('
'); if (!data) { return void cb(void 0, $d); } $('