Просмотр исходного кода

re-plugging 'add example' route

Benoît Hubert 8 лет назад
Родитель
Сommit
2a584c97b6
5 измененных файлов с 28 добавлено и 22 удалено
  1. 1 0
      exemples/javascript/repo-config.json
  2. 1 0
      exemples/jquery/repo-config.json
  3. 1 1
      html/index.mustache.html
  4. 16 16
      js/editor.js
  5. 9 5
      sandboxApp.js

+ 1 - 0
exemples/javascript/repo-config.json

@@ -1,5 +1,6 @@
 {
   "title": "JavaScript",
+  "defaultCategory": "array",
   "categories": [
     {
       "slug": "array",

+ 1 - 0
exemples/jquery/repo-config.json

@@ -1,5 +1,6 @@
 {
   "title": "jQuery",
+  "defaultCategory": "misc",
   "categories": [
     {
       "slug": "selecteurs",

+ 1 - 1
html/index.mustache.html

@@ -41,7 +41,7 @@
             </div>{{/menuExample}}</div>
         </div>
 
-        <div id="notification"></div>
+        <div id="notification" class="alert-box"></div>
         <div class="panel-inner">
 
         {{#errorMessage}}

+ 16 - 16
js/editor.js

@@ -20,8 +20,8 @@ $(document).ready(function() {
   var $panelRight    = $('.panel-right');
   var $panelWrap     = $('.panel-container');
   var $tabItems      = $('#tabs li');
-  var $detailsRepo   = $('details-repo');
-  var $detailsExmp   = $('details-example');
+  var $detailsRepo   = $('#details-repo');
+  var $detailsExmp   = $('#details-example');
   var $window        = $(window);
   var activeMode     = 'html';
   var currentHash; 
@@ -247,18 +247,18 @@ $(document).ready(function() {
   //   });
   // }
 
-  // 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 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();
@@ -277,8 +277,8 @@ $(document).ready(function() {
     rp.post('/' + repoSlug + '/examples', { title: title })
     .then(function(newExample) {
       clearAndCloseEditor();
-      $fileSelect.append(makeFileSelectOption(newExample));
-      $fileSelect.val(newExample.slug);
+      // $fileSelect.append(makeFileSelectOption(newExample));
+      // $fileSelect.val(newExample.slug);
       notify('success', "Exemple créé !");
     })
     .catch(errText => notify('error', 'Erreur: ' + errText));

+ 9 - 5
sandboxApp.js

@@ -48,7 +48,6 @@ app.use(bodyParser.urlencoded({ extended: true }));
 app.use(getAcceptLanguage);
 
 function addExample(slug, title) {
-  examples.push({ slug: slug, title: title });
   return fs.writeFileAsync(examplesJSON, beautify(examples, null, 2, 100));
 }
 
@@ -74,23 +73,28 @@ app.get('/:repoSlug', getIndexRepo);
  */
 app.get('/:repoSlug/:exampleSlug', getIndexExample);
 
-app.post('/examples', function(req, res) {
+app.post('/:repoSlug/examples', function(req, res) {
   var title = req.body.title;
+  var { repoSlug } = req.params;
   if(! req.body.title) {
     res.status(400).send('Le titre ne peut pas être vide !');
   }
-  var existingTitle = _.find(examples, { title: title });
+  var repo = exStore.getRepo(repoSlug);
+  if(! repo) {
+    res.status(404).send("Repo " + repoSlug + "not found");
+  }
+  var existingTitle = _.find(repo.examples, { title: title });
   if(existingTitle) {
     res.status(400).send("L'exemple '" + title + "' existe déjà !");
   }
   var exampleSlug = slug(req.body.title.toLowerCase());
-  var targetDir = __dirname + '/exemples/' + exampleSlug;
+  var targetDir = __dirname + '/exemples/' + repoSlug + '/' + exampleSlug;
 
   fs.mkdirAsync(targetDir)
   .then(() => Promise.map(
     ['contenu.html', 'script.js'], f => fs.writeFileAsync(targetDir + '/' + f, '')
   ))
-  .then(files => addExample(exampleSlug, title))
+  .then(files => repo.examples.push({ slug: slug, title: title }))
   .then(() => res.json({ slug: exampleSlug, title: title }));
 });