Cached all reused match selector sets, moved synchronous functions into callbacks where needed for optimization; Moved all DOM manip into $(function () {} )

This commit is contained in:
Rick Waldron 2010-09-17 10:10:53 +08:00 committed by Maxwell Salzberg
parent 773839308c
commit fcdc1d914c

View file

@ -3,42 +3,53 @@
* the COPYRIGHT file.
*/
function decrementRequestsCounter(){
var old_request_count = $(".new_requests").html().match(/\d+/);
function decrementRequestsCounter() {
var $new_requests = $(".new_requests"),
old_request_count = $new_requests.html().match(/\d+/);
if( old_request_count == 1 ) {
$(".new_requests").html(
$(".new_requests").html().replace(/ \(\d+\)/,''));
$new_requests.html(
$new_requests.html().replace(/ \(\d+\)/,'')
);
} else {
$(".new_requests").html(
$(".new_requests").html().replace(/\d+/,old_request_count-1));
$new_requests.html(
$new_requests.html().replace(/\d+/,old_request_count-1)
);
}
}
$(function() {
$('#move_friends_link').live( 'click', function(){
$.post('/aspects/move_friends',
$.post(
'/aspects/move_friends',
{ 'moves' : $('#aspect_list').data() },
function(){ $('#aspect_title').html("Groups edited successfully!");});
function() {
$('#aspect_title').html("Groups edited successfully!");
}
);
// should the following logic be moved into the $.post() callback?
$("#aspect_list").removeData();
$(".person").css('background-color','none');
$('#aspect_list').removeData();
$(".person").attr('from_aspect_id', function(){return $(this).parent().attr('id')})
$(".person")
.css('background-color','none')
.attr('from_aspect_id', function() {
return $(this).parent().attr('id')
});
});
$("ul .person").draggable({
revert: true
});
$("ul .requested_person").draggable({
$("ul .person .requested_person").draggable({
revert: true
});
// Moved class to logic above - unnec duplicate logic
//$("ul .requested_person").draggable({
// revert: true
//});
$(".aspect ul").droppable({
hoverClass: 'active',
drop: function(event, ui) {
@ -53,16 +64,23 @@ $(function() {
}
});
}else {
var move = {};
move[ 'friend_id' ] = ui.draggable[0].id
move[ 'to' ] = $(this)[0].id;
move[ 'from' ] = ui.draggable[0].getAttribute('from_aspect_id');
} else {
var $aspect_list = $('#aspect_list'),
move = {};
// This is poor implementation
move[ 'friend_id' ] = ui.draggable[0].id; // ui.draggable.attr('id')
move[ 'to' ] = $(this)[0].id;// $(this).attr('id');
move[ 'from' ] = ui.draggable[0].getAttribute('from_aspect_id'); // ui.draggable.attr('from_aspect_id')
// if created custom attr's - should be using `data-foo`
if (move['to'] == move['from']){
$('#aspect_list').data( ui.draggable[0].id, []);
$aspect_list.data( ui.draggable[0].id, []);
ui.draggable.css('background-color','#eee');
} else {
$('#aspect_list').data( ui.draggable[0].id, move);
$aspect_list.data( ui.draggable[0].id, move);
ui.draggable.css('background-color','orange');
}
}
@ -77,44 +95,51 @@ $(function() {
if ($(ui.draggable[0]).hasClass('requested_person')){
$.ajax({
type: "DELETE",
url: "/requests/" + ui.draggable[0].getAttribute('request_id')
url: "/requests/" + ui.draggable.attr('request_id'),
success: function () {
decrementRequestsCounter();
}
});
decrementRequestsCounter();
$(ui.draggable[0]).fadeOut('slow')
}else{
} else {
$.ajax({
type: "DELETE",
url: "/people/" + ui.draggable[0].id
url: "/people/" + ui.draggable.attr('id'),
success: function () {
alert("Removed Friend, proably want an undo countdown.")
}
});
alert("Removed Friend, proably want an undo countdown.")
$(ui.draggable[0]).fadeOut('slow')
}
$(ui.draggable[0]).fadeOut('slow'); // ui.draggable.fadeOut('slow')
}
});
});
$(".aspect h1").live( 'focus', function() {
var $this = $(this);
var id = $this.closest("li").children("ul").attr("id");
var link = "/aspects/"+ id;
$(".aspect h1").live( 'focus', function() {
$this.keypress(function(e) {
if (e.which == 13) {
e.preventDefault();
$this.blur();
var $this = $(this),
id = $this.closest("li").children("ul").attr("id"),
link = "/aspects/"+ id;
//save changes
$.ajax({
type: "PUT",
url: link,
data: {"aspect" : {"name" : $this.text() }}
$this.keypress(function(e) {
if (e.which == 13) {
e.preventDefault();
$this.blur();
//save changes
$.ajax({
type: "PUT",
url: link,
data: {"aspect" : {"name" : $this.text() }}
});
}
//update all other aspect links
$this.keyup(function(e) {
$("#aspect_nav a[href='"+link+"']").text($this.text());
});
}
//update all other aspect links
$this.keyup(function(e) {
$("#aspect_nav a[href='"+link+"']").text($this.text());
});
});
});