forked from segment-boneyard/socrates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-model.js
196 lines (149 loc) · 5.58 KB
/
app-model.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*global Backbone Socrates _ */
var ID_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
var ID_LENGTH = 7;
Socrates.Model = Backbone.Model.extend({
defaults : {
document : null,
documents : null,
state : null
},
bookmarkKey : 'socrates.bookmarks',
initialize : function (attributes, options) {
_.bindAll(this);
this.initializeRouter();
this.initializeDocuments();
this.on('change:document', this.onDocumentOrStateChange)
.on('change:state', this.onDocumentOrStateChange);
},
initializeRouter : function () {
this.router = new Backbone.Router({
routes : {
'' : 'home',
':id(/:state)' : 'document'
}
})
.on('route:home', this.onHomeRoute)
.on('route:document', this.onDocumentRoute);
},
initializeDocuments : function () {
var documents = new Socrates.DocumentCollection()
.on('add remove', this.writeBookmarks)
.on('remove', this.onDocumentRemove);
this.set('documents', documents, {silent:true});
_.each(this.readBookmarks(), this.fetchDocument);
},
// Actions
// -------
fetchDocument : function (id, body) {
if (!_.isString(body)) body = '';
var document = new Socrates.DocumentModel({
id : id,
body : body
});
this.get('documents').add(document);
window.analytics.track('Fetch Document', {
id : id,
title : document.get('title')
});
return document;
},
createDocument : function (body) {
window.analytics.track('Create New Document');
return this.fetchDocument(this.generateDocumentId(), body);
},
readBookmarks : function () {
var bookmarkString = localStorage.getItem(this.bookmarkKey);
return bookmarkString ? bookmarkString.split(',') : [];
},
writeBookmarks : function () {
var ids = this.get('documents').map(function (document) {
return document.id;
});
localStorage.setItem(this.bookmarkKey, ids.join(','));
},
// Route Handlers
// --------------
onHomeRoute : function () {
var body = onboarding;
if (localStorage.getItem(this.bookmarkKey)) {
var index = _.random(0, random.length-1);
body = random[index];
}
this.set('document', this.createDocument(body));
window.analytics.track('Visit Home Page');
},
onDocumentRoute : function (id, state) {
var document = this.get('documents').find(function (document) {
return id === document.id;
});
document || (document = this.fetchDocument(id));
this.set('document', document);
if (state === 'read' || state ==='write') this.set('state', state);
window.analytics.track('Visit Document Page', {
id : id,
state : state
});
},
// Event Handlers
// --------------
onDocumentOrStateChange : function (model) {
if (!this.has('document')) return;
var document = this.get('document');
var state = this.get('state');
var fragment = document.id;
if (state) fragment += '/'+state;
this.router.navigate(fragment);
},
onDocumentRemove : function (removedDocument) {
var openDocument = this.get('document');
// If the open document wasn't removed, don't do anything fancy.
if (openDocument.id !== removedDocument.id) return;
// Otherwise, we need to promote another document. Try to take the last
// of the documents, otherwise make a fresh one.
var document = this.get('documents').last() || this.createDocument();
this.set('document', document);
},
// Helpers
// -------
// Generates a random new document id.
generateDocumentId : function () {
var id = '';
for (var x = 0; x < ID_LENGTH; x++) {
id += ID_CHARACTERS.charAt(Math.floor(Math.random() * 62));
}
return id;
}
});
var onboarding = [
'# What the _heck_ is this?',
'Socrates lets you write Markdown with whoever you want. Write words on the left, read _real_ similar words on the right, and send out the link!',
'',
'It\'s a weekend project by [@ivolo][1] and [@ianstormtaylor][2]. We we\'re always sending around Stypis and Etherpads while working on [Segment.io][3], but we really wanted to just write and read in Markdown instead. So that\'s what we built. Thanks to [Firebase][4], it was incredibly easy and it\'s all realtime! You can see all the code [on Github][5].',
'',
'More importantly though, erase this junk and start writing your own stuff...',
'',
'[1]: https://twitter.com/ivolo',
'[2]: https://twitter.com/ianstormtaylor',
'[3]: https://segment.io',
'[4]: https://firebase.com',
'[5]: https://github.com/segmentio/socrates',
'',
'---',
'',
'Oh, and you can embed Youtube videos too, so...',
'',
'http://www.youtube.com/watch?v=sdRm4hhb8wY'
].join('\n');
var random = [
[
'> I stare in the water at my own sweet reflection,',
'>',
'> And I feel a feeling of warm sweet affection.',
'>',
'> Why doesn\'t my body do the things that I want?',
'>',
'> For lack of exercise, it\'s surely the cause.\n',
'– by **Beverly Dingus**, a poem.\n\n',
'[On Health](http://www.youtube.com/watch?v=sYMYktsKmSk)'
].join('\n')
];