From 9ffa43ce1217f94603b63e95e6f3cada506ececd Mon Sep 17 00:00:00 2001 From: Kerrick Long Date: Tue, 28 Jun 2011 16:05:57 -0500 Subject: [PATCH] Added voteUp() functionality. --- js/common.js | 82 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 4 deletions(-) diff --git a/js/common.js b/js/common.js index b3284a2..cec9699 100644 --- a/js/common.js +++ b/js/common.js @@ -188,6 +188,40 @@ function RedditAPI(domain) { return true; } +/** + * Transmits info to the API and returns the response. + * @alias RedditAPI.apiTransmit(type, url, async, data) + * @param {String} type The type of HTTP request: 'GET' or 'POST'. + * @param {String} url The URL to request. + * @param {Boolean} async If true, the request will be asynchronous. + * @param {Object} data If it exists, send this as a FormData() object. + * @return {Object} Returns the API's response as an object. + * @method + */ +RedditAPI.prototype.apiTransmit = function (type, url, async, data) { + var req; + + req = new XMLHttpRequest(); + req.open(type, url, async); + + if (data) { + req.send(data); + } else { + req.send(null); + } + + if (req.status !== 200) { + console.warn(req); + throw 'Error loading API.\nURL: ' + url + '\nStatus: ' + req.status.toString(); + } else if (JSON.parse(req.responseText).jquery && JSON.parse(req.responseText).jquery[3][3][0] === '.error.USER_REQUIRED') { + console.warn(req); + throw 'Error doing API action.\nURL: ' + url + '\nUser not logged in.'; + } + + return JSON.parse(req.responseText); +}; + + /** * Grabs info about a URL via the reddit API and caches it. * @alias RedditAPI.getInfo(url) @@ -209,9 +243,8 @@ RedditAPI.prototype.getInfo = function (url) { reqUrl = 'http://' + this.domain + '/api/info.json?url=' + encodeURI(url); } - req.open('GET', reqUrl, false); - req.send(null); response = this.apiTransmit('GET', reqUrl, false); + settings.set('modhash', response.data.modhash); postsObj = {}; postCount = 0; @@ -235,6 +268,47 @@ RedditAPI.prototype.getInfo = function (url) { return true; }; +/** + * Votes a thing up. + * @alias RedditAPI.voteUp(event) + * @param {String} thing The FULLNAME of the thing to vote up. + * @return {Boolean} Returns true. + * @method + */ +RedditAPI.prototype.voteUp = function (e) { + var listItem, fullName, url, reqUrl, oldCache, voteWas, formData; + + listItem = e.srcElement.parentNode.parentNode; + fullName = listItem.id; + voteWas = listItem.getAttribute('data-dir'); + url = listItem.parentNode.getAttribute('data-url'); + console.log(fullName); + console.log(url); + reqUrl = 'http://' + this.domain + '/api/vote'; + oldCache = cache.get(url); + console.log(oldCache.posts[fullName]); + formData = new FormData(); + formData.append('id', fullName); + formData.append('uh', settings.get('modhash')); + + if (voteWas === '1') { + formData.append('dir','0'); + listItem.setAttribute('data-dir','0'); + oldCache.posts[fullName].likes = null; + } else if (voteWas === '0') { + formData.append('dir','1'); + listItem.setAttribute('data-dir','1'); + oldCache.posts[fullName].likes = true; + } else if (voteWas === '-1') { + formData.append('dir','1'); + listItem.setAttribute('data-dir','1'); + oldCache.posts[fullName].likes = true; + } + + cache.set(url, oldCache); + this.apiTransmit('POST', reqUrl, false, formData); +}; + reddit = new RedditAPI('www.reddit.com'); /** @@ -318,9 +392,9 @@ Popup.prototype.createListHTML = function (url) { listHTML += '
  • '; listHTML += '
    '; - listHTML += ''; + listHTML += ''; listHTML += '' + data.score + ''; - listHTML += ''; + listHTML += ''; listHTML += '
    '; listHTML += ''; listHTML += '' + data.title + '';