فهرست منبع

W4.1: restructured to folders.

Frederic G. MARAND 9 سال پیش
والد
کامیت
0489c353c1
9فایلهای تغییر یافته به همراه253 افزوده شده و 226 حذف شده
  1. 154 0
      client/main.js
  2. 0 0
      client/textcircle.css
  3. 1 1
      client/textcircle.html
  4. 0 0
      client/textcircle.scss
  5. 0 0
      client/theme/cobalt.css
  6. 6 0
      lib/collections.js
  7. 30 0
      server/main.js
  8. 62 0
      shared/main.js
  9. 0 225
      textcircle.js

+ 154 - 0
client/main.js

@@ -0,0 +1,154 @@
+// code that is only sent to the client
+// main.js is loaded last:
+// http://docs.meteor.com/#/full/structuringyourapp
+
+// subscribe to read data
+Meteor.subscribe("documents");
+Meteor.subscribe("editingUsers");
+
+Template.editor.helpers({
+  // Return the id of the first document you can find.
+  docid: function () {
+    setupCurrentDocument();
+    return Session.get("docid");
+  },
+  // Configure the CodeMirror editor.
+  config: function () {
+    return function (editor) {
+      editor.setOption("lineNumbers", true);
+      editor.setOption("theme", "cobalt");
+      editor.setOption("mode", "html");
+      // Set a callback that gets triggered whenever the user
+      // makes a change in the code editing window.
+      editor.on("change", function (cmEditor /* , info */) {
+        let $preview = $("#viewer_iframe");
+        // Send the current code over to the iframe for rendering.
+        $preview.contents().find("html").html(cmEditor.getValue());
+        Meteor.call("addEditingUser");
+      });
+    };
+  }
+});
+
+Template.editingUsers.helpers({
+  // Retrieve a list of users that are editing this document.
+  users: function () {
+    let doc = Documents.findOne();
+    if (!doc) {
+      // Give up.
+      return null;
+    }
+    let eUsers = EditingUsers.findOne({ docId: doc._id });
+    if (!eUsers) {
+      // Give up.
+      return null;
+    }
+    let users = new Array();
+    let i = 0;
+    for (let userId in eUsers.users) {
+      users[i] = fixObjectsKeys(eUsers.users[userId]);
+      users[i]._id = userId;
+      i++;
+    }
+    return users;
+  }
+});
+
+Template.navbar.helpers({
+  // Retrieve a list of documents.
+  documents: function () {
+    return Documents.find();
+  }
+});
+
+Template.docMeta.helpers({
+  // Find current document.
+  document: function () {
+    return Documents.findOne({ _id: Session.get("docid") });
+  },
+  // Test if a user is allowed to edit current doc.
+  canEdit: function () {
+    let doc = Documents.findOne({ _id: Session.get("docid") });
+    return doc && doc.owner === Meteor.userId();
+  },
+  isPrivate: function () {
+    let doc = Documents.findOne({ _id: Session.get("docid") });
+    return !!(doc.isPrivate);
+  }
+});
+
+Template.editableText.helpers({
+  // Test if a user is allowed to edit current doc.
+  userCanEdit: function (doc, collection) {
+    Meteor._debug("userCanEdit", doc, collection);
+    // Can edit if the document is owned by me.
+    doc = Documents.findOne({
+      _id: Session.get("docid"),
+      owner: Meteor.userId()
+    });
+    return !!doc;
+  }
+});
+
+Template.navbar.events({
+  // Add doc button.
+  "click .js-add-doc": function (event) {
+    event.preventDefault();
+    // User not available.
+    if (!Meteor.user()) {
+      alert("You need to login first!");
+    }
+    else {
+      // They are logged in... lets insert a doc.
+      Meteor.call("addDoc", function (err, res) {
+        if (!err) {
+          console.log("addDoc res", res);
+          Session.set("docid", res);
+        }
+      });
+    }
+  },
+
+  // Load doc button.
+  "click .js-load-doc": function (event) {
+    // This contains a complete document.
+    Session.set("docid", this._id);
+  }
+});
+
+Template.docMeta.events({
+  // Change document privacy.
+  "click .js-tog-private": function (event) {
+    // They are logged in... lets insert a doc.
+    const docid = Session.get("docid");
+    if (docid) {
+      const doc = { _id: docid, isPrivate: event.target.checked };
+      Meteor.call("updateDocPrivacy", doc);
+    }
+  }
+});
+
+// Helper to make sure a doc is available.
+function setupCurrentDocument() {
+  let doc;
+  // No docid set yet.
+  if (!Session.get("docid")) {
+    doc = Documents.findOne();
+    if (doc) {
+      Session.set("docid", doc._id);
+    }
+  }
+}
+
+// This renames object keys by removing hyphens to make them compatible
+// with spacebars.
+function fixObjectsKeys(obj) {
+  let newObj = {};
+  for (let key in obj) {
+    if (obj.hasOwnProperty(key)) {
+      const key2 = key.replace("-", "");
+      newObj[key2] = obj[key];
+    }
+  }
+  return newObj;
+}

