|
|
@@ -1,8 +1,16 @@
|
|
|
$(document).ready(function() {
|
|
|
- var $editor = $('#editor');
|
|
|
- var $editorJs = $('#editor-javascript');
|
|
|
- var $editorHtml = $('#editor-html');
|
|
|
- var $htmlContent = $('#html-content');
|
|
|
+ var $editor = $('#editor');
|
|
|
+ var $editorJs = $('#editor-javascript');
|
|
|
+ var $editorHtml = $('#editor-html');
|
|
|
+ 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 $notification = $('#notification');
|
|
|
+
|
|
|
var activeTab = 'show-html';
|
|
|
|
|
|
var editor;
|
|
|
@@ -44,12 +52,73 @@ $(document).ready(function() {
|
|
|
setActiveTab('html');
|
|
|
loadJS(serverPath + 'script.js');
|
|
|
}, 'text');
|
|
|
+ }
|
|
|
+
|
|
|
+ function addFileSelectItem(item) {
|
|
|
+ $fileSelect.append(
|
|
|
+ '<option value="' + item.slug + '">' +
|
|
|
+ item.title +
|
|
|
+ '</option>'
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ function loadExampleList() {
|
|
|
+ $.get('exemples/liste.json', function(liste) {
|
|
|
+ liste.forEach(addFileSelectItem);
|
|
|
+ }, 'json');
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
+ function notify(type, text) {
|
|
|
+ $notification
|
|
|
+ .addClass(type)
|
|
|
+ .addClass('active');
|
|
|
+ $notification.html(text);
|
|
|
+ setTimeout(function() {
|
|
|
+ $notification.removeClass('active');
|
|
|
+ }, 2000);
|
|
|
+ setTimeout(function() {
|
|
|
+ $notification.removeClass(type);
|
|
|
+ }, 3000);
|
|
|
}
|
|
|
|
|
|
- var fileSelect = $('#fileSelect');
|
|
|
- fileSelect.change(loadExample);
|
|
|
+ function toggleEditor() {
|
|
|
+ $addExampleBtn.toggle();
|
|
|
+ $selectorNav.toggle();
|
|
|
+ $exampleForm.toggle();
|
|
|
+ }
|
|
|
+
|
|
|
+ // function ajaxPost(url, data, success, error) {}
|
|
|
+
|
|
|
+ function saveExample(e) {
|
|
|
+ e.preventDefault();
|
|
|
+ var title = $(this).find('input[name="title"]').val();
|
|
|
+ $.ajax({
|
|
|
+ type: 'POST',
|
|
|
+ url: '/examples',
|
|
|
+ data: JSON.stringify({ title }),
|
|
|
+ success: function(newExample) {
|
|
|
+ clearAndCloseEditor();
|
|
|
+ addFileSelectItem(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'
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ function clearAndCloseEditor() {
|
|
|
+ $exampleForm.find('input').val('');
|
|
|
+ toggleEditor();
|
|
|
+ }
|
|
|
|
|
|
+ $fileSelect.change(loadExample);
|
|
|
+ $addExampleBtn.click(toggleEditor);
|
|
|
+ $exampleCancel.click(clearAndCloseEditor);
|
|
|
+ $exampleForm.submit(saveExample);
|
|
|
+ loadExampleList();
|
|
|
// setActiveTab('html');
|
|
|
});
|