define([
"jquery",
"helper/APICalls",
"lib/URI",
"helper/HostApp",
"lib/Timeago",
"lib/SingleDoubleClick"
],
function(jQuery, APICalls, URI, HostApp) {
function Core() {
this.saveScrollTop = 0;
}
Core.prototype.show = function(container) {
if (container) {
$(container).show();
document.body.scrollTop = this.saveScrollTop;
}
}
Core.prototype.hide = function(container) {
if (container && $(container).is(":visible")) {
this.saveScrollTop = document.body.scrollTop;
$(container).hide();
}
}
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(); };
image.onerror = function() { this.src = 'img/default-avatar.png' };
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) {
/*
{
"app": {
"id": "P8FJjaiRv0AKXfjUMd_4YQ",
"name": "Bungloo on Linux",
"url": "http:\/\/jabs.nu\/bungloo\/"
},
"content": {
"text": "jeena test"
},
"entity": "http:\/\/155969d81672.alpha.attic.is",
"id": "HlSXe8MREzU4h2fGLGSnCA",
"published_at": 1369566009,
"received_at": 1369566008799,
"type": "https:\/\/tent.io\/types\/status\/v0#",
"version": {
"id": "a2f702b4615c7d7dd0f98c73d7b55749880bf6e437a77349454ff10745d134c6",
"published_at": 1369566009,
"received_at": 1369566008799
}
}
*/
var _this = this;
var template = this.getTemplate();
template.item.id = "post-" + status.id + "-" + this.action;
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";
}
if (HostApp.stringForKey("entity") == status.entity) {
template.item.className += " own";
}
template.reply_to.onclick = function() {
var mentions = [];
var status_mentions = [];
if(status.mentions) 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);
return false;
}
template.repost.onclick = function() {
$(template.repost).hide();
_this.repost(status.id, status.entity);
return false;
}
if(bungloo.cache.profiles[status.entity].name) template.username.innerText = bungloo.cache.profiles[status.entity].name;
else 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;
}
if(bungloo.cache.profiles[status.entity].avatar_digest) {
template.image.src = HostApp.serverUrl("attachment").replace(/\{entity\}/, encodeURIComponent(status.entity)).replace(/\{digest\}/, bungloo.cache.profiles[status.entity].avatar_digest);
}
template.image.onclick = template.username.onclick;
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];
if (status.mentions) {
status.mentions.map(function (mention) {
entities.push(mention.entity)
});
}
template.message.innerHTML = this.replaceURLWithHTMLLinks(text, entities, template.message);
this.afterChangingTextinMessageHTML(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 = APICalls.mkApiRootPath("/posts/" + status.id + "/attachments/" + attachment.name);
APICalls.http_call(url, "GET", callback, null, null, attachment.type);
} else {
var url = APICalls.mkApiRootPath("/posts/" + encodeURIComponent(status.entity) + "/" + status.id + "/attachments/" + attachment.name);
APICalls.http_call(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.version.published_at : status.__repost.published_at;
var time = document.createElement("abbr");
time.innerText = this.ISODateString(new Date(published_at));
time.title = time.innerText;
time.className = "timeago";
jQuery(time).timeago();
template.ago.appendChild(time);
template.ago.href = "#"
$(template.ago).single_double_click(function () {
HostApp.showConversation(status.id, status.entity);
return false;
}, function () {
HostApp.showConversationViewForPostIdandEntity(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];
if (typeof lat != "undefined" && typeof lng != "undefined" && lat > 0 && lng > 0) {
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 {
if(status.app) {
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, append) {
var post = document.getElementById("post-" + repost.content.id + "-" + this.action);
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 = $("
$1
")
.replace(/~([^~]+)~/g, "