+ 0 - 0
textcircle.css → client/textcircle.css


+ 1 - 1
textcircle.html → client/textcircle.html

@@ -60,7 +60,7 @@
 
 <template name="editor">
   <h2>doc id: {{ docid }}</h2>
-  {{> sharejsCM docid=docid onRender=config id="editor" }}
+  {{> sharejsCM docid=docid onRender=config id="editor" mode="javascript" }}
 </template>
 
 <template name="viewer">

+ 0 - 0
textcircle.scss → client/textcircle.scss


+ 0 - 0
client/cobalt.css → client/theme/cobalt.css


+ 6 - 0
lib/collections.js

@@ -0,0 +1,6 @@
+// This collection stores all the documents.
+// Note: not Documents, but this.Documents, because of the editing package (?)
+this.Documents = new Mongo.Collection("documents");
+
+// This collection stores sets of users that are editing documents.
+EditingUsers = new Mongo.Collection("editingUsers");

+ 30 - 0
server/main.js

@@ -0,0 +1,30 @@
+// Code that is only sent to the server.
+
+// main.js is loaded last:
+// http://docs.meteor.com/#/full/structuringyourapp
+
+Meteor.startup(function () {
+  // Insert a document if there isn't one already.
+  if (!Documents.findOne()) {
+    Documents.insert({ title: "my new document" });
+  }
+});
+
+// Publish read access to collections.
+
+// All visible docs.
+Meteor.publish("documents", function () {
+  Meteor._debug("Publishing documents");
+  return Documents.find({
+    $or: [
+      { isPrivate: { $ne: true } }, 
+      { owner: this.userId }
+    ]
+  });
+});
+
+// Users editing docs.
+Meteor.publish("editingUsers", function () {
+  Meteor._debug("Publishing all editingUsers");
+  return EditingUsers.find();
+});

+ 62 - 0
shared/main.js

@@ -0,0 +1,62 @@
+// Code that is shared between client and server, i.e. sent to both.
+
+// main.js is loaded last:
+// http://docs.meteor.com/#/full/structuringyourapp
+
+// Methods that provide write access to the data.
+Meteor.methods({
+  addDoc: function () {
+    Meteor._debug("addDoc, this", this);
+    // Not logged-in.
+    if (!this.userId) {
+      return;
+    }
+    else {
+      let doc = {
+        owner: this.userId,
+        createdOn: new Date(),
+        title: "my new doc"
+      };
+      const docid = Documents.insert(doc);
+      return docid;
+    }
+  },
+
+  // Changing doc privacy settings.
+  updateDocPrivacy: function (doc) {
+    Meteor._debug("updatedocPrivacy", this.userId, doc)
+    if (!this.userId) {
+      return;
+    }
+    let res = Documents.update({ _id: doc._id, owner: this.userId }, { $set: { isPrivate: doc.isPrivate } });
+    Meteor._debug("update", res);
+  },
+
+  // Adding editors to a document.
+  addEditingUser: function () {
+    let doc = Documents.findOne();
+    if (!doc) {
+      // No doc: give up.
+      return;
+    }
+    if (!this.userId) {
+      // No logged-in user: give up.
+      return;
+    }
+    // Now I have a doc and possibly a user.
+    let user = Meteor.user().profile;
+    let eUsers = EditingUsers.findOne({ docId: doc._id });
+    // No editing users have been stored yet.
+    if (!eUsers) {
+      eUsers = {
+        docId: doc._id,
+        users: {}
+      };
+    }
+    user.lastEdit = new Date();
+    eUsers.users[this.userId] = user;
+
+    // Upsert: insert or update if filter matches.
+    EditingUsers.upsert({ _id: eUsers._id }, eUsers);
+  }
+});

+ 0 - 225
textcircle.js

