From cc1faa63ec8926afb5d01e3a00936c4096a86819 Mon Sep 17 00:00:00 2001 From: augier Date: Fri, 11 Sep 2015 21:10:58 +0200 Subject: [PATCH] Mobile JS refactor --- app/assets/javascripts/mobile/mobile.js | 113 +---------------- .../javascripts/mobile/mobile_application.js | 17 +++ .../javascripts/mobile/mobile_comments.js | 8 -- .../javascripts/mobile/mobile_drawer.js | 28 +++++ .../javascripts/mobile/mobile_post_actions.js | 114 ++++++++++++++++++ 5 files changed, 162 insertions(+), 118 deletions(-) create mode 100644 app/assets/javascripts/mobile/mobile_application.js create mode 100644 app/assets/javascripts/mobile/mobile_drawer.js create mode 100644 app/assets/javascripts/mobile/mobile_post_actions.js diff --git a/app/assets/javascripts/mobile/mobile.js b/app/assets/javascripts/mobile/mobile.js index 286e1b3e7..a55d2faf1 100644 --- a/app/assets/javascripts/mobile/mobile.js +++ b/app/assets/javascripts/mobile/mobile.js @@ -15,119 +15,12 @@ //= require diaspora //= require helpers/i18n //= require widgets/timeago +//= require mobile/mobile_application //= require mobile/mobile_file_uploader //= require mobile/profile_aspects //= require mobile/tag_following //= require mobile/publisher //= require mobile/mobile_comments - -$(document).ready(function(){ - - $('.shield a').click(function(){ - $(this).parents(".stream_element").removeClass("shield-active"); - return false; - }); - var showLoader = function(link){ - link.addClass('loading'); - }; - - var removeLoader = function(link){ - link.removeClass('loading') - .toggleClass('active') - .toggleClass('inactive'); - }; - - // init autosize plugin - autosize($("textarea")); - - /* Drawer menu */ - $("#menu-badge").bind("tap click", function(evt){ - evt.preventDefault(); - $("#app").toggleClass("draw"); - }); - - /* Show / hide aspects in the drawer */ - $("#all_aspects").bind("tap click", function(evt){ - evt.preventDefault(); - $("#all_aspects + li").toggleClass("hide"); - }); - - /* Show / hide followed tags in the drawer */ - $("#followed_tags > a").bind("tap click", function(evt){ - evt.preventDefault(); - $("#followed_tags + li").toggleClass("hide"); - }); - - /* Heart toggle */ - $(".like-action", ".stream").bind("tap click", function(evt){ - evt.preventDefault(); - var link = $(this), - likeCounter = $(this).closest(".stream_element").find(".like-count"), - url = link.data("url"); - - if(!link.hasClass("loading")){ - if(link.hasClass('inactive')) { - $.ajax({ - url: url, - dataType: 'json', - type: 'POST', - beforeSend: showLoader(link), - success: function(data){ - removeLoader(link); - link.data("url", url + "/" + data.id); - - if(likeCounter){ - likeCounter.text(parseInt(likeCounter.text(), 10) + 1); - } - } - }); - } - else if(link.hasClass("active")){ - $.ajax({ - url: url, - dataType: 'json', - type: 'DELETE', - beforeSend: showLoader(link), - complete: function(){ - removeLoader(link); - link.data("url", url.replace(/\/\d+$/, "")); - - if(likeCounter){ - likeCounter.text(parseInt(likeCounter.text(), 10) - 1); - } - } - }); - } - } - }); - - /* Reshare */ - $(".reshare-action:not(.disabled)", ".stream").bind("tap click", function(evt){ - evt.preventDefault(); - - var link = $(this), - href = link.attr("href"), - confirmText = link.attr('title'); - - if(!link.hasClass("loading")) { - if(link.hasClass('inactive')) { - if(confirm(confirmText)) { - $.ajax({ - url: href + "&provider_display_name=mobile", - dataType: 'json', - type: 'POST', - beforeSend: showLoader(link), - success: function(){ - removeLoader(link); - }, - error: function(){ - removeLoader(link); - alert(Diaspora.I18n.t('failed_to_reshare')); - } - }); - } - } - } - }); -}); +//= require mobile/mobile_post_actions +//= require mobile/mobile_drawer // @license-end diff --git a/app/assets/javascripts/mobile/mobile_application.js b/app/assets/javascripts/mobile/mobile_application.js new file mode 100644 index 000000000..d10e2111d --- /dev/null +++ b/app/assets/javascripts/mobile/mobile_application.js @@ -0,0 +1,17 @@ +(function(){ + Diaspora.Mobile = { + initialize: function(){ + $(".shield a").click(function(){ + $(this).parents(".stream_element").removeClass("shield-active"); + return false; + }); + + // init autosize plugin + autosize($("textarea")); + } + }; +})(); + +$(document).ready(function(){ + Diaspora.Mobile.initialize(); +}); diff --git a/app/assets/javascripts/mobile/mobile_comments.js b/app/assets/javascripts/mobile/mobile_comments.js index 0b6e328eb..f4602a3c0 100644 --- a/app/assets/javascripts/mobile/mobile_comments.js +++ b/app/assets/javascripts/mobile/mobile_comments.js @@ -5,19 +5,11 @@ */ (function() { - Diaspora.Mobile = {}; Diaspora.Mobile.Comments = { stream: function(){ return $(".stream"); }, initialize: function() { var self = this; - $(".stream").on("tap click", "a.back_to_stream_element_top", function() { - var bottomBar = $(this).closest(".bottom_bar").first(); - var streamElement = bottomBar.parent(); - $("html, body").animate({ - scrollTop: streamElement.offset().top - 54 - }, 1000); - }); this.stream().on("tap click", "a.show-comments", function(evt){ evt.preventDefault(); diff --git a/app/assets/javascripts/mobile/mobile_drawer.js b/app/assets/javascripts/mobile/mobile_drawer.js new file mode 100644 index 000000000..547d703a5 --- /dev/null +++ b/app/assets/javascripts/mobile/mobile_drawer.js @@ -0,0 +1,28 @@ +(function(){ + Diaspora.Mobile.Drawer = { + allAspects: $("#all_aspects"), + followedTags: $("#followed_tags"), + menuBadge: $("#menu-badge"), + + initialize: function(){ + this.allAspects.bind("tap click", function(evt){ + evt.preventDefault(); + $(this).find("+ li").toggleClass("hide"); + }); + + this.menuBadge.bind("tap click", function(evt){ + evt.preventDefault(); + $("#app").toggleClass("draw"); + }); + + this.followedTags.bind("tap click", function(evt){ + evt.preventDefault(); + $(this).find("+ li").toggleClass("hide"); + }); + } + }; +})(); + +$(function(){ + Diaspora.Mobile.Drawer.initialize(); +}); diff --git a/app/assets/javascripts/mobile/mobile_post_actions.js b/app/assets/javascripts/mobile/mobile_post_actions.js new file mode 100644 index 000000000..56109caed --- /dev/null +++ b/app/assets/javascripts/mobile/mobile_post_actions.js @@ -0,0 +1,114 @@ +(function(){ + Diaspora.Mobile.PostActions = { + initialize: function() { + $(".like-action", ".stream").bind("tap click", this.onLike); + $(".reshare-action", ".stream").bind("tap click", this.onReshare); + }, + + showLoader: function(link) { + link.addClass("loading"); + }, + + hideLoader: function(link) { + link.removeClass("loading"); + }, + + toggleActive: function(link) { + link.toggleClass("active").toggleClass("inactive"); + }, + + like: function(likeCounter, link){ + var url = link.data("url"); + var onSuccess = function(data){ + Diaspora.Mobile.PostActions.toggleActive(link); + link.data("url", url + "/" + data.id); + if(likeCounter){ + likeCounter.text(parseInt(likeCounter.text(), 10) + 1); + } + }; + + $.ajax({ + url: url, + dataType: "json", + type: "POST", + beforeSend: function() { + Diaspora.Mobile.PostActions.showLoader(link); + }, + success: onSuccess, + complete: function() { + Diaspora.Mobile.PostActions.hideLoader(link); + } + }); + }, + + unlike: function(likeCounter, link){ + var url = link.data("url"); + var onSuccess = function(){ + Diaspora.Mobile.PostActions.toggleActive(link); + link.data("url", url.replace(/\/\d+$/, "")); + + if(likeCounter){ + likeCounter.text(parseInt(likeCounter.text(), 10) - 1); + } + }; + + $.ajax({ + url: url, + dataType: "json", + type: "DELETE", + beforeSend: function() { + Diaspora.Mobile.PostActions.showLoader(link); + }, + success: onSuccess, + complete: function() { + Diaspora.Mobile.PostActions.hideLoader(link); + } + }); + }, + + onLike: function(evt){ + evt.preventDefault(); + var link = $(evt.target), + likeCounter = $(evt.target).closest(".stream_element").find(".like-count"); + + if(!link.hasClass("loading") && link.hasClass("inactive")) { + Diaspora.Mobile.PostActions.like(likeCounter, link); + } + else if(!link.hasClass("loading") && link.hasClass("active")) { + Diaspora.Mobile.PostActions.unlike(likeCounter, link); + } + }, + + onReshare: function(evt) { + evt.preventDefault(); + + var link = $(this), + href = link.attr("href"), + confirmText = link.attr("title"); + + if(!link.hasClass("loading") && link.hasClass("inactive") && confirm(confirmText)) { + $.ajax({ + url: href + "&provider_display_name=mobile", + dataType: "json", + type: "POST", + beforeSend: function() { + Diaspora.Mobile.PostActions.showLoader(link); + }, + success: function() { + Diaspora.Mobile.PostActions.toggleActive(link); + }, + error: function() { + alert(Diaspora.I18n.t("failed_to_reshare")); + }, + complete: function() { + Diaspora.Mobile.PostActions.hideLoader(link); + } + }); + } + } + }; +})(); + +$(function(){ + Diaspora.Mobile.PostActions.initialize(); +});