2011-01-22 18:25:16 -05:00
|
|
|
{% load bookmarklet_includes utils_tags %}
|
2011-01-22 11:22:14 -05:00
|
|
|
|
2011-01-22 21:55:10 -05:00
|
|
|
(function() {
|
2012-07-31 13:04:33 -07:00
|
|
|
window.NEWSBLUR = window.NEWSBLUR || {};
|
2011-01-22 21:53:40 -05:00
|
|
|
|
2011-01-21 20:29:19 -05:00
|
|
|
{% include_bookmarklet_js %}
|
2011-01-22 21:55:10 -05:00
|
|
|
|
2012-08-11 13:53:00 -07:00
|
|
|
NEWSBLUR.Bookmarklet = function(options) {
|
2011-01-21 20:29:19 -05:00
|
|
|
var defaults = {};
|
2011-01-22 11:22:14 -05:00
|
|
|
|
2011-01-22 18:25:16 -05:00
|
|
|
this.token = "{{ token }}";
|
|
|
|
this.active = true;
|
2011-01-21 20:29:19 -05:00
|
|
|
this.username = '{{ user.username }}';
|
2011-01-22 18:25:16 -05:00
|
|
|
this.folders = {{ folders|safe }};
|
|
|
|
this.domain = "{% current_domain %}";
|
2011-03-21 10:15:18 -04:00
|
|
|
this.flags = {
|
|
|
|
'new_folder': false
|
2011-03-21 20:37:58 -04:00
|
|
|
};
|
2011-01-21 20:29:19 -05:00
|
|
|
|
2011-01-22 18:25:16 -05:00
|
|
|
this.options = $.extend({}, defaults, options);
|
2011-01-21 20:29:19 -05:00
|
|
|
this.runner();
|
2012-04-21 17:15:45 -07:00
|
|
|
|
|
|
|
{% if code < 0 %}
|
|
|
|
this.show_error();
|
|
|
|
{% endif %}
|
2011-01-21 20:29:19 -05:00
|
|
|
};
|
|
|
|
|
2012-08-11 13:53:00 -07:00
|
|
|
NEWSBLUR.Bookmarklet.prototype = {
|
2011-01-21 20:29:19 -05:00
|
|
|
|
|
|
|
fix_title: function() {
|
|
|
|
var d = document;
|
2012-08-10 19:20:44 -07:00
|
|
|
d.title = d.title.replace(/\(Sharing\.\.\.\)\s?/g, '');
|
2011-01-21 20:29:19 -05:00
|
|
|
d.title = d.title.replace(/\(Adding\.\.\.\)\s?/g, '');
|
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
|
|
|
this.active = false;
|
2012-08-11 13:53:00 -07:00
|
|
|
$('body').css('overflow', 'scroll');
|
2011-01-21 20:29:19 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
runner: function() {
|
|
|
|
this.fix_title();
|
|
|
|
|
|
|
|
if (this.check_if_on_newsblur()) {
|
2011-01-24 09:58:16 -05:00
|
|
|
var message = "This bookmarklet is successfully installed.\nClick it while on a site you want to read in NewsBlur.";
|
2011-01-21 20:29:19 -05:00
|
|
|
this.alert(message);
|
|
|
|
return this.close();
|
|
|
|
}
|
|
|
|
|
2011-01-21 22:00:36 -05:00
|
|
|
this.attach_css();
|
2011-01-21 20:29:19 -05:00
|
|
|
this.make_modal();
|
|
|
|
this.open_modal();
|
2012-08-10 19:20:44 -07:00
|
|
|
this.get_page_content();
|
2011-01-21 20:29:19 -05:00
|
|
|
|
2011-01-22 11:22:14 -05:00
|
|
|
this.$modal.bind('click', $.rescope(this.handle_clicks, this));
|
2011-01-21 20:29:19 -05:00
|
|
|
},
|
|
|
|
|
2012-04-21 17:15:45 -07:00
|
|
|
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'),
|
|
|
|
'.'
|
|
|
|
]));
|
|
|
|
},
|
|
|
|
|
2011-01-21 22:00:36 -05:00
|
|
|
attach_css: function() {
|
|
|
|
var css = "{% include_bookmarklet_css %}";
|
2012-08-11 13:53:00 -07:00
|
|
|
var style = '<style id="newsblur_bookmarklet_css">' + css + '</style>';
|
|
|
|
if ($('#newsblur_bookmarklet_css').length) {
|
|
|
|
$('#newsblur_bookmarklet_css').replaceWith(style);
|
|
|
|
} else if ($('head').length) {
|
|
|
|
$('head').append(style);
|
|
|
|
} else {
|
|
|
|
$('body').append(style);
|
|
|
|
}
|
2011-01-21 22:00:36 -05:00
|
|
|
},
|
|
|
|
|
2011-01-21 20:29:19 -05:00
|
|
|
alert: function(message) {
|
|
|
|
alert(message);
|
|
|
|
},
|
|
|
|
|
|
|
|
check_if_on_newsblur: function() {
|
2011-01-22 18:25:16 -05:00
|
|
|
if (window.location.href.indexOf(this.domain) != -1) {
|
2011-01-21 20:29:19 -05:00
|
|
|
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)
|
|
|
|
]),
|
2012-08-10 19:20:44 -07:00
|
|
|
$.make('div', { className: 'NB-modal-title' }, 'Sharing \"'+this.get_page_title()+'\"'),
|
|
|
|
$.make('div', { className: 'NB-bookmarklet-main'}, [
|
|
|
|
$.make('div', { className: 'NB-bookmarklet-page' }, [
|
|
|
|
$.make('div', { className: 'NB-bookmarklet-page-title' }),
|
|
|
|
$.make('div', { className: 'NB-bookmarklet-page-content' })
|
|
|
|
])
|
|
|
|
]),
|
2011-01-21 22:00:36 -05:00
|
|
|
$.make('div', { className: 'NB-bookmarklet-folder-container' }, [
|
2011-03-21 20:37:58 -04:00
|
|
|
$.make('img', { className: 'NB-bookmarklet-folder-add-button', src: 'data:image/png;charset=utf-8;base64,{{ add_image }}', title: 'Add New Folder' }),
|
2011-01-21 20:29:19 -05:00
|
|
|
this.make_folders(),
|
2011-03-21 10:15:18 -04:00
|
|
|
$.make('div', { className: 'NB-bookmarklet-new-folder-container' }, [
|
2011-03-21 20:37:58 -04:00
|
|
|
$.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' })
|
2011-03-21 10:15:18 -04:00
|
|
|
])
|
2011-01-21 22:00:36 -05:00
|
|
|
]),
|
|
|
|
$.make('div', { className: 'NB-modal-submit' }, [
|
2011-01-21 20:29:19 -05:00
|
|
|
$.make('div', { className: 'NB-modal-submit-button NB-modal-submit-green' }, 'Add this site')
|
|
|
|
])
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
|
|
|
|
get_page_title: function() {
|
|
|
|
var title = document.title;
|
|
|
|
|
2012-08-11 14:54:44 -07:00
|
|
|
if (title.length > 40) {
|
|
|
|
title = title.substr(0, 40) + '...';
|
2011-01-21 20:29:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return title;
|
|
|
|
},
|
|
|
|
|
|
|
|
make_folders: function() {
|
|
|
|
var folders = this.folders;
|
|
|
|
var $options = $.make('select', { className: 'NB-folders'});
|
|
|
|
|
|
|
|
$options = this.make_folder_options($options, folders, '-');
|
2011-01-21 22:00:36 -05:00
|
|
|
|
|
|
|
var $option = $.make('option', { value: '', selected: true }, "Top Level");
|
|
|
|
$options.prepend($option);
|
|
|
|
|
2011-01-21 20:29:19 -05:00
|
|
|
return $options;
|
|
|
|
},
|
|
|
|
|
|
|
|
make_folder_options: function($options, items, depth) {
|
2012-04-21 17:15:45 -07:00
|
|
|
if (depth && depth.length > 5) {
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2011-01-21 20:29:19 -05:00
|
|
|
for (var i in items) {
|
2012-04-21 17:15:45 -07:00
|
|
|
if (!items.hasOwnProperty(i)) continue;
|
2011-01-21 20:29:19 -05:00
|
|
|
var item = items[i];
|
|
|
|
if (typeof item == "object") {
|
|
|
|
for (var o in item) {
|
2012-04-21 17:15:45 -07:00
|
|
|
if (!item.hasOwnProperty(o)) continue;
|
2011-01-21 20:29:19 -05:00
|
|
|
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({
|
2012-08-10 19:20:44 -07:00
|
|
|
'minWidth': 800,
|
|
|
|
'maxWidth': 800,
|
2011-01-21 20:29:19 -05:00
|
|
|
'overlayClose': true,
|
|
|
|
'onOpen': function (dialog) {
|
|
|
|
dialog.overlay.fadeIn(200, function () {
|
|
|
|
dialog.container.fadeIn(200);
|
|
|
|
dialog.data.fadeIn(200);
|
2011-01-21 22:00:36 -05:00
|
|
|
setTimeout(function() {
|
|
|
|
$(window).resize();
|
|
|
|
}, 10);
|
2011-01-21 20:29:19 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
'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();
|
|
|
|
}
|
|
|
|
});
|
2012-08-11 13:53:00 -07:00
|
|
|
|
|
|
|
$('body').css('overflow', 'hidden');
|
2011-01-21 20:29:19 -05:00
|
|
|
},
|
2011-01-22 11:22:14 -05:00
|
|
|
|
|
|
|
save: function() {
|
2011-01-22 18:25:16 -05:00
|
|
|
var self = this;
|
|
|
|
var $submit = $('.NB-modal-submit-button');
|
2011-01-22 11:22:14 -05:00
|
|
|
var folder = $('.NB-folders').val();
|
2011-01-22 18:25:16 -05:00
|
|
|
var add_site_url = "http://"+this.domain+"{% url api-add-site token %}?callback=?";
|
|
|
|
|
|
|
|
$submit.addClass('NB-disabled').text('Fetching and parsing...');
|
2011-01-22 11:22:14 -05:00
|
|
|
|
2011-03-21 10:15:18 -04:00
|
|
|
var data = {
|
2011-01-22 11:22:14 -05:00
|
|
|
url: window.location.href,
|
|
|
|
folder: folder
|
2011-03-21 10:15:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (this.flags['new_folder']) {
|
|
|
|
var new_folder_name = $('input[name=new_folder_name]', this.$modal).val();
|
|
|
|
if (new_folder_name) {
|
2011-03-21 20:37:58 -04:00
|
|
|
data['new_folder'] = new_folder_name;
|
2011-03-21 10:15:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$.getJSON(add_site_url, data, function(resp) {
|
2011-01-22 18:25:16 -05:00
|
|
|
self.post_save(resp);
|
2011-01-22 11:22:14 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-01-22 18:25:16 -05:00
|
|
|
post_save: function(resp) {
|
|
|
|
var $submit = $('.NB-modal-submit-button');
|
|
|
|
|
|
|
|
$submit.addClass('NB-close');
|
|
|
|
|
|
|
|
if (resp.code == 1) {
|
2011-01-22 21:42:58 -05:00
|
|
|
$submit.html($.make('div', { className: 'NB-bookmarklet-accept' }, [
|
|
|
|
$.make('img', { src: 'data:image/png;charset=utf-8;base64,{{ accept_image }}' }),
|
2011-01-22 18:25:16 -05:00
|
|
|
'Added!'
|
|
|
|
]));
|
|
|
|
setTimeout(function() {
|
2011-01-22 21:42:58 -05:00
|
|
|
$.modal.close();
|
|
|
|
}, 2000);
|
2011-01-22 18:25:16 -05:00
|
|
|
} else {
|
2011-01-22 21:42:58 -05:00
|
|
|
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);
|
2011-01-22 18:25:16 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-03-21 10:15:18 -04:00
|
|
|
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;
|
|
|
|
},
|
|
|
|
|
2012-08-10 19:20:44 -07:00
|
|
|
// =========================
|
|
|
|
// = Page-specific actions =
|
|
|
|
// =========================
|
|
|
|
|
|
|
|
get_page_content: function() {
|
2012-08-11 14:54:44 -07:00
|
|
|
var $title = $('.NB-modal-title', this.$modal);
|
2012-08-10 19:20:44 -07:00
|
|
|
var $content = $('.NB-bookmarklet-page-content', this.$modal);
|
2012-08-11 14:54:44 -07:00
|
|
|
var $readability = $(window.readability.init());
|
2012-08-10 19:20:44 -07:00
|
|
|
|
2012-08-11 14:54:44 -07:00
|
|
|
var title = $readability.children("h1").text();
|
|
|
|
$title.html(title);
|
|
|
|
|
|
|
|
var content = $("#readability-content", $readability).html();
|
|
|
|
$content.html(content);
|
2012-08-10 19:20:44 -07:00
|
|
|
},
|
|
|
|
|
2011-01-21 20:29:19 -05:00
|
|
|
// ===========
|
|
|
|
// = Actions =
|
|
|
|
// ===========
|
|
|
|
|
2011-01-22 11:22:14 -05:00
|
|
|
handle_clicks: function(elem, e) {
|
2011-01-21 20:29:19 -05:00
|
|
|
var self = this;
|
2011-03-21 10:15:18 -04:00
|
|
|
|
2011-01-22 11:22:14 -05:00
|
|
|
$.targetIs(e, { tagSelector: '.NB-modal-submit-button' }, function($t, $p) {
|
2011-01-21 20:29:19 -05:00
|
|
|
e.preventDefault();
|
2011-01-22 18:25:16 -05:00
|
|
|
|
|
|
|
if (!$t.hasClass('NB-disabled')) {
|
|
|
|
self.save();
|
|
|
|
}
|
|
|
|
});
|
2011-03-21 10:15:18 -04:00
|
|
|
|
|
|
|
$.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');
|
|
|
|
});
|
|
|
|
|
2011-01-22 18:25:16 -05:00
|
|
|
$.targetIs(e, { tagSelector: '.NB-close' }, function($t, $p) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
$.modal.close();
|
2011-01-21 20:29:19 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-08-11 13:53:00 -07:00
|
|
|
if (NEWSBLUR.bookmarklet && NEWSBLUR.bookmarklet.active) {
|
|
|
|
NEWSBLUR.bookmarklet.fix_title();
|
2011-01-21 20:29:19 -05:00
|
|
|
return;
|
|
|
|
}
|
2012-08-11 13:53:00 -07:00
|
|
|
NEWSBLUR.bookmarklet = new NEWSBLUR.Bookmarklet();
|
2011-01-21 20:29:19 -05:00
|
|
|
|
2011-01-22 21:55:10 -05:00
|
|
|
})();
|