-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_reply.js.tid
More file actions
474 lines (425 loc) · 11.1 KB
/
_reply.js.tid
File metadata and controls
474 lines (425 loc) · 11.1 KB
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
modifier: bengillies
type: text/javascript
tags: excludeLists excludeSearch excludeMissing
/*global window,tiddlyweb,$*/
/*
* Bookmarker code
* The tabs object encapsulates all the code for each actual tab.
* Each tab is an instance of the Tab object, which includes functions for
* populating the tab with data, turning the data in the tab into a tiddler,
* etc.
* There is a Default tab, which can be extended, with specific values
* overidden only when they differ.
* The app starts up when it receives a message (i.e. receiveMessage).
* This triggers a callback from details (an event queue like thing) that is
* listening for the data to arrive.
*/
(function() {
var id; // we use this ID to verify that we only take notice of the correct
// messages that we receive from postMessage
var details = {
queue: [],
set: function(target, data) {
this[target] = data;
this.done(target);
},
done: function(target) {
var self = this;
this.queue = $.map(this.queue, function(obj, i) {
if (obj.target === target) {
obj.fn(self[target]);
return null;
}
return obj;
});
},
when: function(target, func) {
this.queue.push({
target: target,
fn: func
});
if (this[target]) {
this.done(target);
}
}
};
function _extend(target, obj) {
var key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
if ((!~['string', 'function'].indexOf(typeof obj[key]))
&& (!$.isArray(obj[key]))
&& (target.hasOwnProperty(key))) {
target[key] = _extend(_extend({}, target[key] || {}),
obj[key]);
} else {
target[key] = obj[key];
}
}
}
return target;
}
function Tab(opts) {
_extend(this, opts);
this.$el = $(this.el);
}
Tab.prototype.extend = function(opts) {
var newTab = new Tab(this);
_extend(newTab, opts);
newTab.$el = $(newTab.el);
return newTab;
};
Tab.prototype.setTab = function(data) {
var $el = this.$el,
self = this;
$.each(this.bind, function(field, selectors) {
var res = (self.populate[field]) ?
self.populate[field].call(self, data) : data[field];
res = $.isArray(res) ? res : [res];
selectors = $.isArray(selectors) ? selectors : [selectors];
$.each(selectors, function(i, selector) {
var $subEl = $el.find(selector),
setFn = ($subEl[0].nodeName === 'DIV') ? 'html' : 'val';
if (res[i]) {
$subEl[setFn](res[i]);
}
});
});
this.setFocus();
};
Tab.prototype.setTiddler = function() {
var tiddler = new tiddlyweb.Tiddler(),
priv = this.getPrivate(),
self = this;
$.each(this.bind, function(field, selectors) {
var fn = self.toTiddler[field] || function(txt) { return txt; };
if (tiddler.hasOwnProperty(field)) {
tiddler[field] = self.callWithValues(field, fn);
} else {
tiddler.fields = tiddler.fields || {};
tiddler.fields[field] = self.callWithValues(field, fn);
}
});
details.when('data', function(data) {
tiddler.bag = new tiddlyweb.Bag(data.space + priv, '/');
details.set('tiddler', tiddler);
});
};
Tab.prototype.getPrivate = function() {
return $('.form-actions [name="private"]input').is(':checked') ?
'_private' : '_public';
};
Tab.prototype.setFocus = function() {
var $el = this.$el.find(this.focus);
// use a setTimeout due to weirdness in chrome
window.setTimeout(function() {
$el.focus();
// HACKY: changing the text forces the cursor to the end of the textarea
var val = $el.val();
$el.val('');
$el.val(val);
}, 0);
};
Tab.prototype.callWithValues = function(field, fn) {
var args = [],
selectors = this.bind[field],
$el = this.$el;
selectors = $.isArray(selectors) ? selectors : [selectors];
$.each(selectors, function(i, selector) {
args.push($el.find(selector).val());
});
if (typeof fn === 'function') {
return fn.apply(this, args);
} else {
return this[fn].apply(this, args);
}
};
Tab.prototype.figureTags = function(tagString) {
var brackets = /^\s*\[\[([^\]\]]+)\]\](\s*.*)/,
whitespace = /^\s*([^\s]+)(\s*.*)/,
match,
rest = tagString,
tags = [];
match = brackets.exec(rest) || whitespace.exec(rest);
while (match) {
tags.push(match[1]);
rest = match[2];
match = brackets.exec(rest) || whitespace.exec(rest);
}
return tags;
};
Tab.prototype.isEmpty = function() {
return this.$el.find((this.bind && this.bind.title) || this.focus)
.val() === '';
};
var Default = new Tab({
focus: '[name="text"]textarea',
bind: {
title: '[name="title"]input',
text: '[name="text"]textarea',
tags: '[name="tags"]input',
'_source': '[name="url"]input'
},
populate: {},
toTiddler: { tags: 'figureTags' }
});
var tabs = {
post: Default.extend({
el: '#postForm',
populate: {
tags: function(data) {
var atTag = '@' + data.origin;
data.tags = data.tags || [];
if (!~data.tags.indexOf(atTag)) {
data.tags.push(atTag);
}
return $.map(data.tags, function(tag) {
return ~tag.indexOf(' ') ? '[[' + tag + ']]' : tag;
}).join(' ');
},
text: function(data) {
var replyMsg;
if (data.quote) {
replyMsg = 'in reply to [[' + data.title + ']]@'
+ data.origin + ':';
return replyMsg + '\n\n' + ((data.text) ? '> ' : '')
+ data.text.split('\n').join('\n> ');
} else {
return data.text;
}
}
}
})
};
function pickDefaultTab(data) {
return 'post';
}
function receiveMessage(event) {
var data = JSON.parse(event.data),
tid = new tiddlyweb.Tiddler(data.title);
if (!id) {
id = data.id;
}
function mergeTags(oldTags, newTags) {
if (!newTags) {
return oldTags;
}
if (!oldTags) {
oldTags = [];
}
$.each(newTags, function(i, tag) {
if (tag.charAt(0) === '@' && !~oldTags.indexOf(tag)) {
oldTags.push(tag);
}
});
return oldTags;
}
tid.recipe = new tiddlyweb.Recipe(data.space + '_private', '/');
tid.get(function(tiddler) {
details.set('data', {
title: tiddler.title,
text: tiddler.text,
space: data.space,
_source: data._source,
tags: mergeTags(tiddler.tags, data.tags),
origin: data.origin,
quote: false
});
details.set('eventSrc', {
origin: event.origin,
source: event.source
});
}, function() {
data.tags = mergeTags([], data.tags || []);
data.quote = true;
details.set('data', data);
details.set('eventSrc', {
origin: event.origin,
source: event.source
});
});
window.removeEventListener('message', receiveMessage, false);
}
window.addEventListener('message', receiveMessage, false);
function saveTiddler(callback) {
details.when('tiddler', function(tiddler) {
tiddler.put(function() {
callback(true);
}, function(xhr, error, exc) {
callback(false, error, exc);
});
});
}
function closePage(timeout) {
window.setTimeout(function() {
details.when('eventSrc', function(src) {
src.source.postMessage(JSON.stringify({
type: 'close',
id: id
}), src.origin);
});
}, timeout || 0);
}
function getCurrentTab() {
return $('.nav-tabs .active').data('tab-name');
}
function saveBookmark(event) {
var $successBtn = $('[type="submit"]input');
tabs[getCurrentTab()].setTiddler();
$successBtn.val('Saving...')
.addClass('disabled')
.attr('disabled', 'disabled');
$('.closeBtn').addClass('disabled')
.attr('disabled', 'disabled');
saveTiddler(function(success) {
if (success) {
$successBtn
.val('Saved!')
.removeClass('primary')
.addClass('success');
closePage(1000);
} else {
$successBtn
.removeClass('disabled')
.removeAttr('disabled')
.removeClass('primary')
.addClass('danger')
.val('Error saving. Please try again');
}
});
event.preventDefault();
return false;
}
function Mover($el) {
var moving = false,
initPos = {},
oldPos = {};
var src,
height;
var doMove = function(ev) {
var diff = { x: ev.pageX - oldPos.x, y: ev.pageY - oldPos.y };
$el.animate({
top: '+=' + diff.y,
left: '+=' + diff.x
}, 0);
oldPos.x = ev.pageX;
oldPos.y = ev.pageY;
};
var _receive = function _receive(message) {
var payload = JSON.parse(message.data);
if (payload.id !== id) {
return;
}
switch(payload.type) {
case 'initMove':
$el.css({
top: payload.diff.y + 'px',
left: payload.diff.x + 'px'
}).show();
initPos.x = oldPos.x = payload.diff.x + oldPos.x;
initPos.y = oldPos.y = payload.diff.y + oldPos.y;
break;
case 'doneMove':
$el.show();
window.removeEventListener('message', _receive, false);
}
};
var self;
self = {
start: function(ev) {
if (moving) {
return self.stop(ev);
} else {
moving = true;
}
oldPos.x = ev.pageX;
oldPos.y = ev.pageY;
$el.hide();
window.addEventListener('message', _receive, false);
details.when('eventSrc', function(eventSrc) {
src = eventSrc;
src.source.postMessage(JSON.stringify({
type: 'startMove',
id: id
}), src.origin);
});
// fix the height so that increasing the iframe height doesn't mess things up
height = $el.css('height');
$el.css('height', $el.height());
// stop the user selecting text awkwardly while trying to move
$el.css({
'-webkit-user-select': 'none',
'-moz-user-select': 'none',
'-ms-user-select': 'none',
'-o-user-select': 'none',
'user-select': 'none'
});
$(document).bind('mousemove', doMove);
},
stop: function() {
if (!moving) {
return;
}
window.addEventListener('message', _receive, false);
moving = false;
$el.hide();
$el.css({
top: 0,
left: 0
});
$el.css('height', height);
src.source.postMessage(JSON.stringify({
type: 'stopMove',
id: id,
diff: { x: oldPos.x - initPos.x, y: oldPos.y - initPos.y }
}), src.origin);
$el.css({
'-webkit-user-select': 'auto',
'-moz-user-select': 'auto',
'-ms-user-select': 'auto',
'-o-user-select': 'auto',
'user-select': 'auto'
});
$(document).unbind('mousemove', doMove);
}
};
return self;
}
$(function() {
$('.form-actions [type="submit"]input').click(saveBookmark);
$('.closeBtn').click(closePage);
var mover = new Mover($('.modal'));
$('.modal-header').mousedown(function(ev) {
if (ev.target.nodeName !== 'LI' &&
$(ev.target).closest('.nav-tabs li, #help, #help-info').length === 0) {
mover.start(ev);
}
});
$(document).mouseup(mover.stop);
$('#help').click(function(ev) {
ev.preventDefault();
$('#help-info').toggle(100);
});
details.when('data', function(data) {
// figure out which tab we should start off on
var tab = pickDefaultTab(data);
// populate the tab with data when the user switches to it
$('.nav-tabs').delegate('li', 'click', function(ev) {
var tabName = $(this).data('tab-name');
if (tabs[tabName].isEmpty()) {
tabs[tabName].setTab(data);
}
});
// initialise the app by switching to the correct tab.
$('.nav-tabs li').each(function(i, el) {
var $el = $(el);
if ($el.data('tab-name') === tab) {
$el.find('a').click();
return false;
}
});
// now display the container again
$('#container').show();
});
});
}());