| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- "use strict";
- $(document).ready(function() {
- var $editor = $('#editor');
- var $editorJs = $('#editor-javascript');
- var $editorHtml = $('#editor-html');
- var $editorCss = $('#editor-css');
- var $htmlContent = $('#html-content');
- var $selectorNav = $('#selector');
- var $fileSelect = $('#file-select');
- var $addExampleBtn = $('#add-example-btn');
- var $exampleForm = $('#add-example-form');
- var $exampleSave = $('#add-example-save');
- var $exampleCancel = $('#add-example-cancel');
- var $saveChanges = $('#save-changes');
- var $notification = $('#notification');
- var $revertEditor = $('#revert-editor');
- var $panelLeft = $('.panel-left');
- var $panelRight = $('.panel-right');
- var $panelWrap = $('.panel-container');
- var $tabItems = $('#tabs li');
- var $window = $(window);
- var activeMode = 'html';
- var currentHash;
- var editor;
- var editorSession;
- var editorStorage = new LocalStorageDraft();
- var saveTimeout1;
- var saveTimeout2;
- var exampleList;
- var mapTypes = {
- html: 'html', js: 'javascript', css: 'css'
- }
- if(_webSandboxFiles.length === 0) {
- return;
- }
- // /**
- // * Make the left panel resizable
- // */
- // $panelLeft.resizable({
- // handleSelector: ".splitter",
- // resizeHeight: false,
- // onDrag: function(e) {
- // $editor.width($panelLeft.width());
- // }
- // });
- // /**
- // * Handle window resize
- // */
- // function onResize() {
- // $editor.width($panelLeft.width());
- // $panelWrap.height($window.height());
- // }
- // $window.resize(onResize);
- // onResize();
- function initEditor() {
- editor = ace.edit("editor");
- editorSession = editor.getSession();
- editor.setTheme("ace/theme/eclipse");
- editor.$blockScrolling = Infinity;
- editorSession.setUseWrapMode(true);
- if(_webSandboxFiles) {
- editorSession.setMode("ace/mode/html");
- var firstHtml = _webSandboxFiles.find(f => (f.type === 'html'));
- editorSession.setValue(firstHtml.content);
- }
- }
- /**
- * File list tabs click handler
- */
- $tabItems.click(function() {
- var clickedItem = $(this);
- var type = clickedItem.data('type');
- editorSession.setMode("ace/mode/" + mapTypes[type]);
- var name = clickedItem.html();
- var file = _webSandboxFiles.find(f => (f.name === name));
- editorSession.setValue(file.content);
- // saveToLocalStorage();
- // var mode = $(this).prop('id').substr(5);
- // console.log( .html() );
- // setActiveTab(mode);
- })
- // /**
- // * Set the current hash. If none given, take it from lococation.hash
- // */
- // function setCurrentHash(slug) {
- // if(slug) {
- // window.location.hash = currentHash = slug;
- // }
- // else {
- // currentHash = window.location.hash ?
- // window.location.hash.substr(1) : undefined;
- // }
- // }
- // /**
- // * Set editor mode (html, javascript, css)
- // */
- // function setEditorMode(mode) {
- // editor.getSession().setMode("ace/mode/" + mode);
- // }
- // /**
- // * Save a copy in localStorage
- // */
- // function saveToLocalStorage() {
- // var editorContent = editor.getSession().getValue();
- // editorStorage.saveSource(activeMode, editorContent);
- // saveTimeout1 = undefined;
- // }
- // /**
- // * React to changes in editor by saving a copy
- // */
- // function editorContentChanged() {
- // // console.log('editorContent changed')
- // if(saveTimeout1 || saveTimeout2) {
- // clearTimeout(saveTimeout1);
- // clearTimeout(saveTimeout2);
- // }
- // saveTimeout1 = setTimeout(saveToLocalStorage, 500);
- // // saveTimeout2 = setTimeout(saveChanges, 1000);
- // }
- // editor.getSession().on('change', editorContentChanged);
- // function setActiveTab(mode) {
- // console.log('setting mode', mode);
- // var elementId = 'show-' + mode;
- // $('#show-' + activeMode).removeClass('active');
- // activeMode = mode;
- // $('#' + elementId).addClass('active');
- // var ed = $('#editor-' + mode);
- // setEditorMode(mode);
- // editor.getSession().off('change');
- // editor.getSession().setValue(ed[0].innerHTML);
- // editor.getSession().on('change', editorContentChanged);
- // }
- // function loadExample(exampleSlug) {
- // // console.log('loadExample', exampleSlug);
- // var serverPath = 'exemples/jquery/' + exampleSlug + '/';
- // rp.get(serverPath + 'script.js', 'text')
- // .then(javascript => $editorJs.html(javascript))
- // .then(() => rp.get(serverPath + 'example.html', 'text'))
- // .then(() => rp.get(serverPath + 'styles.css', 'text')
- // .then(css => $editorCss.html(css))
- // .catch(err => {
- // return '';
- // })
- // )
- // .then(html => {
- // $editorHtml.html(html);
- // $('iframe.panel-right')
- // .prop('src', '/examples/' + exampleSlug)
- // setActiveTab('html');
- // setCurrentHash(exampleSlug);
- // var sources = {
- // html: $editorHtml.html(),
- // javascript: $editorJs.html()
- // };
- // editorStorage.init(exampleSlug, sources);
- // })
- // .then(() => {
- // var item = _.find(exampleList, { slug: exampleSlug });
- // // console.log(item.test ? 'test' : 'no test');
- // // loadJS('exemples/' + item.slug + '/test.js', function() {
- // // $('#tests').show();
- // // });
- // });
- // }
- // /**
- // * Build select option string
- // */
- // function makeFileSelectOption(item) {
- // return '<option value="' + item.slug + '">' +
- // item.title +
- // '</option>'
- // }
- // /**
- // * Populate file selector from JSON list content
- // */
- // function resetFileSelect(exampleList) {
- // $fileSelect.html(
- // '<option value="-">—</option>' +
- // exampleList.map(makeFileSelectOption)
- // );
- // }
- // function loadExampleList() {
- // rp.get('/list/jquery', 'json')
- // .then(function(_exampleList) {
- // exampleList = _exampleList;
- // var restoredDraft;
- // resetFileSelect(exampleList);
- // if(currentHash) {
- // $fileSelect.val(currentHash);
- // var item = _.find(exampleList, { slug: currentHash });
- // if( ! item) {
- // return;
- // }
- // restoredDraft = editorStorage.restore(item.slug);
- // if(! restoredDraft) {
- // loadExample(item.slug);
- // }
- // else {
- // console.log('restore', item.slug, restoredDraft.sources);
- // $editorHtml.html(restoredDraft.sources.html);
- // $editorCss.html(restoredDraft.sources.css);
- // $editorJs.html(restoredDraft.sources.javascript);
- // $('iframe.panel-right')
- // .prop('src', '/examples/' + item.slug)
- // // loadJS('exemples/' + item.slug + '/script.js');
- // // if(item.test) {
- // // loadJS('exemples/' + item.slug + '/test.js', function() {
- // // $('#tests').show();
- // // });
- // // }
- // setActiveTab('html');
- // }
- // }
- // });
- // }
- // function notify(type, text) {
- // $notification
- // .addClass(type)
- // .addClass('active');
- // $notification.html(text);
- // setTimeout(function() {
- // $notification.removeClass('active');
- // }, 2000);
- // setTimeout(function() {
- // $notification.removeClass(type);
- // }, 3000);
- // }
- // function toggleEditor() {
- // $addExampleBtn.toggle();
- // $selectorNav.toggle();
- // $exampleForm.toggle();
- // }
- // /**
- // * Save new example
- // */
- // function saveExample(e) {
- // e.preventDefault();
- // var title = $(this).find('input[name="title"]').val();
- // rp.post('/examples', { title: title })
- // .then(function(newExample) {
- // clearAndCloseEditor();
- // $fileSelect.append(makeFileSelectOption(newExample));
- // $fileSelect.val(newExample.slug);
- // notify('success', "Exemple créé !");
- // })
- // .catch(errText => notify('error', 'Erreur: ' + errText));
- // }
- // function clearAndCloseEditor() {
- // $exampleForm.find('input').val('');
- // toggleEditor();
- // }
- // function revertEditor() {
- // editorStorage.reset();
- // location.reload();
- // }
- // function saveChanges() {
- // var payload = editorStorage.getSources();
- // rp.put('/examples/' + currentHash, payload)
- // .then(function(newExample) {
- // notify('success', "Exemple sauvegardé !");
- // loadExample(currentHash);
- // })
- // .catch(function(err) {
- // notify('error', 'Erreur: ' + jqXHR.responseText);
- // });
- // }
- // setCurrentHash();
- // $fileSelect.change(function() {
- // loadExample($(this).val());
- // });
- // $addExampleBtn.click(toggleEditor);
- // $exampleCancel.click(clearAndCloseEditor);
- // $saveChanges.click(saveChanges);
- // $exampleForm.submit(saveExample);
- // $revertEditor.click(revertEditor);
- // $.get('/menu', menu => $('#site-menu').html(menu), 'html');
- // loadExampleList();
- initEditor();
- });
|