/
bookmarklet_subscribe.js
281 lines (229 loc) · 9.92 KB
/
bookmarklet_subscribe.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
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
{% load bookmarklet_includes utils_tags %}
(function() {
window.NEWSBLUR = window.NEWSBLUR || {};
{% include_bookmarklet_js %}
NEWSBLUR.BookmarkletModal = function(options) {
var defaults = {};
this.token = "{{ token }}";
this.active = true;
this.username = '{{ user.username }}';
this.folders = {{ folders|safe }};
this.domain = "{% current_domain %}";
this.flags = {
'new_folder': false
};
this.options = $.extend({}, defaults, options);
this.runner();
{% if code < 0 %}
this.show_error();
{% endif %}
};
NEWSBLUR.BookmarkletModal.prototype = {
fix_title: function() {
var d = document;
d.title = d.title.replace(/\(Adding\.\.\.\)\s?/g, '');
},
close: function() {
this.active = false;
},
runner: function() {
this.fix_title();
if (this.check_if_on_newsblur()) {
var message = "This bookmarklet is successfully installed.\nClick it while on a site you want to read in NewsBlur.";
this.alert(message);
return this.close();
}
this.attach_css();
this.make_modal();
this.open_modal();
this.$modal.bind('click', $.rescope(this.handle_clicks, this));
},
show_error: function() {
$('.NB-bookmarklet-folder-container', this.$modal).hide();
$('.NB-modal-submit', this.$modal).html($.make('div', { className: 'NB-error-invalid' }, [
'This bookmarklet no longer matches an account. Re-create it in ',
$.make('a', { href: 'http://www.newsblur.com/?next=goodies' }, 'Goodies on NewsBlur'),
'.'
]));
},
attach_css: function() {
var css = "{% include_bookmarklet_css %}";
var style = '<style>' + css + '</style>';
if ($('head').length) $('head').append(style);
else $('body').append(style);
},
alert: function(message) {
alert(message);
},
check_if_on_newsblur: function() {
if (window.location.href.indexOf(this.domain) != -1) {
return true;
}
},
make_modal: function() {
var self = this;
this.$modal = $.make('div', { className: 'NB-bookmarklet NB-modal' }, [
$.make('div', { className: 'NB-modal-information' }, [
'Signed in as ',
$.make('b', { style: 'color: #505050' }, this.username)
]),
$.make('div', { className: 'NB-modal-title' }, 'Adding \"'+this.get_page_title()+'\"'),
$.make('div', { className: 'NB-bookmarklet-folder-container' }, [
$.make('img', { className: 'NB-bookmarklet-folder-add-button', src: 'data:image/png;charset=utf-8;base64,{{ add_image }}', title: 'Add New Folder' }),
this.make_folders(),
$.make('div', { className: 'NB-bookmarklet-new-folder-container' }, [
$.make('img', { className: 'NB-bookmarklet-folder-new-label', src: 'data:image/png;charset=utf-8;base64,{{ new_folder_image }}' }),
$.make('input', { type: 'text', name: 'new_folder_name', className: 'NB-bookmarklet-folder-new' })
])
]),
$.make('div', { className: 'NB-modal-submit' }, [
$.make('div', { className: 'NB-modal-submit-button NB-modal-submit-green' }, 'Add this site')
])
]);
},
get_page_title: function() {
var title = document.title;
if (title.length > 20) {
title = title.substr(0, 20) + '...';
}
return title;
},
make_folders: function() {
var folders = this.folders;
var $options = $.make('select', { className: 'NB-folders'});
$options = this.make_folder_options($options, folders, '-');
var $option = $.make('option', { value: '', selected: true }, "Top Level");
$options.prepend($option);
return $options;
},
make_folder_options: function($options, items, depth) {
if (depth && depth.length > 5) {
return $options;
}
for (var i in items) {
if (!items.hasOwnProperty(i)) continue;
var item = items[i];
if (typeof item == "object") {
for (var o in item) {
if (!item.hasOwnProperty(o)) continue;
var folder = item[o];
var $option = $.make('option', { value: o }, depth + ' ' + o);
$options.append($option);
$options = this.make_folder_options($options, folder, depth+'-');
}
}
}
return $options;
},
open_modal: function() {
var self = this;
this.$modal.modal({
'minWidth': 600,
'maxWidth': 600,
'overlayClose': true,
'onOpen': function (dialog) {
dialog.overlay.fadeIn(200, function () {
dialog.container.fadeIn(200);
dialog.data.fadeIn(200);
setTimeout(function() {
$(window).resize();
}, 10);
});
},
'onShow': function(dialog) {
$('#simplemodal-container').corner('6px');
},
'onClose': function(dialog) {
dialog.data.hide().empty().remove();
dialog.container.hide().empty().remove();
dialog.overlay.fadeOut(200, function() {
dialog.overlay.empty().remove();
$.modal.close();
self.close();
});
$('.NB-modal-holder').empty().remove();
}
});
},
save: function() {
var self = this;
var $submit = $('.NB-modal-submit-button');
var folder = $('.NB-folders').val();
var add_site_url = "http://"+this.domain+"{% url api-add-site token %}?callback=?";
$submit.addClass('NB-disabled').text('Fetching and parsing...');
var data = {
url: window.location.href,
folder: folder
};
if (this.flags['new_folder']) {
var new_folder_name = $('input[name=new_folder_name]', this.$modal).val();
if (new_folder_name) {
data['new_folder'] = new_folder_name;
}
}
$.getJSON(add_site_url, data, function(resp) {
self.post_save(resp);
});
},
post_save: function(resp) {
var $submit = $('.NB-modal-submit-button');
$submit.addClass('NB-close');
if (resp.code == 1) {
$submit.html($.make('div', { className: 'NB-bookmarklet-accept' }, [
$.make('img', { src: 'data:image/png;charset=utf-8;base64,{{ accept_image }}' }),
'Added!'
]));
setTimeout(function() {
$.modal.close();
}, 2000);
} else {
var $error = $.make('div', { className: 'NB-bookmarklet-error' }, [
$.make('img', { className: 'NB-bookmarklet-folder-label', src: 'data:image/png;charset=utf-8;base64,{{ error_image }}' }),
$.make('div', resp.message)
]);
$('.NB-bookmarklet-folder-container').hide();
$submit.replaceWith($error);
}
},
open_add_folder: function() {
var $new_folder = $('.NB-bookmarklet-new-folder-container', this.$modal);
$new_folder.slideDown(500);
this.flags['new_folder'] = true;
},
close_add_folder: function() {
var $new_folder = $('.NB-bookmarklet-new-folder-container', this.$modal);
$new_folder.slideUp(500);
this.flags['new_folder'] = false;
},
// ===========
// = Actions =
// ===========
handle_clicks: function(elem, e) {
var self = this;
$.targetIs(e, { tagSelector: '.NB-modal-submit-button' }, function($t, $p) {
e.preventDefault();
if (!$t.hasClass('NB-disabled')) {
self.save();
}
});
$.targetIs(e, { tagSelector: '.NB-bookmarklet-folder-add-button' }, function($t, $p) {
e.preventDefault();
if ($t.hasClass('NB-active')) {
self.close_add_folder();
} else {
self.open_add_folder();
}
$t.toggleClass('NB-active');
});
$.targetIs(e, { tagSelector: '.NB-close' }, function($t, $p) {
e.preventDefault();
$.modal.close();
});
}
};
if (NEWSBLUR.bookmarklet_modal && NEWSBLUR.bookmarklet_modal.active) {
NEWSBLUR.bookmarklet_modal.fix_title();
return;
}
NEWSBLUR.bookmarklet_modal = new NEWSBLUR.BookmarkletModal();
})();