Commits

Benoît Allard committed 834fcc6

We are getting somewhere ...

Comments (0)

Files changed (3)

   window.Users = Users
   window.Messages = Messages
 
+  update_user = ->
+    Users.update(
+          {name: name},
+          {$set: {last_visit: Date.now()}})
+
+  logged_on = ->
+    Session.get('user_name') != undefined
+
   Template.login.user = ->
     Session.get('user_name')
 
           id = Users.insert({name: name})
           console.log "Added user", name, id
         Session.set('user_name', name)
-        Users.update(
-          {name: name},
-          {$set: {last_visit: Date.now()}})
         $('#username').val('')
     'click #logout': (event) ->
       Session.set('user_name')
 
-  Template.messages.messages = ->
+  Template.news.show = logged_on
+  Template.new.show = logged_on
+
+  Template.new.add_msg = ->
+    Session.get('add_msg')
+
+  Template.new.warning = ->
+    Messages.find({user: Session.get('user_name'), archived: false}).count() > 3
+
+  Template.new.nummsg = ->
+    Messages.find({user: Session.get('user_name'), archived: false}).count()
+
+  Template.new.events =
+    'click #new': (event) ->
+      Session.set('add_msg', true)
+    'click #cancel': (event) ->
+      Session.set('add_msg', false)
+    'click #submit': (event) ->
+      text = $('#content').val()
+      now = Date.now()
+      Messages.insert(
+        content: text,
+        user:Session.get('user_name'),
+        archived: false,
+        created_on: now,
+        last_modified_on: now)
+      Session.set('add_msg',false)
+
+  Template.news.news = ->
+    usr = Users.findOne({name: Session.get('user_name')})
+    console.log usr, Session.get('user_name')
+    Messages.find({
+        user: {$ne: usr.name},
+        created_on: {$gt: usr.last_visit}},{
+        sort: {created_on: -1}})
+
+  Template.news.mods = ->
+    usr = Users.findOne({name: Session.get('user_name')})
+    console.log usr, Session.get('user_name')
+    Messages.find({
+        user: {$ne: usr.name},
+        last_modified_on: {$gt: usr.last_visit}
+        created_on: {$lte: usr.last_visit}},{
+        sort: {last_modified_on: -1}})
+
+  Template.users.users = ->
+    Users.find(
+      {name: {$ne: Session.get('user_name')}},
+      sort:
+        name: 1
+      )
+
+  Template.user.messages = ->
+    Messages.find({user: this.name, archived:false}, {sort: {last_modified_on: -1}, limit:3})
+
+  Template.mines.messages = ->
     Messages.find(
-      {},
+      {user: Session.get('user_name')},
       sort:
-        date: -1
+        last_modified_on: -1
       )
+
+  Template.message.show = ->
+    this.user == Session.get('user_name') or not this.archived
+
+  Template.message.loggedonuser = ->
+    this.user == Session.get('user_name')
+
+  Template.message.printauthor = ->
+    this.user != Session.get('user_name')
+
+  Template.message.ago = (date) ->
+    '3 years ago'
+
+  Template.message.edited = ->
+    this.created_on != this.last_modified_on
+
+  Template.message.events =
+    "click #archive": ->
+      Messages.update({_id: this._id},{$set: {archived: true}})
+    "click #unarchive": ->
+      Messages.update({_id: this._id},{$set: {archived: false}})
 </head>
 
 <body>
-  <div class="page">
-    <h1>MsgBoard</h1>
-    {{> login}}
-    {{> messages}}
-    <footer>
-      <p>Written using <a href="http://meteor.com">Meteor</a>, <a href="https://bitbucket.org/benallard/msgboard-meteor">last source</a></p>
-    </footer>
+  <h1>MsgBoard</h1>
+  {{> login}}
+  {{> new}}
+  <div class="content">
+  <div id="block">
+    {{> news}}
   </div>
