/
commands.txt
313 lines (265 loc) · 6.19 KB
/
commands.txt
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# Starting with meteor
curl https://install.meteor.com | /bin/sh
npm install -g meteorite
# Generate app
mrt create whattheslide
cd whattheslide
mkdir -p client/compatibility public private server lib tests
mrt
mv *.css client
mv whattheslide.js lib/application.js
// client/*.css
/* CSS declarations go here */
* {
box-sizing: border-box;
}
h1 {
color: #333;
}
# Create Slide collections/models
// lib/slides.js
Slides = new Meteor.Collection("slides");
// console
Slides.insert({order: 1, content: "# Meteor.js Intro"})
# Displaying slides
// client/slides.html
<template name="slides">
<div class="slides">
{{#each slides}}
{{> slide}}
{{/each}}
</div>
</template>
<template name="slide">
<header>{{title}}</header>
<div class="slide">
<section class="middle">
{{{content}}}
</section>
</div>
</template>
// client/slides.js
Template.slides.slides = function () {
return Slides.find({}, {sort: {order: 1}});
};
Template.slide.title = function () {
return this.title || "Slide " + this.order;
};
// client/*.css
section.middle {
line-height: 1.8;
text-align: center;
display: table-cell;
vertical-align: middle;
height: 350px;
width: 450px;
}
.slide {
position: relative;
overflow: hidden;
width: 450px;
height: 350px;
border: 1px solid black;
background-color: #333;
padding: 0 20px;
}
.slide, .slide h1, .slide h2, .slide h3, .slide h4 {
color: #f8f8f2;
}
// whattheslide.html
<template name="hello">
<h1>Welcome to What The Slide!</h1>
<h2>Current Slides</h2>
{{> slides}}
</template>
// client/slides.html
<template name="new_slide">
<form id="new_slide">
<label>Title (optional)</label>
<input type="text" name="title" />
<br />
<label>Order (optional)</label>
<input type="number" name="order" value="{{currentOrder}}" />
<br />
<label>Content</label>
<textarea name="content" />
<br />
<input type="submit" />
</form>
</template>
Template.new_slide.events = {
'submit': function (event) {
event.preventDefault();
var form={};
$.each($(event.currentTarget).serializeArray(), function() {
form[this.name] = this.value;
});
form['order'] = parseInt(form['order'], 10);
Slides.insert(form);
}
};
Template.new_slide.currentOrder = function () {
return Slides.find({}, {sort: {order: 1}}).count() + 1;
};
// *.html
<template name="hello">
<h1>Welcome to What The Slide!</h1>
<h2>Current Slides</h2>
{{> slides}}
<h2>Add new slide</h2>
{{> new_slide}}
</template>
mrt add marked
// client/slides.html
<template name="slide">
<header>{{title}}</header>
<a href="" class="delete">delete</a>
<div class="slide">
<section class="middle">
{{#markdown}}{{{content}}}{{/markdown}}
</section>
</div>
</template>
// client/slides.js
Template.slide.events = {
'click .delete': function (event) {
event.preventDefault();
Slides.remove({_id: this._id});
}
};
mrt add router
// *.html
{{renderPage}}
// client/slides.js
Template.new_slide.events = {
'submit': function (event) {
event.preventDefault();
var form={};
$.each($(event.currentTarget).serializeArray(), function() {
form[this.name] = this.value;
});
form['order'] = parseInt(form['order'] || 1, 10);
Slides.insert(form);
Meteor.Router.to('/');
}
};
Meteor.Router.add({
'': 'hello',
'/slides/new': 'new_slide'
});
// *.html
<template name="hello">
<h1>Welcome to What The Slide!</h1>
<!-- <h2>Current Slides <a href="/presentation">Show Presentation</a></h2> -->
{{> slides}}
<h2>Add new slide</h2>
<a href="/slides/new">New slide</a>
</template>
# Presentation mode
// *.html
<template name="presentation">
<div class="presentation">
{{> slides}}
</div>
</template>
Template.slide.current = function () {
currentId = (Session.get('selectedSlide') || Slides.findOne({})._id);
return (currentId === this._id) ? "current" : undefined;
};
// client/slides.html
<template name="slide">
<header>{{title}}</header>
<a href="" class="delete">delete</a>
<div class="slide {{current}}">
<section class="middle">
{{#markdown}}{{{content}}}{{/markdown}}
</section>
</div>
</template>
// client/slides.js
Meteor.Router.add({
'': 'hello',
'/slides/new': 'new_slide',
'/presentation': 'presentation'
});
// client/*.css
.presentation .slides {
width: 100%;
height: 100%;
left: 0;
top: 0;
position: absolute;
}
.presentation section.middle {
height: 700px;
width: 900px;
}
.presentation .slide {
font-size: 2em;
display: none;
position: absolute;
left: 50%;
top: 50%;
margin-top: -350px;
width: 900px;
height: 700px;
}
.presentation .slide.current {
visibility: visible;
display: block;
margin-left: -450px;
}
.presentation header, .presentation .delete {
display: none;
}
# Events
// client/slides.js
prevId = function(arrayCursor, id) {
array = arrayCursor.map(function(item){
return item._id;
});
if (!id) return array[0];
index = array.indexOf(id);
return array[Math.max(index - 1, 0)];
};
nextId = function (arrayCursor, id) {
array = arrayCursor.map(function(item){
return item._id;
});
if (!id) return array[array.length - 1];
index = array.indexOf(id);
return array[Math.min(index + 1, array.length - 1)];
};
Template.presentation.created = function (event) {
if (!this.keydown) {
Slides.find({}, {sort: {order: 1}}).observe({
added: function (item) {
Session.set('selectedSlide', Slides.findOne({}, {sort: {order: 1}})._id);
}
});
this.keydown = $(document).on('keydown', function(event) {
switch (event.which) {
case 37:
// back
Session.set('selectedSlide', prevId(Slides.find({}, {sort: {order: 1}}), Session.get('selectedSlide')));
break;
case 39:
// next
Session.set('selectedSlide', nextId(Slides.find({}, {sort: {order: 1}}), Session.get('selectedSlide')));
break;
}
});
}
};
Template.presentation.destroyed = function (event) {
$(document).off('keydown', this.keydown);
};
Moving to publish
mrt remove autopublish
// server/slides.js
Meteor.publish("slides", function (){
return Slides.find({}, {sort: {order: 1}});
});
// client/slides.js
Meteor.subscribe("slides");
https://github.com/oortcloud/unofficial-meteor-faq