diff --git a/customize.dist/src/less2/include/toolbar.less b/customize.dist/src/less2/include/toolbar.less index 73e608c4a..542d3f35b 100644 --- a/customize.dist/src/less2/include/toolbar.less +++ b/customize.dist/src/less2/include/toolbar.less @@ -1181,6 +1181,7 @@ &.fa-print { order: 4; } &.fa-arrows-h { order: 5; } &.fa-cog { order: 5; } + &.fa-paint-brush { order: 5; } &.fa-info-circle { order: 6; } &.fa-help { order: 7; } } diff --git a/www/code/inner.js b/www/code/inner.js index 09158d952..7b654a3b1 100644 --- a/www/code/inner.js +++ b/www/code/inner.js @@ -3,8 +3,10 @@ define([ '/common/diffMarked.js', '/bower_components/nthen/index.js', '/common/sframe-common.js', + '/common/hyperscript.js', '/common/sframe-app-framework.js', '/common/sframe-common-codemirror.js', + '/common/common-interface.js', '/common/common-util.js', '/common/common-hash.js', '/code/markers.js', @@ -48,8 +50,10 @@ define([ DiffMd, nThen, SFCommon, + h, Framework, SFCodeMirror, + UI, Util, Hash, Markers, @@ -276,6 +280,54 @@ define([ }; }; + var mkColorByAuthor = function (framework, markers) { + var common = framework._.sfCommon; + var $cbaButton = framework._.sfCommon.createButton(null, true, { + icon: 'fa-paint-brush', + text: Messages.cba_title, + name: 'cba' + }, function () { + var div = h('div'); + var $div = $(div); + var content = h('div', [ + h('h4', Messages.cba_properties), + h('p', Messages.cba_hint), + div + ]); + var setButton = function (state) { + var button = h('button.btn'); + var $button = $(button); + $div.html('').append($button); + if (state) { + // Add "enable" button + $button.addClass('btn-secondary').text(Messages.cba_enable); + UI.confirmButton(button, { + classes: 'btn-primary' + }, function () { + $button.remove(); + markers.setState(true); + common.setAttribute(['code', 'enableColors'], true); + setButton(false); + }); + return; + } + // Add "disable" button + $button.addClass('btn-danger-alt').text(Messages.cba_disable); + UI.confirmButton(button, { + classes: 'btn-danger' + }, function () { + $button.remove(); + markers.setState(false); + common.setAttribute(['code', 'enableColors'], false); + setButton(true); + }); + }; + setButton(!markers.getState()); + UI.alert(content); + }); + framework._.toolbar.$drawer.append($cbaButton); + }; + var mkFilePicker = function (framework, editor, evModeChange) { evModeChange.reg(function (mode) { if (MEDIA_TAG_MODES.indexOf(mode) !== -1) { @@ -397,14 +449,17 @@ define([ //console.log("%s => %s", CodeMirror.highlightMode, CodeMirror.$language.val()); } - if (newPad && Util.find(privateData, ['settings', 'code', 'enableColors'])) { - var metadataMgr = common.getMetadataMgr(); - var md = Util.clone(metadataMgr.getMetadata()); - md.enableColors = true; - metadataMgr.updateMetadata(md); - } - markers.ready(); + common.getPadMetadata(null, function (md) { + if (md && md.error) { return; } + if (!common.isOwned(md.owners)) { return; } + // We're the owner: add the button and enable the colors if needed + mkColorByAuthor(framework, markers); + if (newPad && Util.find(privateData, ['settings', 'code', 'enableColors'])) { + markers.setState(true); + } + }); + var fmConfig = { dropArea: $('.CodeMirror'), diff --git a/www/code/markers.js b/www/code/markers.js index 4b90da721..f417c02fa 100644 --- a/www/code/markers.js +++ b/www/code/markers.js @@ -336,6 +336,7 @@ define([ var editor = Env.editor; var CodeMirror = Env.CodeMirror; + Env.enabled = Boolean(userDoc.authormarks && userDoc.authormarks.marks); setAuthorMarks(Env, userDoc.authormarks); if (!Env.enabled) { return; } @@ -654,12 +655,38 @@ define([ var md = metadataMgr.getMetadata(); Env.ready = true; Env.myAuthorId = getAuthorId(Env); - Env.enabled = md.enableColors; - if (Env.enabled) { - if (Env.$button) { Env.$button.show(); } + if (!Env.enabled) { return; } + if (Env.$button) { Env.$button.show(); } + if (!Env.authormarks.marks || !Env.authormarks.marks.length) { + Env.authormarks = Util.clone(DEFAULT); + } + setMarks(Env); + }; + + var getState = function (Env) { + return Boolean(Env.authormarks && Env.authormarks.marks); + }; + var setState = function (Env, enabled) { + // If the state has changed in the pad, change the Env too + if (!Env.ready) { return; } + if (Env.enabled === enabled) { return; } + Env.enabled = enabled; + if (!Env.enabled) { + // Reset marks + Env.authormarks = {}; setMarks(Env); + if (Env.$button) { Env.$button.hide(); } + } else { + Env.myAuthorId = getAuthorId(Env); + // If it's a reset, add initial marker + if (!Env.authormarks.marks || !Env.authormarks.marks.length) { + Env.authormarks = Util.clone(DEFAULT); + setMarks(Env); + } + if (Env.$button) { Env.$button.show(); } } + if (Env.ready) { Env.framework.localChange(); } }; Markers.create = function (config) { @@ -680,30 +707,10 @@ define([ var metadataMgr = Env.common.getMetadataMgr(); metadataMgr.onChange(function () { - var md = metadataMgr.getMetadata(); - // If the state has changed in the pad, change the Env too - if (Env.enabled !== md.enableColors) { - Env.enabled = md.enableColors; - if (!Env.enabled) { - // Reset marks - Env.authormarks = {}; - setMarks(Env); - if (Env.$button) { Env.$button.hide(); } - } else { - Env.myAuthorId = getAuthorId(Env); - // If it's a reset, add initial marker - if (!Env.authormarks.marks || !Env.authormarks.marks.length) { - Env.authormarks = Util.clone(DEFAULT); - setMarks(Env); - } - if (Env.$button) { Env.$button.show(); } - } - if (Env.ready) { Env.framework.localChange(); } - } - // If the markers are disabled or if I haven't pushed content since the last reset, // don't update my data - if (!Env.enabled || !Env.myAuthorId || !Env.authormarks.authors[Env.myAuthorId]) { + if (!Env.enabled || !Env.myAuthorId || !Env.authormarks.authors || + !Env.authormarks.authors[Env.myAuthorId]) { return; } @@ -735,7 +742,9 @@ define([ setMarks: call(setMarks), localChange: call(localChange), ready: call(ready), - setButton: call(setButton) + setButton: call(setButton), + getState: call(getState), + setState: call(setState), }; }; diff --git a/www/common/common-ui-elements.js b/www/common/common-ui-elements.js index 049e26366..00d51d46e 100644 --- a/www/common/common-ui-elements.js +++ b/www/common/common-ui-elements.js @@ -1622,18 +1622,7 @@ define([ return void UI.alert(Messages.autostore_notAvailable); } var metadataMgr = common.getMetadataMgr(); - sframeChan.query('Q_PROPERTIES_OPEN', { - metadata: metadataMgr.getMetadata() - }, function (err, data) { - if (!data || !data.cmd) { return; } - if (data.cmd === "UPDATE_METADATA") { - if (!data.key) { return; } - var md = Util.clone(metadataMgr.getMetadata()); - md[data.key] = data.value; - if (!data.value) { delete md[data.key]; } - metadataMgr.updateMetadata(md); - } - }); + sframeChan.query('EV_PROPERTIES_OPEN'); }); }); break; diff --git a/www/common/inner/common-modal.js b/www/common/inner/common-modal.js index 33ac14d9f..2131719cb 100644 --- a/www/common/inner/common-modal.js +++ b/www/common/inner/common-modal.js @@ -69,23 +69,7 @@ define([ Modal.isOwned = function (Env, data) { var common = Env.common; data = data || {}; - var priv = common.getMetadataMgr().getPrivateData(); - var edPublic = priv.edPublic; - var owned = false; - if (Array.isArray(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 (team.viewer) { return; } - if (data.owners.indexOf(team.edPublic) === -1) { return; } - owned = Number(id); - return true; - }); - } - } - return owned; + return common.isOwned(data.owners); }; var blocked = false; diff --git a/www/common/inner/properties.js b/www/common/inner/properties.js index c41119a6f..58f10e738 100644 --- a/www/common/inner/properties.js +++ b/www/common/inner/properties.js @@ -50,56 +50,6 @@ define([ // File and history size... var owned = Modal.isOwned(Env, data); - var metadataMgr = common.getMetadataMgr(); - var priv = metadataMgr.getPrivateData(); - if (owned && priv.app === 'code') { - (function () { - var sframeChan = common.getSframeChannel(); - var md = (opts.data && opts.data.metadata) || {}; - var div = h('div'); - var hint = h('div.cp-app-prop-hint', Messages.cba_hint); - var $div = $(div); - var setButton = function (state) { - var button = h('button.btn'); - var $button = $(button); - $div.html('').append($button); - if (state) { - // Add "enable" button - $button.addClass('btn-secondary').text(Messages.cba_enable); - UI.confirmButton(button, { - classes: 'btn-primary' - }, function () { - $button.remove(); - sframeChan.event("EV_SECURE_ACTION", { - cmd: 'UPDATE_METADATA', - key: 'enableColors', - value: true - }); - common.setAttribute(['code', 'enableColors'], true); - setButton(false); - }); - return; - } - // Add "disable" button - $button.addClass('btn-danger-alt').text(Messages.cba_disable); - UI.confirmButton(button, { - classes: 'btn-danger' - }, function () { - $button.remove(); - sframeChan.event("EV_SECURE_ACTION", { - cmd: 'UPDATE_METADATA', - key: 'enableColors', - value: false - }); - common.setAttribute(['code', 'enableColors'], false); - setButton(true); - }); - }; - setButton(!md.enableColors); - $d.append(h('div.cp-app-prop', [Messages.cba_properties, hint, div])); - })(); - } - // check the size of this file, including additional channels var bytes = 0; var historyBytes; diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js index b7bfcbc67..aef3d3656 100644 --- a/www/common/sframe-common-outer.js +++ b/www/common/sframe-common-outer.js @@ -1035,16 +1035,16 @@ define([ initSecureModal('filepicker', data || {}, cb); }); - sframeChan.on('Q_PROPERTIES_OPEN', function (data, cb) { - initSecureModal('properties', data || {}, cb); + sframeChan.on('EV_PROPERTIES_OPEN', function (data, cb) { + initSecureModal('properties', data || {}); }); sframeChan.on('EV_ACCESS_OPEN', function (data) { - initSecureModal('access', data || {}, null); + initSecureModal('access', data || {}); }); sframeChan.on('EV_SHARE_OPEN', function (data) { - initSecureModal('share', data || {}, null); + initSecureModal('share', data || {}); }); sframeChan.on('Q_TEMPLATE_USE', function (data, cb) { diff --git a/www/common/sframe-common.js b/www/common/sframe-common.js index ad8d6f77e..a04a3a2dc 100644 --- a/www/common/sframe-common.js +++ b/www/common/sframe-common.js @@ -331,6 +331,26 @@ define([ }, cb); }; + funcs.isOwned = function (owners) { + var priv = ctx.metadataMgr.getPrivateData(); + var edPublic = priv.edPublic; + var owned = false; + if (Array.isArray(owners) && owners.length) { + if (owners.indexOf(edPublic) !== -1) { + owned = true; + } else { + Object.keys(priv.teams || {}).some(function (id) { + var team = priv.teams[id] || {}; + if (team.viewer) { return; } + if (owners.indexOf(team.edPublic) === -1) { return; } + owned = Number(id); + return true; + }); + } + } + return owned; + }; + funcs.isPadStored = function (cb) { ctx.sframeChan.query("Q_IS_PAD_STORED", null, function (err, obj) { cb (err || (obj && obj.error), obj); diff --git a/www/secureiframe/inner.js b/www/secureiframe/inner.js index 2c39943cc..dae236031 100644 --- a/www/secureiframe/inner.js +++ b/www/secureiframe/inner.js @@ -74,10 +74,9 @@ define([ }; // Properties modal - create['properties'] = function (data) { + create['properties'] = function () { require(['/common/inner/properties.js'], function (Properties) { Properties.getPropertiesModal(common, { - data: data, onClose: function () { hideIframe(); }