Mobile JS refactor
This commit is contained in:
parent
4f2c6942a5
commit
cc1faa63ec
5 changed files with 162 additions and 118 deletions
|
|
@ -15,119 +15,12 @@
|
||||||
//= require diaspora
|
//= require diaspora
|
||||||
//= require helpers/i18n
|
//= require helpers/i18n
|
||||||
//= require widgets/timeago
|
//= require widgets/timeago
|
||||||
|
//= require mobile/mobile_application
|
||||||
//= require mobile/mobile_file_uploader
|
//= require mobile/mobile_file_uploader
|
||||||
//= require mobile/profile_aspects
|
//= require mobile/profile_aspects
|
||||||
//= require mobile/tag_following
|
//= require mobile/tag_following
|
||||||
//= require mobile/publisher
|
//= require mobile/publisher
|
||||||
//= require mobile/mobile_comments
|
//= require mobile/mobile_comments
|
||||||
|
//= require mobile/mobile_post_actions
|
||||||
$(document).ready(function(){
|
//= require mobile/mobile_drawer
|
||||||
|
|
||||||
$('.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'));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
// @license-end
|
// @license-end
|
||||||
|
|
|
||||||
17
app/assets/javascripts/mobile/mobile_application.js
Normal file
17
app/assets/javascripts/mobile/mobile_application.js
Normal file
|
|
@ -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();
|
||||||
|
});
|
||||||
|
|
@ -5,19 +5,11 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
Diaspora.Mobile = {};
|
|
||||||
Diaspora.Mobile.Comments = {
|
Diaspora.Mobile.Comments = {
|
||||||
stream: function(){ return $(".stream"); },
|
stream: function(){ return $(".stream"); },
|
||||||
|
|
||||||
initialize: function() {
|
initialize: function() {
|
||||||
var self = this;
|
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){
|
this.stream().on("tap click", "a.show-comments", function(evt){
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
|
|
|
||||||
28
app/assets/javascripts/mobile/mobile_drawer.js
Normal file
28
app/assets/javascripts/mobile/mobile_drawer.js
Normal file
|
|
@ -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();
|
||||||
|
});
|
||||||
114
app/assets/javascripts/mobile/mobile_post_actions.js
Normal file
114
app/assets/javascripts/mobile/mobile_post_actions.js
Normal file
|
|
@ -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();
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue