define([ 'jquery', '/bower_components/chainpad-crypto/crypto.js', '/bower_components/chainpad-netflux/chainpad-netflux.js', '/bower_components/textpatcher/TextPatcher.js', '/common/toolbar2.js', 'json.sortify', '/bower_components/chainpad-json-validator/json-ot.js', '/common/cryptpad-common.js', '/common/cryptget.js', '/slide/slide.js', '/bower_components/tweetnacl/nacl-fast.min.js', // needed for media-tag ], function ($, Crypto, Realtime, TextPatcher, Toolbar, JSONSortify, JsonOT, Cryptpad, Cryptget, Slide) { var Messages = Cryptpad.Messages; var module = window.APP = { Cryptpad: Cryptpad, TextPatcher: TextPatcher, Slide: Slide, }; var APP = window.APP; var SLIDE_BACKCOLOR_ID = "cryptpad-backcolor"; var SLIDE_COLOR_ID = "cryptpad-color"; $(function () { Cryptpad.addLoadingScreen(); var stringify = function (obj) { return JSONSortify(obj); }; var ifrw = module.ifrw = $('#pad-iframe')[0].contentWindow; var toolbar; var editor; var secret = Cryptpad.getSecrets(); var readOnly = secret.keys && !secret.keys.editKeyStr; Slide.readOnly = readOnly; if (!secret.keys) { secret.keys = secret.key; } var presentMode = Slide.isPresentURL(); var onConnectError = function () { Cryptpad.errorLoadingScreen(Messages.websocketError); }; var andThen = function (CMeditor) { var $iframe = $('#pad-iframe').contents(); var CodeMirror = Cryptpad.createCodemirror(CMeditor, ifrw, Cryptpad); editor = CodeMirror.editor; var $bar = $('#pad-iframe')[0].contentWindow.$('#cme_toolbox'); var $pad = $('#pad-iframe'); var isHistoryMode = false; var setEditable = module.setEditable = function (bool) { if (readOnly && bool) { return; } editor.setOption('readOnly', !bool); }; var Title; var UserList; var Metadata; var setTabTitle = function (title) { var slideNumber = ''; if (Slide.shown) { //Slide.index && Slide.content.length) { slideNumber = ' (' + Slide.index + '/' + Slide.content.length + ')'; } document.title = title + slideNumber; }; var initialState = Messages.slideInitialState; var $modal = $pad.contents().find('#modal'); var $content = $pad.contents().find('#content'); var $print = $pad.contents().find('#print'); var slideOptions = {}; $content.click(function (e) { if (!e.target) { return; } var $t = $(e.target); if ($t.is('a') || $t.parents('a').length) { e.preventDefault(); var $a = $t.is('a') ? $t : $t.parents('a').first(); var href = $a.attr('href'); window.open(href); } }); Slide.setModal(APP, $modal, $content, $pad, ifrw, slideOptions, initialState); var enterPresentationMode = function (shouldLog) { Slide.show(true, editor.getValue()); if (shouldLog) { Cryptpad.log(Messages.presentSuccess); } }; if (presentMode) { enterPresentationMode(true); } var textColor; var backColor; var config = { initialState: '{}', websocketURL: Cryptpad.getWebsocketURL(), channel: secret.channel, // our public key validateKey: secret.keys.validateKey || undefined, readOnly: readOnly, crypto: Crypto.createEncryptor(secret.keys), transformFunction: JsonOT.validate, network: Cryptpad.getNetwork() }; var canonicalize = function (t) { return t.replace(/\r\n/g, '\n'); }; var setHistory = function (bool, update) { isHistoryMode = bool; setEditable(!bool); if (!bool && update) { config.onRemote(); } }; var initializing = true; var stringifyInner = function (textValue) { var obj = { content: textValue, metadata: { users: UserList.userData, defaultTitle: Title.defaultTitle, slideOptions: slideOptions } }; if (!initializing) { obj.metadata.title = Title.title; } if (textColor) { obj.metadata.color = textColor; } if (backColor) { obj.metadata.backColor = backColor; } // stringify the json and send it into chainpad return stringify(obj); }; var onLocal = config.onLocal = function () { if (initializing) { return; } if (isHistoryMode) { return; } if (readOnly) { return; } editor.save(); var textValue = canonicalize(CodeMirror.$textarea.val()); var shjson = stringifyInner(textValue); module.patchText(shjson); Slide.update(textValue); if (module.realtime.getUserDoc() !== shjson) { console.error("realtime.getUserDoc() !== shjson"); } }; var metadataCfg = { slideOptions: function (newOpt) { if (stringify(newOpt) !== stringify(slideOptions)) { $.extend(slideOptions, newOpt); // TODO: manage realtime + cursor in the "options" modal ?? Slide.updateOptions(); } } }; var updateColors = metadataCfg.slideColors = function (text, back) { if (text) { textColor = text; $modal.css('color', text); $modal.css('border-color', text); $pad.contents().find('#' + SLIDE_COLOR_ID).css('color', text); } if (back) { backColor = back; $modal.css('background-color', back); $pad.contents().find('#' + SLIDE_COLOR_ID).css('background', back); $pad.contents().find('#' + SLIDE_BACKCOLOR_ID).css('color', back); } }; var createFileDialog = function () { var $body = $iframe.find('body'); var $block = $body.find('#fileDialog'); if (!$block.length) { $block = $('
', {id: "fileDialog"}).appendTo($body); } $block.html(''); $('', { 'class': 'close fa fa-times', 'title': Messages.filePicker_close }).click(function () { $block.hide(); }).appendTo($block); var $description = $('

').text(Messages.filePicker_description); $block.append($description); var $filter = $('

').appendTo($block); var $container = $('', {'class': 'fileContainer'}).appendTo($block); var updateContainer = function () { $container.html(''); var filter = $filter.find('.filter').val().trim(); var list = Cryptpad.getUserFilesList(); var fo = Cryptpad.getFO(); list.forEach(function (id) { var data = fo.getFileData(id); var name = fo.getTitle(id); if (filter && name.toLowerCase().indexOf(filter.toLowerCase()) === -1) { return; } var $span = $('', {'class': 'element'}).appendTo($container); var $inner = $('').text(name); $span.append($inner).click(function () { var cleanName = name.replace(/[\[\]]/g, ''); var text = '!['+cleanName+']('+data.href+')'; editor.replaceSelection(text); $block.hide(); console.log(data.href); }); }); }; var to; $('', { type: 'text', 'class': 'filter', 'placeholder': Messages.filePicker_filter }).appendTo($filter).on('keypress', function () { if (to) { window.clearTimeout(to); } to = window.setTimeout(updateContainer, 300); }); $filter.append(' '+Messages.or+' '); var data = {FM: APP.FM}; $filter.append(Cryptpad.createButton('upload', false, data, function () { $block.hide(); })); updateContainer(); $body.keydown(function (e) { if (e.which === 27) { $block.hide(); } }); $block.show(); }; var createPrintDialog = function () { var slideOptionsTmp = { title: false, slide: false, date: false, transition: true, style: '' }; $.extend(slideOptionsTmp, slideOptions); var $container = $('

'); var $container2 = $('
').appendTo($container); var $div = $('
').appendTo($container2); var $p = $('

', {'class': 'msg'}).appendTo($div); $('').text(Messages.printOptions).appendTo($p); $p.append($('
')); // Slide number $('', {type: 'checkbox', id: 'checkNumber', checked: slideOptionsTmp.slide}).on('change', function () { var c = this.checked; console.log(c); slideOptionsTmp.slide = c; }).appendTo($p).css('width', 'auto'); $('