|
@@ -9,49 +9,111 @@ $(document).ready(function() {
|
|
|
var $exampleForm = $('#add-example-form');
|
|
var $exampleForm = $('#add-example-form');
|
|
|
var $exampleSave = $('#add-example-save');
|
|
var $exampleSave = $('#add-example-save');
|
|
|
var $exampleCancel = $('#add-example-cancel');
|
|
var $exampleCancel = $('#add-example-cancel');
|
|
|
|
|
+ var $saveChanges = $('#save-changes');
|
|
|
var $notification = $('#notification');
|
|
var $notification = $('#notification');
|
|
|
|
|
+ var activeMode = 'html';
|
|
|
|
|
+ var currentHash;
|
|
|
|
|
+ var editor;
|
|
|
|
|
+ var editorStorage = new LocalStorageDraft();
|
|
|
|
|
+ var saveTimeout;
|
|
|
|
|
|
|
|
- var activeTab = 'show-html';
|
|
|
|
|
|
|
|
|
|
- var editor;
|
|
|
|
|
|
|
+ editor = ace.edit("editor");
|
|
|
|
|
+ editor.setTheme("ace/theme/eclipse");
|
|
|
|
|
+ editor.$blockScrolling = Infinity;
|
|
|
|
|
+ editor.getSession().setUseWrapMode(true);
|
|
|
|
|
+
|
|
|
|
|
+ function setCurrentHash(slug) {
|
|
|
|
|
+ if(slug) {
|
|
|
|
|
+ console.log('save current hash', slug);
|
|
|
|
|
+ window.location.hash = currentHash = slug;
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ currentHash = window.location.hash ?
|
|
|
|
|
+ window.location.hash.substr(1) : undefined;
|
|
|
|
|
+ if(currentHash) console.log('restored current hash', currentHash);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- function initEditor(mode) {
|
|
|
|
|
- editor = ace.edit("editor");
|
|
|
|
|
- editor.setTheme("ace/theme/eclipse");
|
|
|
|
|
|
|
+ function setEditorMode(mode) {
|
|
|
editor.getSession().setMode("ace/mode/" + mode);
|
|
editor.getSession().setMode("ace/mode/" + mode);
|
|
|
- editor.getSession().setUseWrapMode(true);
|
|
|
|
|
}
|
|
}
|
|
|
- // editor.getSession().on('change', function() {
|
|
|
|
|
- // console.log(arguments)
|
|
|
|
|
- // });
|
|
|
|
|
-
|
|
|
|
|
- function setActiveTab(which) {
|
|
|
|
|
- var elementId = 'show-' + which;
|
|
|
|
|
- $('#' + activeTab).removeClass('active');
|
|
|
|
|
- activeTab = elementId;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ function saveToLocalStorage() {
|
|
|
|
|
+ var editorContent = editor.getSession().getValue();
|
|
|
|
|
+ console.log('saveToLocalStorage', activeMode, editorContent.substr(0, 10) + '[...]');
|
|
|
|
|
+ editorStorage.saveSource(activeMode, editorContent);
|
|
|
|
|
+ saveTimeout = undefined;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function editorContentChanged() {
|
|
|
|
|
+ if(saveTimeout) {
|
|
|
|
|
+ clearTimeout(saveTimeout);
|
|
|
|
|
+ }
|
|
|
|
|
+ saveTimeout = setTimeout(saveToLocalStorage, 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');
|
|
$('#' + elementId).addClass('active');
|
|
|
- var ed = $('#editor-' + which);
|
|
|
|
|
- initEditor(which);
|
|
|
|
|
|
|
+ var ed = $('#editor-' + mode);
|
|
|
|
|
+ setEditorMode(mode);
|
|
|
editor.getSession().setValue(ed[0].innerHTML);
|
|
editor.getSession().setValue(ed[0].innerHTML);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$('#tabs button').click(function() {
|
|
$('#tabs button').click(function() {
|
|
|
- var which = $(this).prop('id').substr(5);
|
|
|
|
|
- setActiveTab(which);
|
|
|
|
|
|
|
+ var mode = $(this).prop('id').substr(5);
|
|
|
|
|
+ setActiveTab(mode);
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- function loadExample() {
|
|
|
|
|
- var exampleName = $(this).val();
|
|
|
|
|
- var serverPath = 'exemples/' + exampleName + '/';
|
|
|
|
|
- $.get(serverPath + 'script.js', function(javascript) {
|
|
|
|
|
- $editorJs.html(javascript);
|
|
|
|
|
- }, 'text');
|
|
|
|
|
- $.get(serverPath + '/contenu.html', function(html) {
|
|
|
|
|
|
|
+ function loadAsync(url, dataType) {
|
|
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
|
|
+ $.ajax({
|
|
|
|
|
+ type: 'GET',
|
|
|
|
|
+ url: url,
|
|
|
|
|
+ success: function(data) {
|
|
|
|
|
+ resolve(data);
|
|
|
|
|
+ },
|
|
|
|
|
+ error: function(jqXHR) {
|
|
|
|
|
+ reject(new Error(jqXHR.responseText));
|
|
|
|
|
+ }
|
|
|
|
|
+ }, dataType);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function loadExample(exampleSlug) {
|
|
|
|
|
+ var serverPath = 'exemples/' + exampleSlug + '/';
|
|
|
|
|
+ // $.get(serverPath + 'script.js', function(javascript) {
|
|
|
|
|
+ // $editorJs.html(javascript);
|
|
|
|
|
+ // }, 'text');
|
|
|
|
|
+ // $.get(serverPath + '/contenu.html', function(html) {
|
|
|
|
|
+ // $editorHtml.html(html);
|
|
|
|
|
+ // $htmlContent.html(html);
|
|
|
|
|
+ // setActiveTab('html');
|
|
|
|
|
+ // setCurrentHash(exampleSlug);
|
|
|
|
|
+ // loadJS(serverPath + 'script.js');
|
|
|
|
|
+ // }, 'text');
|
|
|
|
|
+ loadAsync(serverPath + 'script.js', 'text')
|
|
|
|
|
+ .then(javascript => $editorJs.html(javascript))
|
|
|
|
|
+ .then(() => loadAsync(serverPath + '/contenu.html', 'text'))
|
|
|
|
|
+ .then(html => {
|
|
|
$editorHtml.html(html);
|
|
$editorHtml.html(html);
|
|
|
$htmlContent.html(html);
|
|
$htmlContent.html(html);
|
|
|
setActiveTab('html');
|
|
setActiveTab('html');
|
|
|
|
|
+ setCurrentHash(exampleSlug);
|
|
|
|
|
+ var sources = {
|
|
|
|
|
+ html: $editorHtml.html(),
|
|
|
|
|
+ javascript: $editorJs.html()
|
|
|
|
|
+ };
|
|
|
|
|
+ editorStorage.init(exampleSlug, sources);
|
|
|
loadJS(serverPath + 'script.js');
|
|
loadJS(serverPath + 'script.js');
|
|
|
- }, 'text');
|
|
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function addFileSelectItem(item) {
|
|
function addFileSelectItem(item) {
|
|
@@ -64,7 +126,29 @@ $(document).ready(function() {
|
|
|
|
|
|
|
|
function loadExampleList() {
|
|
function loadExampleList() {
|
|
|
$.get('exemples/liste.json', function(liste) {
|
|
$.get('exemples/liste.json', function(liste) {
|
|
|
|
|
+ var didRestore;
|
|
|
liste.forEach(addFileSelectItem);
|
|
liste.forEach(addFileSelectItem);
|
|
|
|
|
+ if(currentHash) {
|
|
|
|
|
+ console.log('I have current hash', currentHash);
|
|
|
|
|
+ $fileSelect.val(currentHash);
|
|
|
|
|
+ var item = _.find(liste, { slug: currentHash });
|
|
|
|
|
+ if( ! item) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ console.log('I have current item', item);
|
|
|
|
|
+ restoredDraft = editorStorage.restore(item.slug);
|
|
|
|
|
+ if(! restoredDraft) {
|
|
|
|
|
+ console.log('I did not restore');
|
|
|
|
|
+ loadExample(item.slug);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ console.log('I did restore');
|
|
|
|
|
+ $editorHtml.html(restoredDraft.sources.html);
|
|
|
|
|
+ $editorJs.html(restoredDraft.sources.javascript);
|
|
|
|
|
+ setActiveTab('html');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}, 'json');
|
|
}, 'json');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -99,7 +183,7 @@ $(document).ready(function() {
|
|
|
success: function(newExample) {
|
|
success: function(newExample) {
|
|
|
clearAndCloseEditor();
|
|
clearAndCloseEditor();
|
|
|
addFileSelectItem(newExample);
|
|
addFileSelectItem(newExample);
|
|
|
- notify('success', "Exemple sauvegardé !");
|
|
|
|
|
|
|
+ notify('success', "Exemple créé !");
|
|
|
},
|
|
},
|
|
|
error: function(jqXHR, textStatus, errorThrown ) {
|
|
error: function(jqXHR, textStatus, errorThrown ) {
|
|
|
console.log(jqXHR, textStatus, errorThrown);
|
|
console.log(jqXHR, textStatus, errorThrown);
|
|
@@ -115,9 +199,33 @@ $(document).ready(function() {
|
|
|
toggleEditor();
|
|
toggleEditor();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $fileSelect.change(loadExample);
|
|
|
|
|
|
|
+ function saveChanges() {
|
|
|
|
|
+ var payload = editorStorage.getSources();
|
|
|
|
|
+
|
|
|
|
|
+ $.ajax({
|
|
|
|
|
+ type: 'PUT',
|
|
|
|
|
+ url: '/examples/' + currentHash,
|
|
|
|
|
+ data: JSON.stringify(payload),
|
|
|
|
|
+ success: function(newExample) {
|
|
|
|
|
+ notify('success', "Exemple sauvegardé !");
|
|
|
|
|
+ },
|
|
|
|
|
+ error: function(jqXHR, textStatus, errorThrown ) {
|
|
|
|
|
+ console.log(jqXHR, textStatus, errorThrown);
|
|
|
|
|
+ notify('error', 'Erreur: ' + jqXHR.responseText);
|
|
|
|
|
+ },
|
|
|
|
|
+ contentType: 'application/json',
|
|
|
|
|
+ dataType: 'json'
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ setCurrentHash();
|
|
|
|
|
+
|
|
|
|
|
+ $fileSelect.change(function() {
|
|
|
|
|
+ loadExample($(this).val());
|
|
|
|
|
+ });
|
|
|
$addExampleBtn.click(toggleEditor);
|
|
$addExampleBtn.click(toggleEditor);
|
|
|
$exampleCancel.click(clearAndCloseEditor);
|
|
$exampleCancel.click(clearAndCloseEditor);
|
|
|
|
|
+ $saveChanges.click(saveChanges);
|
|
|
$exampleForm.submit(saveExample);
|
|
$exampleForm.submit(saveExample);
|
|
|
loadExampleList();
|
|
loadExampleList();
|
|
|
// setActiveTab('html');
|
|
// setActiveTab('html');
|