(function () { 'use strict'; var serviceId = 'evernote'; angular.module('app').factory(serviceId, ['config', '$q', 'user', '$window', '$http', evernote]); //evernote related service function evernote(config, $q, user, $window, $http) { var service = { getAllNotebooks: getAllNotebooks, getAllNotesForNotebook: getAllNotesForNotebook, getNote: getNote, removeOAuth: removeOAuth, authenticate: authenticate }; return service; //get all notebooks for the user function getAllNotebooks() { var request = $http({ method: "get", url: config.evernoteRemoteServiceUrl + 'notebooks/' }); return (request.then(function (response) { return response.data; }, handleError)); } //get all notes in a notebook function getAllNotesForNotebook(notebookId) { var request = $http({ method: "get", url: config.evernoteRemoteServiceUrl + 'notebook/'+notebookId+'/notes' }); return (request.then(function (response) { return response.data; }, handleError)); } //get the full note function getNote(noteId) { var request = $http({ method: "get", url: config.evernoteRemoteServiceUrl + 'note/' + noteId }); return (request.then(function (response) { return response.data; }, handleError)); } //remove Evernote OAuth from MyShowcase function removeOAuth() { return user.removeEvernoteOAuth(); } function handleError(response) { if (!angular.isObject(response.data) ||!response.data.message) return ($q.reject("An unknown error occurred.")); // Otherwise, use expected error message. return ($q.reject(response.data.message)); } //authenticate with OAuth to Evernote function authenticate() { //to get round the browser popup blocker we have to open the popup window first - because this event is initiated by the user (clicking on a button) and the browser doesn't invoke the blocker if the user has initiated //once the window is open we can set the location to the correct url once it is available from the async call //if you open the window in the handler from the async call the browser doesn't regard it as user initiated so blocks it by default var deferred = $q.defer(); //the listener funcion will be called when the opened browser window sends this window a message (not in the case of IE) function listener(event) { var response = queryParamsFromUrl(event.data); if (event.data!=="verified") deferred.reject(response); else deferred.resolve(); //resolve the promise $window.removeEventListener('message', listener, false); } //open the popup, hopefully popup blockers won't stop this as it was user initiated var authWindow = $window.open('evernotepreauth.html', '_evernoteOauthSigninWindow', popupSize(1000, 800)); if (isNotIE()) $window.addEventListener('message', listener, false); //get the evernote call forward url return user.getEvernoteCallForwardUrl().then(function (data) { //now set our empty popup window location to the evernote call forward url authWindow.location.replace(data.url); if(!isNotIE()) deferred.resolve(); //if its IE resolve the promise here because the listener function won't be called return deferred.promise; }); } //test if browser is Internet Explorer function isNotIE() { return navigator.userAgent.indexOf("Trident") === -1; } //make sure we get a correct popup window function popupSize(popupWidth, popupHeight) { var popupLeft, popupTop; // Metrics for the current browser window. var x0 = $window.screenX || $window.screenLeft; var y0 = $window.screenY || $window.screenTop; var width = $window.outerWidth || $document.documentElement.clientWidth; var height = $window.outerHeight || $document.documentElement.clientHeight; // Computed popup window metrics. popupLeft = Math.round(x0) + (width - popupWidth) / 2; popupTop = Math.round(y0) + (height - popupHeight) / 2.5; if (popupLeft < x0) popupLeft = x0; if (popupTop < y0) popupTop = y0; return 'width=' + popupWidth + ',height=' + popupHeight + ',' + 'left=' + popupLeft + ',top=' + popupTop + ',' + 'dialog=yes,dependent=yes,scrollbars=yes,location=yes'; } //get data from a url query string function queryParamsFromUrl(url) { var match = /^[^?#]+(\?([^\#]*))?(\#(.*))?$/.exec(url); if (!match) { return {}; } var query = match[2] || '' , fragment = match[4] || '' , fragmentOffset = fragment.indexOf('?') , params = {}; if (fragmentOffset !== -1) fragment = fragment.substring(fragmentOffset + 1); var kvp = query.split('&').concat(fragment.split('&')); kvp.forEach(function (kv) { var offset = kv.indexOf('='); if (offset === -1) return; params[decodeURIComponent(kv.substring(0, offset))] = decodeURIComponent(kv.substring(offset + 1)); }); return params; } } })();