Commits

Anonymous committed 28abe78

edit/show work. time to syle

Comments (0)

Files changed (7)

_attachments/js/blog.js

+Date.prototype.parse_rfc3339 = function (string) {
+    var date=new Date(0);
+    var match = string.match(/(\d{4})-(\d\d)-(\d\d)\s*(?:[\sT]\s*(\d\d):(\d\d)(?::(\d\d))?(\.\d*)?\s*(Z|([-+])(\d\d):(\d\d))?)?/);
+    if (!match) return;
+    if (match[2]) match[2]--;
+    if (match[7]) match[7] = (match[7]+'000').substring(1,4);
+    var field = [null,'FullYear','Month','Date','Hours','Minutes','Seconds','Milliseconds'];
+    for (var i=1; i<=7; i++) if (match[i]) date['setUTC'+field[i]](match[i]);
+    if (match[9]) date.setTime(date.getTime()+(match[9]=='-'?1:-1)*(match[10]*3600000+match[11]*60000) );
+    return date.getTime();
+}
+
+function f(n) {    // Format integers to have at least two digits.
+    return n < 10 ? '0' + n : n;
+}
+Date.prototype.rfc3339 = function() {
+    return this.getUTCFullYear()   + '-' +
+         f(this.getUTCMonth() + 1) + '-' +
+         f(this.getUTCDate())      + 'T' +
+         f(this.getUTCHours())     + ':' +
+         f(this.getUTCMinutes())   + ':' +
+         f(this.getUTCSeconds())   + 'Z';
+};
+
 
 
 function Blog(app) {
         var converter = new Showdown.converter();
         return converter.makeHtml(body);
     }
+
+    this.link_for = function(docid) {
+        return '<a href="'+app.showPath('post',docid)+'">'+docid+'</a>';
+    }
 }
 
 

_attachments/js/editor.js

     }
 
     
-    $("#content").TextArea({
+    $("#body").TextArea({
         change_callback: converter_callback 
     });
 
     var lastRoomleft = 0;
-    var bottomEditHeight = $('#main-footer').offset().top - $('#content').offset().top -
-            $("#content").height();
+    var bottomEditHeight = $('#main-footer').offset().top - $('#body').offset().top -
+            $("#body").height();
     
     $(window).resize(resizePanes);
     
     window.setInterval(resizePanes, 250);
     function resizePanes() {
         var roomLeft =  $(window).height() - $('#main-footer').height() 
-         - $('#content').offset().top - bottomEditHeight;
+         - $('#body').offset().top - bottomEditHeight;
         
         if (roomLeft < 0)
             roomLeft = 0;
 
         lastRoomLeft = roomLeft;
         
-        $('#content').height(roomLeft);
+        $('#body').height(roomLeft);
         $('#mdpreview').height(roomLeft);  
             
     }

_attachments/js/jquery.couchapp.js

               $.cookies.set("login", login, '/'+dbname)
               win && win(login);
             } else {
-              $.cookies.set("login", "", '/'+dbname)
+              $.cookies.set("login" "", '/'+dbname)
               fail && fail(s, e, r);
             }
           }});        

_attachments/js/textarea.jquery.js

                 return;
             }
             this.change_callback();  
