123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- // 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");
- Meteor.subscribe("comments");
- Router.configure({
- layoutTemplate: "ApplicationLayout"
- });
- Router.route("/", function () {
- Meteor._debug("Routing /");
- this.render("navbar", { to: "header" });
- this.render("docList", { to: "main" });
- });
- Router.route("/documents/:_id", function () {
- Meteor._debug("Routing /documents/" + this.params._id);
- Session.set("docid", this.params._id);
- this.render("navbar", { to: "header" });
- this.render("docItem", { to: "main" });
- });
- 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", Session.get("docid"));
- });
- };
- }
- });
- Template.editingUsers.helpers({
- // Retrieve a list of users that are editing this document.
- users: function () {
- let doc = Documents.findOne({ _id: Session.get("docid") });
- 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.docList.helpers({
- // Retrieve a list of documents.
- documents: function () {
- return Documents.find();
- }
- });
- Template.insertCommentForm.helpers({
- docid: function () {
- return Session.get("docid");
- }
- });
- Template.commentList.helpers({
- comments: function () {
- return Comments.find({ docid: Session.get("docid") });
- }
- });
- 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;
- }
|