-
Notifications
You must be signed in to change notification settings - Fork 4
/
editable-text-demo.js
175 lines (160 loc) · 6.02 KB
/
editable-text-demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
Posts = new Mongo.Collection('posts');
Comments = new Mongo.Collection('comments');
Schemas = {};
Schemas.Post = new SimpleSchema({
title: {
type: String,
label: "Title",
optional:true,
max: 200
},
body: {
type: String,
label: "Body",
optional:true
},
timestamp: {
type: Number,
label: "Timestamp"
},
user: {
type: String,
label: "User",
optional: true
},
tags: {
type: [String],
label: "Tags",
optional: true
}
});
Schemas.Comment = new SimpleSchema({
text: {
type: String,
label: "text",
optional: true
},
post_id: {
type: String,
label: "post_id"
},
timestamp: {
type: Number,
label: "Timestamp"
},
user: {
type: String,
label: "User",
optional: true
}
});
Schemas.User = new SimpleSchema({
emails: {
type: [Object],
optional: true
},
"emails.$.address": {
type: String,
regEx: SimpleSchema.RegEx.Email
},
"emails.$.verified": {
type: Boolean
},
createdAt: {
type: Date
},
services: {
type: Object,
optional: true,
blackbox: true
},
profile: {
type: Object,
optional: true,
blackbox: true
}
});
Posts.attachSchema(Schemas.Post);
Comments.attachSchema(Schemas.Comment);
Meteor.users.attachSchema(Schemas.User);
// Config for editable text widget
EditableText.useTransactions = true;
EditableText.maximumImageSize = 200000;
EditableText.registerCallbacks({
addTimestampToDoc : function (doc) {
var extraFields = {timestamp: Date.now()};
if (Meteor.user()) {
extraFields.user = Meteor.user().emails[0].address;
}
return _.extend(doc, extraFields);
},
callMethod : function (doc, Collection, newValue) {
if (Meteor.isClient) {
Meteor.call("testMethod", doc);
}
return newValue;
}
});
Meteor.methods({
"testMethod" : function (doc) {
// console.log("Method doc: ", doc);
}
});
// Config for transactions
tx.requireUser = false; // Means a user who is not logged in gets to undo/redo
if (Meteor.isClient) {
tx.undoRedoButtonClass = 'btn btn-default undo-redo';
Session.setDefault('editor', 'default');
EditableText.froalaDefaultOptions = {enter: $.FroalaEditor.ENTER_DIV};
Template.posts.helpers({
posts: function() {
return Posts.find({}, {sort: {timestamp: -1}});
},
comments: function() {
return Comments.find({post_id: this._id}, {sort: {timestamp: 1}});
},
newCommentDoc: function() {
return {};
},
timestamp: function() {
var time = (new Date(this.timestamp)).toDateString();
return time.substr(0, time.length - 4);
},
postOptions : function() {
return {
collection: "posts",
field: "title",
removeEmpty: true,
acceptEmpty: true,
placeholder: "Post title",
substitute: '<i class="fa fa-pencil"></i>'
}
},
selected : function (type) {
return Session.equals('editor', type);
}
});
Template.posts.events({
'click button.froala, click button.default' : function () {
Session.set('editor', (Session.equals('editor', 'default')) ? 'froala' : 'default');
}
});
}
if (Meteor.isServer) {
var destroy = function() {
Posts.remove({});
Comments.remove({});
tx.Transactions.remove({});
Posts.insert({_id: "abc123", timestamp: Date.now(), title: "Editable post title - delete this title to remove the post", body: '<div>This is the body of the post, written with the <strong>wysiwyg editor</strong>. It is editable because we wrote {{> editableText collection="posts" field="body" wysiwyg=true}} in the template instead of {{body}}.<br></div><div><br></div><div>This demo app was written with meteor packages (<a href="https://github.com/aldeed/meteor-collection2" target="_blank">aldeed:collection2</a>, <a href="https://github.com/JackAdams/meteor-transactions" target="_blank">babrahams:transactions</a>, <a href="https://github.com/ianmartorell/meteor-accounts-ui-bootstrap-3" target="_blank">ian:accounts-ui-bootstrap-3</a>, <a href="https://github.com/meteorhacks/fast-render/" target="_blank">meteorhacks:fast-render</a>, <a href="https://github.com/JackAdams/meteor-editable-text-wysiwyg-bootstrap-3" target="_blank">babrahams:editable-text-wysiwyg-bootstrap-3</a>, <a href="https://github.com/JackAdams/meteor-editable-list" target="_blank">babrahams:editable-list</a>) and a minimal amount of code (<a href="https://github.com/JackAdams/editable-text-demo/blob/master/editable-text-demo.html" target="_blank">html</a>, <a href="https://github.com/JackAdams/editable-text-demo/blob/master/editable-text-demo.js" target="_blank">js</a>, <a href="https://github.com/JackAdams/editable-text-demo/blob/master/editable-text-demo.css" target="_blank">css</a>). It demonstrates some of the uses of the <a href="https://github.com/JackAdams/meteor-editable-text">babrahams:editable-text</a> package.</div><div><br></div><div>See the source at <a href="https://github.com/JackAdams/editable-text-demo">https://github.com/JackAdams/editable-text-demo.</a></div>', tags: ['Drag to reorder', 'Click to edit']});
Comments.insert({post_id: "abc123", timestamp:Date.now(), text: "To remove a comment, delete the text and press 'Enter'. This is possible because we wrote {{> editableText collection=\"comments\" field=\"text\" textarea=true removeEmpty=true}} instead of {{text}} in the template.", user: "[email protected]"});
Comments.insert({post_id: "abc123", timestamp:Date.now(), text: "Sign in with - email: [email protected], password: password - for email addresses in posts and comments."});
Comments.insert({post_id: "abc123", timestamp:Date.now(), text: "All posts will self destruct every 15 minutes."});
if (!Meteor.users.findOne({emails: {$elemMatch: {address: "[email protected]"}}})) {
Accounts.createUser({username: "demo-user", email: "[email protected]", password: "password"});
}
Meteor.setTimeout(function() {
destroy();
}, 15 * 60 * 1000);
}
destroy();
}