-            this.el.keydown(this.change_callback).keyup(this.change_callback);
+            this.el.keyup(this.change_callback);
+            this.el.bind('paste', this.change_callback);
+            this.el.bind('input', this.change_callback);
         },
 
         handleKey: function(e) {

lib/templates/edit.html

     <link rel="openid.server" href="http://www.myopenid.com/server">
     <link rel="openid.delegate" href="http://bchesneau.myopenid.com/">
     <link rel="home index" href="http://benoitc.org/" type="text/html">
-    <link rel="me author" href="http://benoitc.org/apropos.html" type="text/html">
+    <link rel="me author" href="http://benoitc.org/benoitc.html" type="text/html">
 
     <link rel="stylesheet" href="<%= assets %>/css/layout.css" type="text/css" media="screen, projection" />
     <link rel="stylesheet" href="<%= assets %>/css/default.css" type="text/css" media="screen, projection" />
 <body>
         <header>
         <h1>
-            <a rel="me author" href="<%= assets %>/about.html">Benoît Chesneau</a>
+            <a rel="me author" href="<%= assets %>/benoitc.html">Benoît Chesneau</a>
         </h1>
         </header>
         <div id="edit">
+            <h2>Nouvel article</h2>
             <form id="fedit" name="fedit" action="new.html"
                 method="post">
                 <p><label for="title">Titre<label><input type="text"
                         name="title" id="title" value=""></p>
                         
-                <textarea name="content" id="content" cols="80"
+                <textarea name="body" id="body" cols="80"
                     rows="20"></textarea>
 
                 <p><label for="labels">Libellés</label><input type="text"
                 name="labels" id="labels" value=""></p>
                 <p><input type="submit" name="save" id="save" value="Save">
-            or <a href="cancel">Cancel</a></p>
+            or <a href="cancel">Cancel</a> <span id="saved" style="display:none;">Saved</span></p>
             </form>
             <div id="mdpreview"></div>
         </div>
 
         <footer id="main-footer">
             <p class="copyright">Copyright &copy; 2009 by <a rel="me author"
-                href="apropos.html">Benoît Chesneau</a> </p>
-
-        
+                href="benoitc.html">Benoît Chesneau</a> </p>
         </footer>
 
     
     <script src="<%= assets %>/js/jquery.couchapp.js"></script>
     <script src="<%= assets %>/js/editor.js"></script>
     <script src="<%= assets %>/js/blog.js"></script>
+    <script src="<%= assets %>/js/urlify.js"></script>
+
     <script>
         $(document).ready(function() {
 
                     // setup CouchApp document/form system, adding app-specific callbacks
                     // rename docForm?
                     var B = new Blog(app);
-                    // thin controller - move to B?
 
-                    var postForm = app.docForm("form#new-post", {
+                    var postForm = app.docForm("form#fedit", {
                         id : <%= docid %>,
-                        fields : ["title", "body", "tags"],
+                        fields : ["title", "body", "labels"],
+                        template : {
+                            doc_type : "post",
+                            format : "markdown",
+                            author : login
+                        },
                         onLoad : function(doc) {
                             if (doc._id) {
+                                $("#edit h2").html('Édition de ' + B.link_for(doc._id));
+                            }
 
-                            }
+                            $('#save').click(function() {
+                                if (!$("#title").val().match(/^['\-\"\/ !?&.,;:@\(\)\w\u00A1-\uFFFF]+$/i)) {
+                                    alert("Titre invalide.");
+                                    return false;
+                                }
+                            });
+
                         },
                         beforeSave : function(doc) {
-                            doc.html = B.formatBody(doc.content);
-                            doc.format = "markdown";
+                            doc.html = B.formatBody(doc.body);
 
+
+                            if (!doc.created_at) {
+                                doc.created_at = new Date().rfc3339();
+                            }
+
+                            if (!doc.slug) {
+                                doc.slug = doc.created_at.slice(0, 10) + "_" +
+                                   URLify(doc.title, 255);
+                                doc._id = doc.slug;
+                            }
+
+
+                            doc.updated = new Date().rfc3339();
                             if(doc.labels) {
                                 doc.labels = doc.labels.split(",");
                                 for(var idx in doc.labels) {
-                                    doc.labelsidx] = $.trim(doc.labels[idx]);
+                                    doc.labels[idx] = $.trim(doc.labels[idx]);
                                 }
                             } 
                         },
                         success : function(resp) {
-                           /* $("#saved").text("Saved _rev: "+resp.rev).fadeIn(500).fadeOut(3000);
-                            B.editing(resp.id);*/
+                           $("#saved").text("Saved _rev: "+resp.rev).fadeIn(500).fadeOut(3000);
+                           $("#edit h2").html('Édition de ' + B.link_for(resp.id));
                         }
                     });  
 
                     document.location = absurl;
                 });
             });
-            
-            
-            
+        
         });
     </script>
-
 </html>
+function(doc, req) {  
+  // !json lib.templates.post
+  // !code lib.helpers.ejs.ejs
+  // !code lib.helpers.ejs.view
+  // !code lib.helpers.template2
+  // !code lib.helpers.couchapp
+
+
+  // we only show html
+  return template(lib.templates.post, {
+    doc: doc,
+    assets : assetPath(),
+    editPostPath : showPath('edit', doc._id),
+    index : listPath('index','recent-posts',{descending:true, limit:8})
+  });
+}

validate_doc_update.js

     }
   }
 
-  /*
+  
   // general timestamps
   if (oldDoc && oldDoc.created_at != newDoc.created_at) 
     forbidden("You may not change the created_at field of a doc.");
   
   // this ensures that the date will be UTC, parseable, and collate correctly
-  if (newDoc.created_at) {
-    if (!newDoc.created_at.match(/\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2} \+0000/))
-      forbidden("Sorry, "+newDoc.created_at+" is not a valid date format. Try: 2008/12/10 21:16:02 +0000");
-  }
+  //if (newDoc.created_at) {
+  //  if (!newDoc.created_at.match(/\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2} \+0000/))
+  //    forbidden("Sorry, "+newDoc.created_at+" is not a valid date format. Try: 2008/12/10 21:16:02 +0000");
+  //}
     
   if (type == 'post') {
     // post required fields
     require(newDoc.comment, "Comments require a comment field.");
     require(newDoc.format, "Comments require a format field.");
     if (newDoc.commenter && newDoc.commenter.url) {*/
-   //   require(newDoc.commenter.url.match(/^https?:\/\/[^.]*\..*/), "Commenter URL is not valid.");      
-   // }
-  //}
+      require(newDoc.commenter.url.match(/^https?:\/\/[^.]*\..*/), "Commenter URL is not valid.");      
+    }
+  }
   return true;
 }