Created
March 27, 2016 13:55
-
-
Save katsuhide/56c346da16f659261ed5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
"use strict"; | |
var Item, ItemRepository, User, addOnloadHandler, corsRequest, getElementsByClassName, main, setIframeHeight, setInnerText, template; | |
Item = (function() { | |
function Item(item) { | |
var tag, _i, _len, _ref; | |
this.title = item.title; | |
this.url = item.url; | |
this.tags = []; | |
_ref = item.tags; | |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | |
tag = _ref[_i]; | |
this.tags.push({ | |
name: tag.name, | |
url: "http://qiita.com/tags/" + tag.name | |
}); | |
} | |
this.user = new User(item.user); | |
} | |
return Item; | |
})(); | |
ItemRepository = (function() { | |
ItemRepository.prototype.itemContainer = {}; | |
function ItemRepository() {} | |
ItemRepository.prototype.findByUsername = function(username, callback) { | |
if (username in this.itemContainer) { | |
callback(this.itemContainer[username]); | |
return; | |
} | |
return corsRequest("https://qiita.com/api/v1/users/" + username + "/items?per_page=5", (function(_this) { | |
return function(rows) { | |
var row, _i, _len; | |
_this.itemContainer[username] = []; | |
for (_i = 0, _len = rows.length; _i < _len; _i++) { | |
row = rows[_i]; | |
_this.itemContainer[username].push(new Item(row)); | |
} | |
return callback(_this.itemContainer[username]); | |
}; | |
})(this)); | |
}; | |
return ItemRepository; | |
})(); | |
User = (function() { | |
function User(user) { | |
this.name = user.url_name; | |
this.profileImageUrl = user.profile_image_url; | |
this.url = "http://qiita.com/users/" + this.name; | |
} | |
return User; | |
})(); | |
corsRequest = function(url, callback) { | |
var method, request; | |
method = 'get'; | |
request = new XMLHttpRequest(); | |
if ("withCredentials" in request) { | |
request.open(method, url, true); | |
} else if (typeof XDomainRequest !== "undefined") { | |
request = new XDomainRequest(); | |
request.open(method, url); | |
} else { | |
throw "Failed to initialize CORSRequest"; | |
} | |
request.onload = (function(_this) { | |
return function() { | |
var result; | |
result = JSON.parse(request.response); | |
if (request.status < 200 || 300 <= request.status) { | |
throw result.error; | |
} | |
return callback(result); | |
}; | |
})(this); | |
return request.send(); | |
}; | |
getElementsByClassName = function(oElm, strTagName, strClassName) { | |
var arrElements, arrReturnElements, i, oElement, oRegExp, _i, _ref; | |
if (strTagName === "*" && oElm.all) { | |
arrElements = oElm.all; | |
} else { | |
arrElements = oElm.getElementsByTagName(strTagName); | |
} | |
arrReturnElements = []; | |
strClassName = strClassName.replace(/\-/g, "\\-"); | |
oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)"); | |
oElement; | |
for (i = _i = 0, _ref = arrElements.length; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { | |
oElement = arrElements[i]; | |
if (oElement && oRegExp.test(oElement.className)) { | |
arrReturnElements.push(oElement); | |
} | |
} | |
return arrReturnElements; | |
}; | |
addOnloadHandler = function(newFunction) { | |
if (window.addEventListener) { | |
return window.addEventListener('load', newFunction, false); | |
} else if (window.attachEvent) { | |
return window.attachEvent('onload', newFunction); | |
} | |
}; | |
setIframeHeight = function(iframe) { | |
var iframeWin; | |
if (iframe) { | |
iframeWin = iframe.contentWindow || iframe.contentDocument.parentWindow; | |
if (iframeWin.document.body) { | |
return iframe.height = iframeWin.document.documentElement.scrollHeight || iframeWin.document.body.scrollHeight; | |
} | |
} | |
}; | |
setInnerText = function(element, text) { | |
if (typeof element.textContent !== "undefined") { | |
return element.textContent = text; | |
} else { | |
return element.innerText = text; | |
} | |
}; | |
template = "<!DOCTYPE html>\n<html lang=\"ja\">\n<head>\n<meta charset=\"utf-8\" />\n<style type=\"text/css\">\nbody{padding:0;margin:0;font-family:'Trebuchet MS','Arial','Helvetica','ヒラギノ角ゴ Pro W3','Hiragino Kaku Gothic Pro','メイリオ',Meiryo,'MS Pゴシック','MS PGothic',sans-serif;background:#fff}.bar{height:30px;overflow:hidden;line-height:30px;text-overflow:ellipsis;white-space:nowrap;vertical-align:middle;background:-webkit-linear-gradient(top,#313a29,#21291a);background:-moz-linear-gradient(top,#313a29,#21291a);background:-ms-linear-gradient(top,#313a29,#21291a);background:-o-linear-gradient(top,#313a29,#21291a);background:linear-gradient(top,#313a29,#21291a);border-bottom:3px solid #59bb0c}a.user{font-size:14px;color:#fff;text-decoration:none}a.user:hover{text-decoration:underline}.avatar{width:14px;height:14px;margin-right:3px}.title{color:#117ec6;text-decoration:none;word-wrap:break-word}.title:hover{text-decoration:underline}.tag{display:inline-block;height:14px;padding:0 3px;margin:0 2px;font-size:10px;line-height:14px;color:#fff;text-decoration:none;vertical-align:middle;background-color:#59bb0c;border-radius:3px}.tag:hover{text-decoration:underline}.item{padding:5px 15px;font-size:14px;line-height:18px;border-bottom:1px solid #ddd}\n</style>\n</head>\n<body>\n<div class=\"items\" id=\"items\"></div>\n</body>\n</html>"; | |
main = function() { | |
var widgets; | |
widgets = getElementsByClassName(document, 'a', 'qiita-timeline'); | |
return addOnloadHandler(function() { | |
var doc, iframe, itemRepository, itemsBlock, username, widget, _i, _len, _results; | |
_results = []; | |
for (_i = 0, _len = widgets.length; _i < _len; _i++) { | |
widget = widgets[_i]; | |
username = widget.getAttribute('data-qiita-username'); | |
iframe = document.createElement('iframe'); | |
iframe.style.display = 'none'; | |
iframe.setAttribute("frameBorder", "0"); | |
iframe.style.width = '100%'; | |
widget.parentNode.appendChild(iframe); | |
widget.style.display = 'none'; | |
doc = frames[frames.length - 1].document; | |
doc.open(); | |
doc.write(template); | |
doc.close(); | |
itemsBlock = doc.getElementById('items'); | |
itemRepository = new ItemRepository(); | |
_results.push(itemRepository.findByUsername(username, function(items) { | |
var item, itemElement, tag, tagElement, title, _j, _k, _len1, _len2, _ref; | |
for (_j = 0, _len1 = items.length; _j < _len1; _j++) { | |
item = items[_j]; | |
itemElement = document.createElement('div'); | |
itemElement.setAttribute('class', 'item'); | |
title = document.createElement('a'); | |
setInnerText(title, item.title); | |
title.setAttribute('href', item.url); | |
title.setAttribute('class', 'title'); | |
title.setAttribute('target', '_blank'); | |
itemElement.appendChild(title); | |
_ref = item.tags; | |
for (_k = 0, _len2 = _ref.length; _k < _len2; _k++) { | |
tag = _ref[_k]; | |
tagElement = document.createElement('a'); | |
setInnerText(tagElement, tag.name); | |
tagElement.setAttribute('href', tag.url); | |
tagElement.setAttribute('class', 'tag'); | |
tagElement.setAttribute('target', '_blank'); | |
itemElement.appendChild(tagElement); | |
} | |
itemsBlock.appendChild(itemElement); | |
} | |
iframe.style.display = 'block'; | |
return setIframeHeight(iframe); | |
})); | |
} | |
return _results; | |
}); | |
}; | |
main(); | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment