', {
- 'class': 'cp-splitter'
- }).appendTo($preview);
-
- $preview.on('scroll', function() {
- splitter.css('top', $preview.scrollTop() + 'px');
- });
-
- var $target = $iframe.find('.CodeMirror');
-
- splitter.on('mousedown', function (e) {
- e.preventDefault();
- var x = e.pageX;
- var w = $target.width();
-
- $iframe.on('mouseup mousemove', function handler(evt) {
- if (evt.type === 'mouseup') {
- $iframe.off('mouseup mousemove', handler);
- return;
- }
- $target.css('width', (w - x + evt.pageX) + 'px');
- });
- });
- }
-*/
-
- Cryptpad.removeLoadingScreen();
- setEditable(!readOnly);
- initializing = false;
-
- onLocal(); // push local state to avoid parse errors later.
-
- if (readOnly) {
- config.onRemote();
- return;
- }
- //UserList.getLastName(toolbar.$userNameButton, isNew);
-
- var fmConfig = {
- dropArea: $('.CodeMirror'),
- body: $('body'),
- onUploaded: function (ev, data) {
- //var cursor = editor.getCursor();
- //var cleanName = data.name.replace(/[\[\]]/g, '');
- //var text = '!['+cleanName+']('+data.url+')';
- var parsed = Cryptpad.parsePadUrl(data.url);
- var hexFileName = Cryptpad.base64ToHex(parsed.hashData.channel);
- var src = '/blob/' + hexFileName.slice(0,2) + '/' + hexFileName;
- var mt = '
';
- editor.replaceSelection(mt);
- }
- };
- APP.FM = common.createFileManager(fmConfig);
- };
-
- config.onRemote = function () {
- if (initializing) { return; }
- if (isHistoryMode) { return; }
-
- var oldDoc = canonicalize(CodeMirror.$textarea.val());
- var shjson = APP.realtime.getUserDoc();
-
- // Update the user list (metadata) from the hyperjson
- //Metadata.update(shjson);
-
- var hjson = JSON.parse(shjson);
- var remoteDoc = hjson.content;
-
- if (hjson.metadata) {
- metadataMgr.updateMetadata(hjson.metadata);
- }
-
- var highlightMode = hjson.highlightMode;
- if (highlightMode && highlightMode !== APP.highlightMode) {
- CodeMirror.setMode(highlightMode, onModeChanged);
- }
-
- CodeMirror.setValueAndCursor(oldDoc, remoteDoc, TextPatcher);
- drawPreview();
-
- if (!readOnly) {
- var textValue = canonicalize(CodeMirror.$textarea.val());
- var shjson2 = stringifyInner(textValue);
- if (shjson2 !== shjson) {
- console.error("shjson2 !== shjson");
- TextPatcher.log(shjson, TextPatcher.diff(shjson, shjson2));
- APP.patchText(shjson2);
- }
- }
- if (oldDoc !== remoteDoc) { Cryptpad.notify(); }
- };
-
- config.onAbort = function () {
- // inform of network disconnect
- setEditable(false);
- toolbar.failed();
- Cryptpad.alert(Messages.common_connectionLost, undefined, true);
- };
-
- config.onConnectionChange = function (info) {
- setEditable(info.state);
- //toolbar.failed();
- if (info.state) {
- initializing = true;
- //toolbar.reconnecting(info.myId);
- Cryptpad.findOKButton().click();
- } else {
- Cryptpad.alert(Messages.common_connectionLost, undefined, true);
- }
- };
-
- config.onError = onConnectError;
-
- cpNfInner = common.startRealtime(config);
- metadataMgr = cpNfInner.metadataMgr;
-
- editor.on('change', onLocal);
-
- Cryptpad.onLogout(function () { setEditable(false); });
- };
-/*
- var interval = 100;
- var second = function (CM) {
- Cryptpad.ready(function () {
- andThen(CM);
- Cryptpad.reportAppUsage();
- });
- Cryptpad.onError(function (info) {
- if (info && info.type === "store") {
- onConnectError();
- }
- });
- };
-
- var first = function () {
- if (ifrw.CodeMirror) {
- second(ifrw.CodeMirror);
- } else {
- console.log("CodeMirror was not defined. Trying again in %sms", interval);
- setTimeout(first, interval);
- }
- };
-
- first();*/
- var CMEDITOR_CHECK_INTERVAL = 100;
- var cmEditorAvailable = function (cb) {
- var intr;
- var check = function () {
- if (window.CodeMirror) {
- clearTimeout(intr);
- cb(window.CodeMirror);
- }
- };
- intr = setInterval(function () {
- console.log("CodeMirror was not defined. Trying again in %sms", CMEDITOR_CHECK_INTERVAL);
- check();
- }, CMEDITOR_CHECK_INTERVAL);
- check();
- };
- var main = function () {
- var CM;
- var CodeMirror;
- var editor;
- var common;
-
- nThen(function (waitFor) {
- cmEditorAvailable(waitFor(function (cm) {
- CM = cm;
- }));
- $(waitFor(function () {
- Cryptpad.addLoadingScreen();
- }));
- SFCommon.create(waitFor(function (c) { APP.common = common = c; }));
- }).nThen(function (/*waitFor*/) {
- CodeMirror = Cryptpad.createCodemirror(window, Cryptpad, null, CM);
- $('.CodeMirror').addClass('fullPage');
- editor = CodeMirror.editor;
- Cryptpad.onError(function (info) {
- if (info && info.type === "store") {
- onConnectError();
- }
- });
- andThen(editor, CodeMirror, common);
- });
- };
- main();
-});
diff --git a/www/code2/main.js b/www/code2/main.js
deleted file mode 100644
index 9fe5a935c..000000000
--- a/www/code2/main.js
+++ /dev/null
@@ -1,41 +0,0 @@
-// Load #1, load as little as possible because we are in a race to get the loading screen up.
-define([
- '/bower_components/nthen/index.js',
- '/api/config',
- 'jquery',
- '/common/requireconfig.js',
- '/common/sframe-common-outer.js'
-], function (nThen, ApiConfig, $, RequireConfig, SFCommonO) {
- var requireConfig = RequireConfig();
-
- // Loaded in load #2
- nThen(function (waitFor) {
- $(waitFor());
- }).nThen(function (waitFor) {
- var req = {
- cfg: requireConfig,
- req: [ '/common/loading.js' ],
- pfx: window.location.origin
- };
- window.rc = requireConfig;
- window.apiconf = ApiConfig;
- $('#sbox-iframe').attr('src',
- ApiConfig.httpSafeOrigin + '/code2/inner.html?' + requireConfig.urlArgs +
- '#' + encodeURIComponent(JSON.stringify(req)));
-
- // This is a cheap trick to avoid loading sframe-channel in parallel with the
- // loading screen setup.
- var done = waitFor();
- var onMsg = function (msg) {
- var data = JSON.parse(msg.data);
- if (data.q !== 'READY') { return; }
- window.removeEventListener('message', onMsg);
- var _done = done;
- done = function () { };
- _done();
- };
- window.addEventListener('message', onMsg);
- }).nThen(function (/*waitFor*/) {
- SFCommonO.start();
- });
-});
diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js
index a45425995..ed2e52dea 100644
--- a/www/common/cryptpad-common.js
+++ b/www/common/cryptpad-common.js
@@ -556,6 +556,16 @@ define([
});
common.findOKButton().text(Messages.cancelButton);
};
+ // Secure iframes
+ common.useTemplate = function (href, Crypt, cb) {
+ var parsed = parsePadUrl(href);
+ if(!parsed) { throw new Error("Cannot get template hash"); }
+ Crypt.get(parsed.hash, function (err, val) {
+ if (err) { throw new Error(err); }
+ var p = parsePadUrl(window.location.href);
+ Crypt.put(p.hash, val, cb);
+ });
+ };
// STORAGE
/* fetch and migrate your pad history from the store */
@@ -735,17 +745,19 @@ define([
return list;
};
// Needed for the secure filepicker app
- common.getSecureFilesList = function (cb) {
+ common.getSecureFilesList = function (filter, cb) {
var store = common.getStore();
if (!store) { return void cb("Store is not ready"); }
var proxy = store.getProxy();
var fo = proxy.fo;
var list = {};
var hashes = [];
- fo.getFiles([fo.ROOT]).forEach(function (id) {
+ var types = filter.types;
+ var where = filter.where;
+ fo.getFiles(where).forEach(function (id) {
var data = fo.getFileData(id);
var parsed = parsePadUrl(data.href);
- if ((parsed.type === 'file' || parsed.type === 'media')
+ if ((!types || types.length === 0 || types.indexOf(parsed.type) !== -1)
&& hashes.indexOf(parsed.hash) === -1) {
hashes.push(parsed.hash);
list[id] = data;
diff --git a/www/common/metadata-manager.js b/www/common/metadata-manager.js
index 1c8994d00..5299634fb 100644
--- a/www/common/metadata-manager.js
+++ b/www/common/metadata-manager.js
@@ -73,6 +73,7 @@ define(['json.sortify'], function (Sortify) {
});
};
+ console.log('here register');
sframeChan.on('EV_METADATA_UPDATE', function (ev) {
meta = ev;
if (ev.priv) {
diff --git a/www/common/sframe-common-file.js b/www/common/sframe-common-file.js
index b4bd15c88..f62720ff6 100644
--- a/www/common/sframe-common-file.js
+++ b/www/common/sframe-common-file.js
@@ -46,7 +46,9 @@ define([
$('
').text(Messages.cancel).appendTo($thead);
var createTableContainer = function ($body) {
+ console.log($body);
File.$container = $('', { id: 'cp-fileupload' }).append($table).appendTo($body);
+ console.log('done');
return File.$container;
};
@@ -62,6 +64,25 @@ define([
return data;
};
+ var sframeChan = common.getSframeChannel();
+ var onError = $.noop,
+ onComplete = $.noop,
+ updateProgress = $.noop,
+ onPending = $.noop;
+ sframeChan.on('EV_FILE_UPLOAD_STATE', function (data) {
+ if (data.error) {
+ return void onError(data.error);
+ }
+ if (data.complete && data.href) {
+ return void onComplete(data.href);
+ }
+ if (typeof data.progress !== "undefined") {
+ return void updateProgress(data.progress);
+ }
+ });
+ sframeChan.on('Q_CANCEL_PENDING_FILE_UPLOAD', function (data, cb) {
+ onPending(cb);
+ });
var upload = function (file) {
var blob = file.blob; // This is not a blob but an array buffer
var u8 = new Uint8Array(blob);
@@ -80,16 +101,14 @@ define([
var $pc = $row.find('.cp-fileupload-table-progress');
var $link = $row.find('.cp-fileupload-table-link');
- var sframeChan = common.getSframeChannel();
-
- var updateProgress = function (progressValue) {
+ updateProgress = function (progressValue) {
$pv.text(Math.round(progressValue*100)/100 + '%');
$pb.css({
width: (progressValue/100)*$pc.width()+'px'
});
};
- var onComplete = function (href) {
+ onComplete = function (href) {
$link.attr('href', href)
.click(function (e) {
e.preventDefault();
@@ -108,7 +127,7 @@ define([
queue.next();
};
- var onError = function (e) {
+ onError = function (e) {
queue.inProgress = false;
queue.next();
if (e === 'TOO_LARGE') {
@@ -123,24 +142,10 @@ define([
return void Cryptpad.alert(Messages.upload_serverError);
};
- var onPending = function (cb) {
+ onPending = function (cb) {
Cryptpad.confirm(Messages.upload_uploadPending, cb);
};
- sframeChan.on('EV_FILE_UPLOAD_STATE', function (data) {
- if (data.error) {
- return void onError(data.error);
- }
- if (data.complete && data.href) {
- return void onComplete(data.href);
- }
- if (typeof data.progress !== "undefined") {
- return void updateProgress(data.progress);
- }
- });
- sframeChan.on('Q_CANCEL_PENDING_FILE_UPLOAD', function (data, cb) {
- onPending(cb);
- });
file.noStore = config.noStore;
try {
file.blob = Nacl.util.encodeBase64(u8);
@@ -168,7 +173,8 @@ define([
return;
}
if (queue.inProgress) { return; }
- File.$container.show();
+ // setTimeout to fix a firefox error 'NS_ERROR_NOT_AVAILABLE'
+ window.setTimeout(function () { File.$container.show(); });
var file = queue.queue.shift();
upload(file);
};
@@ -177,7 +183,8 @@ define([
obj.id = id;
queue.queue.push(obj);
- $table.show();
+ // setTimeout to fix a firefox error 'NS_ERROR_NOT_AVAILABLE'
+ window.setTimeout(function () { $table.show(); });
var estimate = FileCrypto.computeEncryptedSize(obj.blob.byteLength, obj.metadata);
var $progressBar = $(' ', {'class':'cp-fileupload-table-progress-container'});
diff --git a/www/common/sframe-common-interface.js b/www/common/sframe-common-interface.js
index 9541b8220..7e35c504d 100644
--- a/www/common/sframe-common-interface.js
+++ b/www/common/sframe-common-interface.js
@@ -261,69 +261,6 @@ define([
return $userAdmin;
};
- // createFileDialog can only be used in filepicker due to access rights restrictions
- UI.createFileDialog = function (cfg) {
- var common = cfg.common;
- var sframeChan = common.getSframeChannel();
- var updateContainer;
- var hideFileDialog = function () {
- sframeChan.event('EV_FILE_PICKER_CLOSE');
- };
- // Create modal
- var $blockContainer = Cryptpad.createModal({
- id: 'cp-filepicker-dialog',
- $body: cfg.$body,
- onClose: hideFileDialog
- }).show();
- // Set the fixed content
- var $block = $blockContainer.find('.cp-modal');
- var $description = $(' ').text(Messages.filePicker_description);
- $block.append($description);
- var $filter = $(' ', {'class': 'cp-modal-form'}).appendTo($block);
- var to;
- $('', {
- type: 'text',
- 'class': 'cp-filepicker-filter',
- 'placeholder': Messages.filePicker_filter
- }).appendTo($filter).on('keypress', function () {
- if (to) { window.clearTimeout(to); }
- to = window.setTimeout(updateContainer, 300);
- });
- $filter.append(common.createButton('upload', false, cfg.data, function () {
- hideFileDialog();
- }));
- var $container = $('', {'class': 'cp-filepicker-content'}).appendTo($block);
- // Update the files list when needed
- updateContainer = function () {
- $container.html('');
- var filter = $filter.find('.cp-filepicker-filter').val().trim();
- var todo = function (err, list) {
- if (err) { return void console.error(err); }
- Object.keys(list).forEach(function (id) {
- var data = list[id];
- var name = data.title || '?';
- if (filter && name.toLowerCase().indexOf(filter.toLowerCase()) === -1) {
- return;
- }
- var $span = $('', {
- 'class': 'cp-filepicker-content-element',
- 'title': name,
- }).appendTo($container);
- $span.append(Cryptpad.getFileIcon(data));
- $span.append(name);
- $span.click(function () {
- if (typeof cfg.onSelect === "function") { cfg.onSelect(data.href); }
- hideFileDialog();
- });
- });
- };
- common.getFilesList(todo);
- };
- updateContainer();
- sframeChan.on('EV_FILE_PICKER_REFRESH', updateContainer);
- };
-
-
UI.initFilePicker = function (common, cfg) {
var onSelect = cfg.onSelect || $.noop;
var sframeChan = common.getSframeChannel();
@@ -331,9 +268,35 @@ define([
onSelect(data);
});
};
- UI.openFilePicker = function (common) {
+ UI.openFilePicker = function (common, types) {
var sframeChan = common.getSframeChannel();
- sframeChan.event("EV_FILE_PICKER_OPEN");
+ sframeChan.event("EV_FILE_PICKER_OPEN", types);
+ };
+
+ UI.openTemplatePicker = function (common) {
+ var metadataMgr = common.getMetadataMgr();
+ var type = metadataMgr.getMetadataLazy().type;
+ var first = true; // We can only pick a template once (for a new document)
+ var fileDialogCfg = {
+ onSelect: function (data) {
+ if (data.type === type && first) {
+ Cryptpad.addLoadingScreen(null, true);
+ var sframeChan = common.getSframeChannel();
+ sframeChan.query('Q_TEMPLATE_USE', data.href, function () {
+ first = false;
+ Cryptpad.removeLoadingScreen();
+ common.feedback('TEMPLATE_USED');
+ });
+ return;
+ }
+ }
+ };
+ common.initFilePicker(common, fileDialogCfg);
+ var pickerCfg = {
+ types: [type],
+ where: ['template']
+ };
+ common.openFilePicker(common, pickerCfg);
};
return UI;
diff --git a/www/common/sframe-common-outer.js b/www/common/sframe-common-outer.js
index e56142ae1..f95855caa 100644
--- a/www/common/sframe-common-outer.js
+++ b/www/common/sframe-common-outer.js
@@ -255,7 +255,7 @@ define([
});
var FP = {};
- var initFilePicker = function () {
+ var initFilePicker = function (types) {
var config = {};
config.onFilePicked = function (data) {
sframeChan.event('EV_FILE_PICKED', data);
@@ -264,16 +264,21 @@ define([
FP.$iframe.hide();
};
config.onFileUpload = onFileUpload;
+ config.types = types;
if (!FP.$iframe) {
FP.$iframe = $(' |