editor.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. $(document).ready(function() {
  2. var $editor = $('#editor');
  3. var $editorJs = $('#editor-javascript');
  4. var $editorHtml = $('#editor-html');
  5. var $htmlContent = $('#html-content');
  6. var $selectorNav = $('#selector');
  7. var $fileSelect = $('#file-select');
  8. var $addExampleBtn = $('#add-example-btn');
  9. var $exampleForm = $('#add-example-form');
  10. var $exampleSave = $('#add-example-save');
  11. var $exampleCancel = $('#add-example-cancel');
  12. var $notification = $('#notification');
  13. var activeTab = 'show-html';
  14. var editor;
  15. function initEditor(mode) {
  16. editor = ace.edit("editor");
  17. editor.setTheme("ace/theme/eclipse");
  18. editor.getSession().setMode("ace/mode/" + mode);
  19. editor.getSession().setUseWrapMode(true);
  20. }
  21. // editor.getSession().on('change', function() {
  22. // console.log(arguments)
  23. // });
  24. function setActiveTab(which) {
  25. var elementId = 'show-' + which;
  26. $('#' + activeTab).removeClass('active');
  27. activeTab = elementId;
  28. $('#' + elementId).addClass('active');
  29. var ed = $('#editor-' + which);
  30. initEditor(which);
  31. editor.getSession().setValue(ed[0].innerHTML);
  32. }
  33. $('#tabs button').click(function() {
  34. var which = $(this).prop('id').substr(5);
  35. setActiveTab(which);
  36. })
  37. function loadExample() {
  38. var exampleName = $(this).val();
  39. var serverPath = 'exemples/' + exampleName + '/';
  40. $.get(serverPath + 'script.js', function(javascript) {
  41. $editorJs.html(javascript);
  42. }, 'text');
  43. $.get(serverPath + '/contenu.html', function(html) {
  44. $editorHtml.html(html);
  45. $htmlContent.html(html);
  46. setActiveTab('html');
  47. loadJS(serverPath + 'script.js');
  48. }, 'text');
  49. }
  50. function addFileSelectItem(item) {
  51. $fileSelect.append(
  52. '<option value="' + item.slug + '">' +
  53. item.title +
  54. '</option>'
  55. );
  56. }
  57. function loadExampleList() {
  58. $.get('exemples/liste.json', function(liste) {
  59. liste.forEach(addFileSelectItem);
  60. }, 'json');
  61. }
  62. function notify(type, text) {
  63. $notification
  64. .addClass(type)
  65. .addClass('active');
  66. $notification.html(text);
  67. setTimeout(function() {
  68. $notification.removeClass('active');
  69. }, 2000);
  70. setTimeout(function() {
  71. $notification.removeClass(type);
  72. }, 3000);
  73. }
  74. function toggleEditor() {
  75. $addExampleBtn.toggle();
  76. $selectorNav.toggle();
  77. $exampleForm.toggle();
  78. }
  79. // function ajaxPost(url, data, success, error) {}
  80. function saveExample(e) {
  81. e.preventDefault();
  82. var title = $(this).find('input[name="title"]').val();
  83. $.ajax({
  84. type: 'POST',
  85. url: '/examples',
  86. data: JSON.stringify({ title }),
  87. success: function(newExample) {
  88. clearAndCloseEditor();
  89. addFileSelectItem(newExample);
  90. notify('success', "Exemple sauvegardé !");
  91. },
  92. error: function(jqXHR, textStatus, errorThrown ) {
  93. console.log(jqXHR, textStatus, errorThrown);
  94. notify('error', 'Erreur: ' + jqXHR.responseText);
  95. },
  96. contentType: 'application/json',
  97. dataType: 'json'
  98. });
  99. }
  100. function clearAndCloseEditor() {
  101. $exampleForm.find('input').val('');
  102. toggleEditor();
  103. }
  104. $fileSelect.change(loadExample);
  105. $addExampleBtn.click(toggleEditor);
  106. $exampleCancel.click(clearAndCloseEditor);
  107. $exampleForm.submit(saveExample);
  108. loadExampleList();
  109. // setActiveTab('html');
  110. });