@@ -1,225 +0,0 @@
-// This collection stores all the documents.
-// Note: not Documents, but this.Documents, because of the editing package (?)
-this.Documents = new Mongo.Collection("documents");
-// This collection stores sets of users that are editing documents.
-EditingUsers = new Mongo.Collection("editingUsers");
-
-if (Meteor.isClient) {
-  Meteor.subscribe("documents");
-  Meteor.subscribe("editingUsers");
-
-  Template.editor.helpers({
-    // Return the id of the first document you can find.
-    docid: function () {
-      setupCurrentDocument();
-      return Session.get("docid");
-    },
-    // Configure the CodeMirror editor.
-    config: function () {
-      return function (editor) {
-        editor.setOption("lineNumbers", true);
-        editor.setOption("theme", "cobalt");
-        editor.setOption("mode", "html");
-        // Set a callback that gets triggered whenever the user
-        // makes a change in the code editing window.
-        editor.on("change", function (cmEditor /* , info */) {
-          let $preview = $("#viewer_iframe");
-          // Send the current code over to the iframe for rendering.
-          $preview.contents().find("html").html(cmEditor.getValue());
-          Meteor.call("addEditingUser");
-        });
-      };
-    }
-  });
-
-  Template.editingUsers.helpers({
-    // Retrieve a set of users that are editing this document.
-    users: function () {
-      let doc = Documents.findOne();
-      if (!doc) {
-        // Give up.
-        return null;
-      }
-      let eUsers = EditingUsers.findOne({ docId: doc._id });
-      if (!eUsers) {
-        // Give up.
-        return null;
-      }
-      let users = new Array();
-      let i = 0;
-      for (let userId in eUsers.users) {
-        users[i] = fixObjectsKeys(eUsers.users[userId]);
-        users[i]._id = userId;
-        i++;
-      }
-      return users;
-    }
-  });
-
-  Template.navbar.helpers({
-    documents: function () {
-      return Documents.find();
-    }
-  });
-
-  Template.docMeta.helpers({
-    document: function () {
-      return Documents.findOne({ _id: Session.get("docid") });
-    },
-    canEdit: function () {
-      let doc = Documents.findOne({ _id: Session.get("docid") });
-      return doc && doc.owner === Meteor.userId();
-    },
-    isPrivate: function () {
-      let doc = Documents.findOne({ _id: Session.get("docid") });
-      return !!(doc.isPrivate);
-    }
-  });
-
-  Template.editableText.helpers({
-    userCanEdit: function (doc, collection) {
-      Meteor._debug("userCanEdit", doc, collection);
-      // Can edit if the document is owned by me.
-      doc = Documents.findOne({
-        _id: Session.get("docid"),
-        owner: Meteor.userId()
-      });
-      return !!doc;
-    }
-  });
-
-  Template.navbar.events({
-    "click .js-add-doc": function (event) {
-      event.preventDefault();
-      // User not available.
-      if (!Meteor.user()) {
-        alert("You need to login first.");
-      }
-      else {
-        Meteor.call("addDoc", function (err, res) {
-          if (!err) {
-            console.log("addDoc res", res);
-            Session.set("docid", res);
-          }
-        });
-      }
-    },
-
-    "click .js-load-doc": function (event) {
-      // This contains a complete document.
-      Session.set("docid", this._id);
-    }
-  });
-
-  Template.docMeta.events({
-    "click .js-tog-private": function (event) {
-      const docid = Session.get("docid");
-      if (docid) {
-        const doc = { _id: docid, isPrivate: event.target.checked };
-        Meteor.call("updateDocPrivacy", doc);
-      }
-    }
-  });
-} // end isClient...
-
-if (Meteor.isServer) {
-  Meteor.publish("documents", function () {
-    Meteor._debug("Publishing documents");
-    return Documents.find({
-      $or: [
-        { isPrivate: false },
-        { owner: this.userId }
-      ]
-    });
-  });
-  Meteor.publish("editingUsers", function () {
-    Meteor._debug("Publishing all editingUsers");
-    return EditingUsers.find();
-  });
-
-  Meteor.startup(function () {
-    // Insert a document if there isn't one already.
-    if (!Documents.findOne()) {
-      Documents.insert({ title: "my new document" });
-    }
-  });
-}
-
-// Methods that provide write access to the data.
-Meteor.methods({
-  addDoc: function () {
-    Meteor._debug("addDoc, this", this);
-    // Not logged-in.
-    if (!this.userId) {
-      return;
-    }
-    else {
-      let doc = {
-        owner: this.userId,
-        createdOn: new Date(),
-        title: "my new doc"
-      };
-      const docid = Documents.insert(doc);
-      return docid;
-    }
-  },
-
-  updateDocPrivacy: function (doc) {
-    Meteor._debug("updatedocPrivacy", this.userId, doc)
-    if (!this.userId) {
-      return;
-    }
-    let res = Documents.update({ _id: doc._id, owner: this.userId }, { $set: { isPrivate: doc.isPrivate } });
-    Meteor._debug("update", res);
-  },
-
-  addEditingUser: function () {
-    let doc = Documents.findOne();
-    if (!doc) {
-      // No doc: give up.
-      return;
-    }
-    if (!this.userId) {
-      // No logged-in user: give up.
-      return;
-    }
-    // Now I have a doc and possibly a user.
-    let user = Meteor.user().profile;
-    let eUsers = EditingUsers.findOne({ docId: doc._id });
-    // No editing users have been stored yet.
-    if (!eUsers) {
-      eUsers = {
-        docId: doc._id,
-        users: {}
-      };
-    }
-    user.lastEdit = new Date();
-    eUsers.users[this.userId] = user;
-    // Upsert: insert or update if filter matches.
-    EditingUsers.upsert({ _id: eUsers._id }, eUsers);
-  }
-});
-
-function setupCurrentDocument() {
-  let doc;
-  // No docid set yet.
-  if (!Session.get("docid")) {
-    doc = Documents.findOne();
-    if (doc) {
-      Session.set("docid", doc._id);
-    }
-  }
-}
-
-// This renames object keys by removing hyphens to make them compatible
-// with spacebars.
-function fixObjectsKeys(obj) {
-  let newObj = {};
-  for (let key in obj) {
-    if (obj.hasOwnProperty(key)) {
-      const key2 = key.replace("-", "");
-      newObj[key2] = obj[key];
-    }
-  }
-  return newObj;
-}