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', '/bower_components/croppie/croppie.min.js', 'css!/bower_components/croppie/croppie.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) { var base = common.getMetadataMgr().getPrivateData().origin; common.getPadAttribute('', waitFor(function (err, val) { if (err || !val) { waitFor.abort(); return void cb(err || 'EEMPTY'); } delete val.owners; delete val.expire; Util.extend(data, val); if (data.href) { data.href = base + data.href; } if (data.roHref) { data.roHref = base + data.roHref; } })); common.getPadMetadata(null, waitFor(function (obj) { if (obj && obj.error) { return; } data.owners = obj.owners; data.expire = obj.expire; data.pending_owners = obj.pending_owners; })); }).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 pending_owners = data.pending_owners || []; var redrawAll = function () {}; var div1 = h('div.cp-usergrid-user.cp-share-column.cp-ownership'); var div2 = h('div.cp-usergrid-user.cp-share-column.cp-ownership'); var $div1 = $(div1); var $div2 = $(div2); // Remove owner column var drawRemove = function (pending) { var _owners = {}; var o = (pending ? pending_owners : owners) || []; o.forEach(function (ed) { var f; Object.keys(friends).some(function (c) { if (friends[c].edPublic === ed) { f = friends[c]; return true; } }); if (ed === edPublic) { f = f || user; if (f.name) { f.edPublic = edPublic; } } _owners[ed] = f || { displayName: Messages._getKey('owner_unknownUser', [ed]), edPublic: ed, }; }); var msg = pending ? Messages.owner_removePendingText : Messages.owner_removeText; var removeCol = UIElements.getUserGrid(msg, { common: common, data: _owners, noFilter: true }, function () { }); var $div = $(removeCol.div); // 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 btnMsg = pending ? Messages.owner_removePendingButton : Messages.owner_removeButton; var removeButton = h('button.no-margin', btnMsg); $(removeButton).click(function () { // Check selection var $sel = $div.find('.cp-usergrid-user.cp-selected'); var sel = $sel.toArray(); if (!sel.length) { return; } 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; }); NThen(function (waitFor) { var msg = me ? Messages.owner_removeMeConfirm : Messages.owner_removeConfirm; 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: pending ? 'RM_PENDING_OWNERS' : 'RM_OWNERS', value: toRemove }, waitFor(function (err, res) { err = err || (res && res.error); if (err) { waitFor.abort(); redrawAll(); var text = err === "INSUFFICIENT_PERMISSIONS" ? Messages.fm_forbidden : Messages.error; return void UI.warn(text); } UI.log(Messages.saved); })); }).nThen(function (waitFor) { sel.forEach(function (el) { var friend = friends[$(el).attr('data-curve')]; if (!friend) { return; } common.mailbox.sendTo("RM_OWNER", { channel: channel, title: data.title, pending: pending, 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(); }); }); $div.append(h('p', removeButton)); return $div; }; // 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 || pending_owners.indexOf(_friends[curve].edPublic) !== -1 || !_friends[curve].notifications) { delete _friends[curve]; } }); var addCol = UIElements.getUserGrid(Messages.owner_addText, { common: common, data: _friends }, function () { //console.log(arguments); }); $div2 = $(addCol.div); // When clicking on the add button, we get the selected users. var addButton = h('button.no-margin', Messages.owner_addButton); $(addButton).click(function () { // Check selection var $sel = $div2.find('.cp-usergrid-user.cp-selected'); var sel = $sel.toArray(); if (!sel.length) { return; } var toAdd = sel.map(function (el) { return friends[$(el).attr('data-curve')].edPublic; }).filter(function (x) { return x; }); NThen(function (waitFor) { var msg = Messages.owner_addConfirm; 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(); redrawAll(); var text = err === "INSUFFICIENT_PERMISSIONS" ? Messages.fm_forbidden : Messages.error; return void UI.warn(text); } })); }).nThen(function (waitFor) { 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(Messages.saved); }); }); $div2.append(h('p', addButton)); return $div2; }; redrawAll = function (md) { var todo = function (obj) { if (obj && obj.error) { return; } owners = obj.owners || []; pending_owners = obj.pending_owners || []; $div1.empty(); $div2.empty(); $div1.append(drawRemove(false)).append(drawRemove(true)); $div2.append(drawAdd()); }; if (md) { return void todo(md); } common.getPadMetadata({ channel: data.channel }, todo); }; $div1.append(drawRemove(false)).append(drawRemove(true)); $div2.append(drawAdd()); var handler = sframeChan.on('EV_RT_METADATA', function (md) { if (!$div1.length) { return void handler.stop(); } owners = md.owners || []; pending_owners = md.pending_owners || []; redrawAll(md); }); // Create modal var link = h('div.cp-share-columns', [ div1, div2 /*drawRemove()[0], drawAdd()[0]*/ ]); var linkButtons = [{ className: 'cancel', name: Messages.filePicker_close, onClick: function () {}, keys: [27] }]; return UI.dialog.customModal(link, {buttons: linkButtons}); }; var getRightsProperties = function (common, data, cb) { var $div = $('
'); if (!data) { return void cb(void 0, $div); } var draw = function () { var $d = $('
'); var priv = common.getMetadataMgr().getPrivateData(); var user = common.getMetadataMgr().getUserData(); var edPublic = priv.edPublic; var owned = false; var _owners = {}; if (data.owners && data.owners.length) { if (data.owners.indexOf(edPublic) !== -1) { owned = true; } else { Object.keys(priv.teams || {}).some(function (id) { var team = priv.teams[id] || {}; if (data.owners.indexOf(team.edPublic) === -1) { return; } owned = id; return true; }); } var strangers = 0; data.owners.forEach(function (ed) { // If a friend is an owner, add their name to the list // otherwise, increment the list of strangers // Our edPublic? print "Yourself" if (ed === edPublic) { _owners[ed] = { selected: true, name: user.name, avatar: user.avatar }; return; } // One of our teams? print the team name if (Object.keys(priv.teams || {}).some(function (id) { var team = priv.teams[id] || {}; if (team.edPublic !== ed) { return; } _owners[ed] = { name: team.name, avatar: team.avatar }; return true; })) { return; } // One of our friends? print the friend name if (Object.keys(priv.friends || {}).some(function (c) { var friend = priv.friends[c] || {}; if (friend.edPublic !== ed || c === 'me') { return; } _owners[friend.edPublic] = { name: friend.displayName, avatar: friend.avatar }; return true; })) { return; } // Otherwise it's a stranger strangers++; }); if (strangers) { _owners['stangers'] = { name: Messages._getKey('properties_unknownUser', [strangers]), }; } } var _ownersGrid = UIElements.getUserGrid(Messages.creation_owners, { common: common, noSelect: true, data: _owners, large: true }, function () {}); if (_ownersGrid) { $d.append(_ownersGrid.div); } else { $d.append([ h('label', Messages.creation_owners), ]); $d.append(UI.dialog.selectable(Messages.creation_noOwner, { id: 'cp-app-prop-owners', })); } var parsed; if (data.href || data.roHref) { parsed = Hash.parsePadUrl(data.href || data.roHref); } // XXX Teams owner: transfer ownership if (owned && data.roHref && parsed.type !== 'drive' && parsed.hashData.type === 'pad') { var manageOwners = h('button.no-margin', Messages.owner_openModalButton); $(manageOwners).click(function () { var modal = createOwnerModal(common, data); UI.openCustomModal(modal, { wide: true, }); }); $d.append(h('p', manageOwners)); } if (!data.noExpiration) { var expire = Messages.creation_expireFalse; if (data.expire && typeof (data.expire) === "number") { expire = new Date(data.expire).toLocaleString(); } $('