twister HTML + Javascript User Interface
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

539 lines
18 KiB

11 years ago
// mobile_abstract.js
// 2013 Miguel Freitas
//
// Mobile Abstration Layer
// Try to sort out lowlevel differences between jquery and jquery mobile twister interfaces
var MAL = function()
{
this.postboardLoading = function() {
if( $.hasOwnProperty("mobile") ) {
$.mobile.showPageLoadingMsg();
/*
setTimeout(function(){
$.mobile.hidePageLoadingMsg();
}, 10 * 1000);
*/
} else {
$(".postboard-loading").show();
}
}
this.postboardLoaded = function() {
if( $.hasOwnProperty("mobile") ) {
$.mobile.hidePageLoadingMsg();
var curPage = $.mobile.activePage.attr("id");
$( '#'+curPage+' .content ul.posts').listview('refresh');
installPostboardClick();
} else {
$(".postboard-loading").hide();
}
}
this.dmThreadListLoaded = function() {
if( $.hasOwnProperty("mobile") ) {
$.mobile.hidePageLoadingMsg();
var $dmThreadList = $("#directmsg ul.direct-messages-thread");
$dmThreadList.listview('refresh');
} else {
}
}
this.dmChatListLoaded = function(dmConvo) {
if( $.hasOwnProperty("mobile") ) {
$.mobile.hidePageLoadingMsg();
var $dmChatList = $("#dmchat ul.direct-messages-list");
$dmChatList.listview('refresh');
$.mobile.silentScroll( $(".dm-form").offset().top );
} else {
var modalContent = dmConvo.parents(".modal-content");
modalContent.scrollTop(modalContent[0].scrollHeight);
}
}
this.relatedPostLoaded = function() {
if( $.hasOwnProperty("mobile") ) {
$.mobile.hidePageLoadingMsg();
var curPage = $.mobile.activePage.attr("id");
$( '#'+curPage+' .content ul.posts').listview('refresh');
} else {
$(".postboard-loading").hide();
}
}
this.followingListLoaded = function() {
if( $.hasOwnProperty("mobile") ) {
$.mobile.hidePageLoadingMsg();
$(".following-list").listview('refresh');
} else {
$(".postboard-loading").hide();
}
}
this.searchUserListLoaded = function() {
if( $.hasOwnProperty("mobile") ) {
$.mobile.hidePageLoadingMsg();
$(".userMenu-search-profiles").listview('refresh');
} else {
var $searchResultsModal = $( ".search-results" );
$searchResultsModal.slideDown( "fast" );
}
}
this.reportNewPosts = function(newPosts) {
if( $.hasOwnProperty("mobile") ) {
var newTweetsBar = $(".timeline-refresh .ui-btn-text");
if( newPosts ) {
document.title = "(" + String(newPosts) + ") twister";
newTweetsBar.text("(" + String(newPosts) + ")");
} else {
document.title = "twister";
newTweetsBar.text("Refresh");
}
} else {
var newTweetsBar = $(".wrapper").find(".postboard-news");
var newTweetsBarMenu = $(".userMenu").find(".menu-news"); // added for home menu entry
11 years ago
if( newPosts ) {
document.title = "(" + String(newPosts) + ") twister";
10 years ago
newTweetsBar.text(polyglot.t("new_posts", newPosts));
11 years ago
newTweetsBar.fadeIn("slow");
newTweetsBarMenu.text(String(newPosts));
newTweetsBarMenu.addClass("show");
if ($.Options.getShowDesktopNotifPostsOpt() === 'enable') {
this.showDesktopNotif(false, polyglot.t('You got')+' '+polyglot.t('new_posts', newPosts)+' '+polyglot.t('in postboard')+'.', false,'twister_notification_new_posts', $.Options.getShowDesktopNotifPostsTimerOpt(), (function() {
requestTimelineUpdate('pending',this,followingUsers,promotedPostsOnly);
}).bind(newPosts), false)
}
11 years ago
} else {
newTweetsBar.hide();
newTweetsBar.text("");
newTweetsBarMenu.text("");
newTweetsBarMenu.removeClass("show");
11 years ago
document.title = "twister";
}
}
}
this.getStreamPostsParent = function() {
if( $.hasOwnProperty("mobile") ) {
return $( '.timeline ul');
} else {
return $("#posts");
}
}
this.setPostTemplate = function(p) {
if( $.hasOwnProperty("mobile") ) {
this.postTemplate = p;
}
}
this.getPostTemplate = function() {
if( $.hasOwnProperty("mobile") ) {
return this.postTemplate;
} else {
return $("#post-template");
}
}
// how do we map usernames into urls
this.userUrl = function(username) {
if( $.hasOwnProperty("mobile") ) {
return "#profile?user=" + username;
} else {
return "#profile?user=" + username;
}
}
// recover username from url (only for hash)
this.urlToUser = function(url) {
var dummyUrl = this.userUrl("");
var urlIdx = url.indexOf(dummyUrl);
if( urlIdx >= 0 )
return url.substr(urlIdx + dummyUrl.length);
else
return "";
}
this.followingUrlToUser = function(url) {
var dummyUrl = this.followingUrl("");
var urlIdx = url.indexOf(dummyUrl);
if( urlIdx >= 0 )
return url.substr(urlIdx + dummyUrl.length);
else
return "";
}
11 years ago
this.mentionsUrl = function(username) {
if( $.hasOwnProperty("mobile") ) {
return "#mentions?user=" + username;
} else {
return "#mentions?user=" + username;
}
}
this.hashtagUrl = function(h) {
if( $.hasOwnProperty("mobile") ) {
return "#hashtag?hashtag=" + h;
} else {
return "#hashtag?hashtag=" + h;
}
}
this.dmchatUrl = function(username) {
if( $.hasOwnProperty("mobile") ) {
return "#dmchat?user=" + username;
} else {
return "#directmessages?user=" + username;
11 years ago
}
}
this.followingUrl = function(username) {
if( $.hasOwnProperty("mobile") ) {
return "#following?user=" + username;
} else {
return "#following?user=" + username;
}
}
11 years ago
this.followUrl = function(username) {
if( $.hasOwnProperty("mobile") ) {
return "#following?follow=" + username;
} else {
return "#following?follow=" + username;
}
}
this.unfollowUrl = function(username) {
if( $.hasOwnProperty("mobile") ) {
return "#following?unfollow=" + username;
} else {
return "#following?unfollow=" + username;
}
}
this.newPostToUrl = function(username) {
if( $.hasOwnProperty("mobile") ) {
return "#newmsg?replyto=" + encodeURIComponent("@"+username);
} else {
return "#newmsg?replyto=" + encodeURIComponent("@"+username);
}
}
this.newPostHashtagToUrl = function(hashtag) {
if( $.hasOwnProperty("mobile") ) {
return "#newmsg?replyto=" + encodeURIComponent("#"+hashtag);
} else {
return "#newmsg?replyto=" + encodeURIComponent("#"+hashtag);
}
}
this.updateNewMentionsUI = function(newMentions) {
if( $.hasOwnProperty("mobile") ) {
var $mentionsCounterBtnText = $(".mentions-count .ui-btn-text");
if( newMentions ) {
$mentionsCounterBtnText.text("@ (" + newMentions + ")");
} else {
$mentionsCounterBtnText.text("@");
}
} else {
var $mentionsCounter = $(".userMenu-connections .messages-qtd");
if( newMentions ) {
$mentionsCounter.text(newMentions);
$mentionsCounter.fadeIn();
} else {
$mentionsCounter.hide();
}
}
}
this.updateNewDMsUI = function(newDMs) {
if( $.hasOwnProperty("mobile") ) {
var $DMsCounterAllPages = $(".newdms-count");
for( var i = 0; i < $DMsCounterAllPages.length; i++) {
var $textElement = $DMsCounterAllPages.eq(i).find(".ui-btn-text");
if( !$textElement.length ) $textElement = $DMsCounterAllPages.eq(i);
if( newDMs ) {
$textElement.text("Direct Msg (" + newDMs + ")");
} else {
$textElement.text("Direct Msg");
}
}
} else {
var $DMsCounter = $(".userMenu-messages .messages-qtd");
if( newDMs ) {
$DMsCounter.text(newDMs);
$DMsCounter.fadeIn();
} else {
$DMsCounter.hide();
}
}
}
this.updateMyOwnPostCount = function(numPosts) {
if( $.hasOwnProperty("mobile") ) {
console.log("FIXME: implement MAL_updateMyOwnPostCount");
} else {
$(".mini-profile .posts-count").text(numPosts);
}
}
this.goHome = function(clearTimeline) {
if( $.hasOwnProperty("mobile") ) {
$.mobile.navigate( "#home" );
} else {
window.location.href = "home.html";
}
}
this.goLogin = function() {
if( $.hasOwnProperty("mobile") ) {
$.mobile.navigate( "#login" );
} else {
window.location.href = "login.html";
}
}
this.goNetwork = function() {
if( $.hasOwnProperty("mobile") ) {
$.mobile.navigate( "#network" );
} else {
window.location.href = "network.html";
}
}
this.goProfileEdit = function() {
if( $.hasOwnProperty("mobile") ) {
$.mobile.navigate( "#profile-edit" );
} else {
window.location.href = "profile-edit.html";
}
}
this.showMentions = function(username) {
if( $.hasOwnProperty("mobile") ) {
$.mobile.navigate( this.mentionsUrl(username) );
} else {
if ($(".postboard").length) {
openMentionsModal();
} else {
window.location.href = 'home.html'+this.mentionsUrl(username);
}
}
}
this.showDMchat = function(username) {
if (username) {
if( $.hasOwnProperty("mobile") ) {
$.mobile.navigate( this.dmchatUrl(username) );
} else {
if ($(".postboard").length) {
window.location.hash = this.dmchatUrl(username);
} else {
window.location.href = 'home.html'+this.dmchatUrl(username);
}
}
} else {
if( $.hasOwnProperty("mobile") ) {
$.mobile.navigate( '#directmsg' );
} else {
if ($(".postboard").length) {
window.location.hash = '#directmessages';
} else {
window.location.href = 'home.html#directmessages';
}
}
}
}
11 years ago
this.setNetworkStatusMsg = function(msg, statusGood) {
if( $.hasOwnProperty("mobile") ) {
$(".network-status").text(msg);
if( statusGood ) {
$(".connection-status").addClass("connected");
} else {
$(".connection-status").removeClass("connected");
}
} else {
$(".network-status").text(msg);
if( statusGood ) {
$(".connection-status").addClass("connected");
} else {
$(".connection-status").removeClass("connected");
}
}
}
this.enableButton = function( $button ) {
if( $.hasOwnProperty("mobile") ) {
$button.button("enable");
} else {
$button.removeClass("disabled");
$button.removeAttr("disabled");
}
}
this.disableButton = function( $button ) {
if( $.hasOwnProperty("mobile") ) {
$button.button("disable");
} else {
$button.addClass("disabled");
$button.attr("disabled","true");
}
}
this.changedUser = function() {
if( $.hasOwnProperty("mobile") ) {
timelineChangedUser();
$("#home .posts").html("");
newmsgsChangedUser();
followingChangedUser();
twisterInitialized = false;
} else {
window.location.href = "home.html";
}
}
this.soundNotifyMentions = function() {
if( $.hasOwnProperty("mobile") ) {
} else {
$.Options.mensNotif();
}
}
this.soundNotifyDM = function() {
if( $.hasOwnProperty("mobile") ) {
} else {
$.Options.DMsNotif();
}
}
this.showDesktopNotif = function(notifyTitle, notifyBody, notifyIcon, notifyTag, notifyTimer, actionOnClick, actionOnPermDenied) {
function doNotification() {
if (!notifyTitle) {
notifyTitle = polyglot.t('notify_desktop_title');
}
if (!notifyIcon) {
notifyIcon = '../img/twister_mini.png';
}
if (!notifyTag) {
notifyTag = 'twister_notification';
}
if (!notifyTimer) {
notifyTimer = 3600 * 24 * 30; // one month
}
var doActionOnClick = false;
if (typeof actionOnClick === 'function') {
doActionOnClick = function() {
actionOnClick();
window.focus();
}
}
var desktopNotification = new Notify(notifyTitle, {
body: notifyBody,
icon: notifyIcon,
tag: notifyTag,
timeout: notifyTimer,
notifyClick: doActionOnClick,
notifyError: function() { alert(polyglot.t('notify_desktop_error')) }
});
desktopNotification.show();
}
if (Notify.needsPermission) {
Notify.requestPermission(false, actionOnPermDenied);
} else {
doNotification();
}
}
this.reqRepAfterCB = function(postLi, postsFromJson) {
if ($.hasOwnProperty("mobile")) {
for( var i = 0; i < postsFromJson.length; i++) {
var newStreamPost = postToElem(postsFromJson[i], "related");
newStreamPost.hide();
postLi.after(newStreamPost);
newStreamPost.slideDown("fast");
}
} else {
var $replist = $('<ol class="sub-replies"></ol>');
var $newli = $('<li class="post-replies"></li>');
postLi.after($newli);
$newli.append($replist);
for (var i = 0; i < postsFromJson.length && $.MAL.getExpandedPostsCount(postLi) < maxExpandPost; i++) {
var newStreamPost = postToElem(postsFromJson[i], "related");
newStreamPost.hide();
$replist.prepend(newStreamPost);
newStreamPost.slideDown("fast");
requestRepliesAfter(newStreamPost);
}
}
$.MAL.relatedPostLoaded();
};
this.getExpandedPostsCount = function(postLi) {
if ($.hasOwnProperty('mobile')) {
return postLi.siblings().length;
} else {
return postLi.parents('.module.post.original.open').find('.post.related').length;
}
};
11 years ago
}
jQuery.MAL = new MAL;
function filterLangPost(post) {
if ($.Options.getFilterLangOpt() !== 'disable' && $.Options.getFilterLangListOpt().length > 0) {
var langFilterAccuracy = $.Options.getFilterLangAccuracyOpt();
var langFilterList = $.Options.getFilterLangListOpt();
var langFilterSubj = '';
var langFilterProb = [];
var langFilterPass = ($.Options.getFilterLangOpt() === 'whitelist') ? false : true;
var langFilterReason = 'language isn\'t in '+$.Options.getFilterLangOpt();
if (typeof(post['userpost']['rt']) !== 'undefined') {
langFilterSubj = post['userpost']['rt']['msg'].replace(/\@\S\w*|https?:\/\/\S*|\#/g, '').replace(/\s+/g, ' ');
} else {
langFilterSubj = post['userpost']['msg'].replace(/\@\S\w*|https?:\/\/\S*|\#/g, '').replace(/\s+/g, ' ');
}
langFilterProb = franc.all(langFilterSubj, {'minLength': 5}); // FIXME minLength may be optional sometimes
for (var i = 0; i < langFilterProb.length; i++) {
if (langFilterProb[i][1] > langFilterAccuracy) {
if (langFilterProb[i][0] === 'und') { // FIXME maybe there's should be an option for whitelist mode to allow this
langFilterPass = true;
langFilterReason = 'it\'s undefined language';
break;
} else if (langFilterProb[i][0] === 'cmn') {
langFilterPass = true;
langFilterReason = 'it\'s common language';
break;
} else if (langFilterList.indexOf(langFilterProb[i][0]) > -1) {
if ($.Options.getFilterLangOpt() === 'whitelist') {
langFilterPass = true;
langFilterReason = 'it\'s '+langFilterProb[i][0]+', whitelisted';
break;
} else {
langFilterPass = false;
langFilterReason = 'it\'s '+langFilterProb[i][0]+', blacklisted';
break;
}
}
}
}
post['langFilter'] = {};
post['langFilter']['subj'] = langFilterSubj;
post['langFilter']['prob'] = langFilterProb;
post['langFilter']['pass'] = langFilterPass;
post['langFilter']['reason'] = langFilterReason;
return ($.Options.getFilterLangSimulateOpt()) ? true : langFilterPass;
}
}