|
@@ -1,225 +0,0 @@
|
|
|
-
|
|
|
-
|
|
|
-this.Documents = new Mongo.Collection("documents");
|
|
|
-
|
|
|
-EditingUsers = new Mongo.Collection("editingUsers");
|
|
|
-
|
|
|
-if (Meteor.isClient) {
|
|
|
- Meteor.subscribe("documents");
|
|
|
- Meteor.subscribe("editingUsers");
|
|
|
-
|
|
|
- Template.editor.helpers({
|
|
|
-
|
|
|
- docid: function () {
|
|
|
- setupCurrentDocument();
|
|
|
- return Session.get("docid");
|
|
|
- },
|
|
|
-
|
|
|
- config: function () {
|
|
|
- return function (editor) {
|
|
|
- editor.setOption("lineNumbers", true);
|
|
|
- editor.setOption("theme", "cobalt");
|
|
|
- editor.setOption("mode", "html");
|
|
|
-
|
|
|
-
|
|
|
- editor.on("change", function (cmEditor /* , info */) {
|
|
|
- let $preview = $("#viewer_iframe");
|
|
|
-
|
|
|
- $preview.contents().find("html").html(cmEditor.getValue());
|
|
|
- Meteor.call("addEditingUser");
|
|
|
- });
|
|
|
- };
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- Template.editingUsers.helpers({
|
|
|
-
|
|
|
- users: function () {
|
|
|
- let doc = Documents.findOne();
|
|
|
- if (!doc) {
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
- let eUsers = EditingUsers.findOne({ docId: doc._id });
|
|
|
- if (!eUsers) {
|
|
|
-
|
|
|
- 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);
|
|
|
-
|
|
|
- doc = Documents.findOne({
|
|
|
- _id: Session.get("docid"),
|
|
|
- owner: Meteor.userId()
|
|
|
- });
|
|
|
- return !!doc;
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- Template.navbar.events({
|
|
|
- "click .js-add-doc": function (event) {
|
|
|
- event.preventDefault();
|
|
|
-
|
|
|
- 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) {
|
|
|
-
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
-}
|
|
|
-
|
|
|
-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 () {
|
|
|
-
|
|
|
- if (!Documents.findOne()) {
|
|
|
- Documents.insert({ title: "my new document" });
|
|
|
- }
|
|
|
- });
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-Meteor.methods({
|
|
|
- addDoc: function () {
|
|
|
- Meteor._debug("addDoc, this", this);
|
|
|
-
|
|
|
- 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) {
|
|
|
-
|
|
|
- return;
|
|
|
- }
|
|
|
- if (!this.userId) {
|
|
|
-
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- let user = Meteor.user().profile;
|
|
|
- let eUsers = EditingUsers.findOne({ docId: doc._id });
|
|
|
-
|
|
|
- if (!eUsers) {
|
|
|
- eUsers = {
|
|
|
- docId: doc._id,
|
|
|
- users: {}
|
|
|
- };
|
|
|
- }
|
|
|
- user.lastEdit = new Date();
|
|
|
- eUsers.users[this.userId] = user;
|
|
|
-
|
|
|
- EditingUsers.upsert({ _id: eUsers._id }, eUsers);
|
|
|
- }
|
|
|
-});
|
|
|
-
|
|
|
-function setupCurrentDocument() {
|
|
|
- let doc;
|
|
|
-
|
|
|
- if (!Session.get("docid")) {
|
|
|
- doc = Documents.findOne();
|
|
|
- if (doc) {
|
|
|
- Session.set("docid", doc._id);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-function fixObjectsKeys(obj) {
|
|
|
- let newObj = {};
|
|
|
- for (let key in obj) {
|
|
|
- if (obj.hasOwnProperty(key)) {
|
|
|
- const key2 = key.replace("-", "");
|
|
|
- newObj[key2] = obj[key];
|
|
|
- }
|
|
|
- }
|
|
|
- return newObj;
|
|
|
-}
|