easybib_popup.js | |
---|---|
/*jshint indent:2 */
/*global chrome:true */ | |
This is the script to the menu-popup, user interactions are relayed to as a message send over the port the background.js, for the credibility check it also reacts to an update-event, fired by the background.js | (function() {
'use strict';
var port, debug;
function getUserProfileLink(res) {
var username = res.name;
return $('<a href="http://easybib.com/account" target="_blank" tabindex="-1" data-id="accountlink">' + username + '</a>');
}
function isSuperuser(res) {
var role = res.role;
var matchCriteria = /(mybibpro|admin|schooledition|libraryedition)/ig;
return (role.match(matchCriteria)) ? true : false;
}
function getLogoutLink(res) {
return $('<a href="http://easybib.com/logout" target="_blank" tabindex="-1" data-id="logoutlink">Logout</a>');
}
function getLoginLink() {
return $('<a href="http://easybib.com/login" target="_blank" tabindex="-1" data-id="accountlink">Login</a>');
}
function getRegisterLink() {
return $('<a href="http://easybib.com/register" target="_blank" tabindex="-1" data-id="accountlink">Register</a>');
}
function getProjectOption(project) {
var id = project.links[0].href.split('/').pop();
return $('<option value="' + id + '" ' + ((project.current === true) ? ' selected' : '') + ' >' + project.data.name + '</option>');
}
port = chrome.extension.connect({
name: 'background'
}); |
send opened message, so that the login status and the project list can be set out of the simple cache implemented in the background.js | port.postMessage({
type: 'popup-opened'
}); |
wrapping console.logs for debug purposes | debug = {
active: true,
log: function() {
if (debug.active) {
console.log.apply(console, arguments);
}
}
}; |
Here is all the initialization for the browser-action popup | $(document).ready(function() {
var getSelection = function() {
var selection = window.getSelection();
if (selection.type == 'None') {
return false;
}
var range = selection.getRangeAt(0);
var div;
if (range) {
div = document.createElement('div');
div.appendChild(range.cloneContents());
}
return div.innerHTML;
};
port.onMessage.addListener(function(msg) {
if (msg.type === 'update-credibility') {
if (msg.state === 'done') {
var content = $('<span>');
var learn_more = '<a href="http://www.easybib.com/kb/index/view/id/172/page/1" target="_blank" ';
learn_more += 'class="evaluate">Learn more</a>.';
if (msg.res === 'yes') {
content = $('<span style="color: #00AA00;">Website is credible. ' + learn_more + '</span>');
} else if (msg.res === 'no') {
content = $('<span style="color: #AA0000;">Website is not credible. ' + learn_more + '</span>');
} else if (msg.res === 'may') {
content = $('<span style="color: #ffb400;">Website may be credible. ' + learn_more + '</span>');
}
$('#info_credibility')
.empty()
.append(content);
} else {
$('#info_credibility')
.empty()
.append($('<span>' + msg.text + '</span>'));
}
}
if (msg.type === 'check-textselection') {
var codeString = '(' + getSelection + ')();';
chrome.tabs.executeScript(null, {
code: codeString
}, function(content){
if (!(content[0].length)) {
$('#action_add_to_notebook').off('click').css({'opacity':'.7'}).text('Please make a selection.');
}
});
}
if (msg.type === 'update-loginstatus') {
if (msg.state === 'done') {
var $auth = $('[data-id="auth"]');
var $viewBibliography = $('#action_view_bibliography'); |
if user is logged in | if (msg.res) { |
fetch the first time and only if its not cached | if (!msg.res.cached) {
port.postMessage({
type: 'get-projectlist'
});
}
$('#action_add_to_notebook').click(function() {
var codeString = '(' + getSelection + ')();';
chrome.tabs.executeScript(null, {
code: codeString
}, function(highlightedContent) {
if (!highlightedContent) {
return;
}
chrome.tabs.getSelected(null, function(tab){
var projectID = $('.project_list option:selected')[0].value;
$('#output').delay(2500).html('<strong>Note Uploaded Successfully</strong>');
port.postMessage({
type: 'gonotebook',
content: {
"currentPageURL":tab.url,
"title": tab.title,
"evidence": highlightedContent[0],
"paraphrasing": "",
"comment": ""
},
projectID: projectID
});
});
});
});
$auth
.empty()
.append(
getUserProfileLink(msg.res),
' / ',
getLogoutLink(msg.res)
);
} else { |
on logged out state | $('[data-id="projectlist-container"]').hide();
$('[data-id="projectlist"]').empty();
$auth
.empty()
.append(
getLoginLink(),
' / ',
getRegisterLink()
);
}
}
}
if (msg.type === 'update-projectlist') {
if (msg.state === 'done') {
var $list = $('[data-id="projectlist"]');
$list.empty();
if (msg.res && msg.res.data && msg.res.data.length > 0) {
for (var i in msg.res.data) {
var item = msg.res.data[i];
$list.append(getProjectOption(item));
}
$('[data-id="projectlist-container"]').show();
}
}
}
}); |
init an easybib-Utils-object var easyUtils = new EasyBibUtils(); | $('#action_cite_on_easybib').click(function() {
chrome.windows.getCurrent(function(currentWindow) {
chrome.tabs.getSelected(currentWindow.id, function(selectedTab) {
port.postMessage({
type: 'cite',
param: selectedTab.url
});
});
});
}); |
Click on the Logo | $('#action_go_easybib').click(function() {
port.postMessage({
type: 'goeasybib'
});
}); |
Click on Bibliography | $('#action_view_bibliography').click(function() {
port.postMessage({
type: 'gobibliography'
});
});
$('[data-id="projectlist"]').on('change', function() {
port.postMessage({
type: 'change-active-project',
project_id: $('[data-id="projectlist"]').val()
});
}); |
Handle Search inputs | $('#action_search_easybib').keydown(function(evt) { |
if ENTER | if (evt.which === 13) {
evt.preventDefault();
port.postMessage({
type: 'search',
param: $('#action_search_easybib').val()
});
}
}); |
checking the credibitilty for the active tab in the active window | $('#info_credibility')
.empty()
.append($('<span>checking ...</span>'));
$('[data-id="refresh-button"]').click(function() {
port.postMessage({
type: 'check-loginstatus'
});
chrome.windows.getCurrent(function(currentWindow) {
chrome.tabs.getSelected(currentWindow.id, function(selectedTab) {
port.postMessage({
type: 'check-credibility',
param: selectedTab.url
});
});
});
});
chrome.windows.getCurrent(function(currentWindow) {
chrome.tabs.getSelected(currentWindow.id, function(selectedTab) {
port.postMessage({
type: 'check-credibility',
param: selectedTab.url
});
});
});
});
}());
|