define([
"jquery",
"helper/Paths",
"lib/URI",
"helper/HostApp",
"helper/Cache",
"lib/Timeago"
],
function(jQuery, Paths, URI, HostApp, Cache) {
function Core() {
this.cache = new Cache();
}
Core.prototype.getTemplate = function() {
if(this.template == "undefined") {
return jQuery.extend(true, {}, this.template);
}
var a = document.createElement("a");
var item = document.createElement("li");
var aside = document.createElement("aside");
item.appendChild(aside);
var ago = a.cloneNode();
ago.className = "ago";
aside.appendChild(ago);
var reply_to = a.cloneNode();
reply_to.className = "reply_to"
reply_to.innerText = " ";
reply_to.href = "#";
aside.appendChild(reply_to);
var repost = a.cloneNode();
repost.className = "repost";
repost.innerText = " ";
repost.href = "#";
aside.appendChild(repost);
var remove = a.cloneNode();
remove.className = "remove";
remove.innerText = " ";
remove.href = "#";
aside.appendChild(remove);
var image = document.createElement("img");
image.className = "image";
image.src = "img/default-avatar.png";
image.onmousedown = function(e) { e.preventDefault(); };
item.appendChild(image);
var image_username = a.cloneNode();
image.appendChild(image_username);
var data = document.createElement("div");
data.className = "data";
item.appendChild(data);
var head = document.createElement("h1");
data.appendChild(head);
var username = a.cloneNode();
head.appendChild(username);
var space = document.createTextNode(" ");
head.appendChild(space);
var geo = document.createElement("a");
geo.style.display = "none";
head.appendChild(geo);
head.appendChild(space.cloneNode());
var is_private = document.createElement("span")
is_private.className = "is_private";
is_private.style.display = "none";
is_private.innerHTML = "P";
is_private.title = "Private";
head.appendChild(is_private);
head.appendChild(space.cloneNode());
var pin = document.createElement("img");
pin.src = "img/pin.png";
pin.alt = "Map link";
geo.appendChild(pin);
head.appendChild(space.cloneNode());
var reposted_by = document.createElement("span");
reposted_by.className = "reposted_by";
reposted_by.style.display = "none";
var reposted_count = document.createElement("span");
reposted_count.innerText = "by 0 people";
reposted_by.appendChild(reposted_count)
var reposted_list = document.createElement("ul");
reposted_list.className = "reposted_list";
reposted_by.appendChild(reposted_list);
head.appendChild(reposted_by)
var message = document.createElement("p");
message.className = "message";
data.appendChild(message);
var images = document.createElement("p")
images.className = "images";
data.appendChild(images);
var from = message.cloneNode();
from.className = "from";
data.appendChild(from);
var from_text = document.createTextNode("from ");
from.appendChild(from_text)
var source = document.createElement("a");
source.className = "source";
from.appendChild(source)
this.template = {
item: item,
reply_to: reply_to,
is_private: is_private,
image: image,
username: username,
repost: repost,
reposted_by: reposted_by,
message: message,
ago: ago,
source: source,
geo: geo,
images: images,
remove: remove
}
return jQuery.extend(true, {}, this.template);;
}
Core.prototype.getStatusDOMElement = function(status) {
var _this = this;
var template = this.getTemplate();
template.item.id = "post-" + status.id;
template.item.status = status;
if (HostApp.stringForKey("entity") == status.entity && typeof status.__repost == "undefined") {
template.remove.onclick = function() {
_this.remove(status.id);
return false;
}
} else if (typeof status.__repost != "undefined" && HostApp.stringForKey("entity") == status.__repost.entity) {
template.remove.onclick = function() {
_this.remove(status.__repost.id, null, "repost");
return false;
}
} else {
template.remove.style.display = "none";
}
template.reply_to.onclick = function() {
var mentions = [];
var status_mentions = status.mentions.slice(0);
if (typeof status.__repost != "undefined") {
status_mentions.push({entity:status.__repost.entity});
}
for (var i = 0; i < status_mentions.length; i++) {
var mention = status_mentions[i];
if(mention.entity != HostApp.stringForKey("entity"))
mentions.push(mention);
}
_this.replyTo(status.entity, status.id, mentions, (status && status.permissions && !status.permissions.public));
return false;
}
template.repost.onclick = function() {
$(template.repost).hide();
_this.repost(status.id, status.entity);
return false;
}
template.username.innerText = status.entity;
template.username.href = status.entity;
template.username.title = status.entity;
template.username.onclick = function() {
HostApp.showProfileForEntity(status.entity);
return false;
}
template.image.onclick = template.username.onclick;
var profile_callback = function(p) {
var basic = p["https://tent.io/types/info/basic/v0.1.0"];
if (p && basic) {
if(basic.name) {
template.username.title = template.username.innerText;
template.username.innerText = basic.name;
}
if(basic.avatar_url) {
template.image.onerror = function() { template.image.src = 'img/default-avatar.png' };
template.image.src = basic.avatar_url;
}
}
}
var p = this.cache.profiles.getItem(status.entity);
if (p && p != "null") {
profile_callback(p);
} else {
Paths.findProfileURL(status.entity, function(profile_url) {
if (profile_url) {
Paths.getURL(profile_url, "GET", function(resp) {
var p = JSON.parse(resp.responseText);
if (p && p != "null") {
_this.cache.profiles.setItem(status.entity, p);
profile_callback(p);
}
}, null, false); // do not send auth-headers
}
});
}
if (status && status.permissions && !status.permissions.public) {
template.is_private.style.display = '';
}
var text = "";
if (status.type == "https://tent.io/types/post/photo/v0.1.0") {
text = status.content.caption;
} else {
if (status.content && status.content.text) {
text = status.content.text;
}
}
this.parseForMedia(text, template.images);
text = text.escapeHTML().replace(/\n/g, "
");
var entities = [status.entity];
status.mentions.map(function (mention) {
entities.push(mention.entity)
});
template.message.innerHTML = this.replaceURLWithHTMLLinks(text, entities, template.message);
if (status.type == "https://tent.io/types/post/photo/v0.1.0") {
for (var i = 0; i < status.attachments.length; i++) {
// closure needed for the callback
(function() {
var attachment = status.attachments[i];
var img = new Image();
img.className = "photo";
template.images.appendChild(img);
var callback = function(resp) {
img.src = "data:" + attachment.type + ";base64," + resp.responseText;
}
if (status.entity == HostApp.stringForKey("entity")) {
var url = Paths.mkApiRootPath("/posts/" + status.id + "/attachments/" + attachment.name);
Paths.getURL(url, "GET", callback, null, null, attachment.type);
} else {
var url = Paths.mkApiRootPath("/posts/" + encodeURIComponent(status.entity) + "/" + status.id + "/attachments/" + attachment.name);
Paths.getURL(url, "GET", callback, null, null, attachment.type);
}
})();
}
}
this.findMentions(template.message, status.mentions);
for (var i = 0; i < status.mentions.length; i++) {
var mention = status.mentions[i];
if (mention.entity == HostApp.stringForKey("entity")) {
this.template.item.className = "mentioned";
break;
}
}
var published_at = typeof status.__repost == "undefined" ? status.published_at : status.__repost.published_at;
var time = document.createElement("abbr");
time.innerText = this.ISODateString(new Date(published_at * 1000));
time.title = time.innerText;
time.className = "timeago";
jQuery(time).timeago();
template.ago.appendChild(time);
template.ago.href = "#"
template.ago.onclick = function() {
HostApp.showConversation(status.id, status.entity);
return false;
}
// {"type":"Point","coordinates":[57.10803113,12.25854746]}
if (status.content && status.content.location && (typeof status.content.location.type == "undefined" || status.content.location.type == "Point")) {
var lat = status.content.location.coordinates[0];
var lng = status.content.location.coordinates[1];
var href = this.mapHref(lat, lng);
template.geo.href = href;
template.geo.style.display = "";
this.addMap(lat, lng, template.images);
}
if (typeof status.__repost != "undefined") {
template.source.href = status.__repost.app.url;
template.source.innerHTML = status.__repost.app.name;
template.source.title = status.__repost.app.url;
} else {
template.source.href = status.app.url;
template.source.innerHTML = status.app.name;
template.source.title = status.app.url;
}
return template.item;
}
Core.prototype.getRepost = function(repost, before_node) {
var post = document.getElementById("post-" + repost.content.id);
if (post) {
if (repost.entity == HostApp.stringForKey("entity")) {
var remove = $(post).find(".remove");
remove.show();
var _this = this;
remove.get(0).onclick = function(e) {
var callback = function() {
// make remove button to remove post if it is the entities
if(post.status.entity == HostApp.stringForKey("entity")) {
remove.get(0).onclick = function(e) {
_this.remove(post.status.id);
return false;
}
} else {
remove.hide();
}
$(post).find(".repost").show();
}
_this.remove(repost.id, callback, "repost");
return false;
};
}
var reposted_count = $(post).find(".reposted_by ul li").length + 1;
var people_person = reposted_count == 1 ? "person" : "people";
$(post).find(".reposted_by span").html("by " + reposted_count + " " + people_person);
$(post).find(".reposted_by").show();
var li = $("