+  <div id="block">
+    {{> users}}
+  </div>
+  <div id="block">
+    {{> mines}}
+  </div>
+  </div>
+  <footer>
+    <p>Written using <a href="http://meteor.com">Meteor</a>, <a href="https://bitbucket.org/benallard/msgboard-meteor">last source</a></p>
+  </footer>
 </body>
 
 <template name="login">
   <div>
     {{#if user}}
-      <a id="logout" href=''>logout</a>
+      <a id="logout" href='javascript:void(0)'>logout</a>
       <h2>{{ user }}</h2>
     {{else}}
       <input type="text" id="username" placeholder="Your name" />
   </div>
 </template>
 
-<template name="messages">
+<template name="new">
+  <div>
+  {{#if show}}
+    {{#if add_msg}}
+    {{#if warning}}
+    <p>Warning, you already have {{ nummsg }} active messages. Only the three
+most recent one will be shown to other people. Consider archiving
+some of them.</p>
+    {{/if}}
+    <fieldset>
+      <legend>Type your message</legend>
+      <textarea placeholder="You message here ..." name="content" rows=10
+      id="content"></textarea>
+      <a id="submit" href='javascript:void(0)'>Submit</a>
+      <a id="cancel" href='javascript:void(0)'>Cancel</a>
+    </fieldset>
+    {{else}}
+    <a id='new' href='javascript:void(0)'>New Message</a>
+    {{/if}}
+  {{/if}}
+  </div>
+</template>
+
+<template name="news">
+  {{#if show}}
+  <article>
+    <h3>New messages:</h3>
+    {{#each news}}
+      {{> message}}
+    {{/each}}
+  </article>
+  <article>
+    <h3>Modified messages:</h3>
+    {{#each mods}}
+      {{> message}}
+    {{/each}}
+  </article>
+  {{else}}
+    <p>Would you logon, would you have an history there</p>
+  {{/if}}
+</template>
+
+<template name="users">
+  <nav><ul>
+  {{#each users}}
+    <a href='#user{{name}}'>{{name}}</a>
+  {{/each}}
+  </ul></nav>
+  {{#each users}}
+    {{> user}}
+  {{/each}}
+</template>
+
+<template name="user">
+  <h3 id="user{{name}}">{{ name }}</h3>
+  {{#each messages}}
+    {{> message}}
+  {{/each}}
+</template>
+
+<template name="mines">
+  <h3>My messages:</h3>
   {{#each messages}}
     {{> message}}
   {{/each}}
 </template>
 
 <template name="message">
-  <div class="message">
-    <h3>{{ title }}</h3>
-  </div>
+  {{# if show}}
+  <section class="message{{#if archived}} archived{{/if}}" id="{{_id}}">
+    <p class=info>Posted&nbsp;
+    {{#if printauthor }}by {{ user }} {{/if}}
+    {{ ago created_on }}
+    {{#if edited }}
+      <span title="last modified&nbsp;{{ ago last_modified_on }}">*</span>
+    {{/if}}
+    </p>
+    {{ content }}
+    {{#if loggedonuser }}
+    <nav><ul>
+      {{#if archived }}
+        <li><a id="unarchive" href="javascript:void(0)">Un-Archive</a></li>
+      {{else}}
+        <li><a id="edit" href="javascript:void(0)">Edit</a></li>
+        <li><a id="archive" href="javascript:void(0)">Archive</a></li>
+      {{/if}}
+    </ul></nav>
+    {{/if}}
+  </section>
+  {{/if}}
 </template>
   color: black;
 }
 
+#block {
+  float: left;
+  width: 620px;
+}
+
+
 a {
   color: #004b6b;
 }
 
+nav ul {
+  list-style: none;
+  margin: 0px 5px 0px 0px;
+  li {
+    display: inline;
+  }
+}
+
 .page {
   width: 540px;
   margin: 40px auto;
   font-size: 70%;
 }
 
+textarea {width: 100%}
+
+
+.message {
+
+  border: 3px solid #DDD;
+  border-radius: 10px;
+  margin-left: 30px;
+  padding: 5px;
+
+  .info {
+    margin-top: 0;
+    margin-bottom: 0;
+    color: #888;
+    font-size: 70%;
+    float: right;
+  }
+
+  nav{
+    border-top: solid 1px #BABDB6;
+    text-align: right;
+    clear: both;
+  }
+
+  .document{
+    margin: 1em;
+  }
+
+  &.archived{
+    color: #999;
+  }
+}
+
+
 footer {
-  font-size: 70%
+  font-size: 70%;
+  clear: both;
 }