refactor aspect-edit.js in prep for jasmine testing

This commit is contained in:
Sarah Mei 2010-11-02 13:37:27 -07:00
parent e282abd526
commit fad9c09212
3 changed files with 157 additions and 144 deletions

View file

@ -1,88 +1,84 @@
/* Copyright (c) 2010, Diaspora Inc. This file is /* Copyright (c) 2010, Diaspora Inc. This file is
* licensed under the Affero General Public License version 3 or later. See * licensed under the Affero General Public License version 3 or later. See
* the COPYRIGHT file. * the COPYRIGHT file.
*/ */
function decrementRequestsCounter() { var AspectEdit = {
var $new_requests = $(".new_requests");
var request_html = $new_requests.html();
var old_request_count = request_html.match(/\d+/);
if( old_request_count == 1 ) { initialize: function() {
$new_requests.html(
request_html.replace(/ \(\d+\)/,'')
);
} else {
$new_requests.html(
request_html.replace(/\d+/,old_request_count-1)
);
}
}
// Dragging person between aspects
$(function() {
$("ul .person").draggable({ $("ul .person").draggable({
revert: true, revert: true,
start: function(event,ui){ start: AspectEdit.startDrag,
$(this).children("img").animate({'height':80, 'width':80, 'opacity':0.8},200) drag: AspectEdit.duringDrag,
.tipsy("hide"); stop: AspectEdit.stopDrag
$(".draggable_info").fadeIn(100);
},
drag: function(event,ui){
$(this).children("img").tipsy("hide"); //ensure this is hidden
},
stop: function(event,ui){
$(this).children("img").animate({'height':70, 'width':70, 'opacity':1},200);
$(".draggable_info").fadeOut(100);
}
}); });
$(".aspect ul.dropzone").droppable({ $(".aspect ul.dropzone").droppable({
hoverClass: 'active', hoverClass: 'active',
drop: function(event, ui) { drop: AspectEdit.onDropMove
});
$(".aspect_remove ul").droppable({
hoverClass: 'active',
drop: AspectEdit.onDropDelete
});
$(".delete").live("click", AspectEdit.deletePerson);
$(".aspect h3").live('focus', AspectEdit.changeName);
},
startDrag: function(event, ui) {
$(this).children("img").animate({'height':80, 'width':80, 'opacity':0.8}, 200)
.tipsy("hide");
$(".draggable_info").fadeIn(100);
},
duringDrag: function(event, ui) {
$(this).children("img").tipsy("hide"); //ensure this is hidden
},
stopDrag: function(event, ui) {
$(this).children("img").animate({'height':70, 'width':70, 'opacity':1}, 200);
$(".draggable_info").fadeOut(100);
},
onDropMove: function(event, ui) {
var dropzone = $(this); var dropzone = $(this);
var person = ui.draggable; var person = ui.draggable;
if( person.hasClass('request') ){ if (person.hasClass('request')) {
$.ajax({ $.ajax({
type: "DELETE", type: "DELETE",
url: "/requests/" + person.attr('data-guid'), url: "/requests/" + person.attr('data-guid'),
data: {"accept" : true, "aspect_id" : dropzone.attr('data-aspect_id') }, data: {"accept" : true, "aspect_id" : dropzone.attr('data-aspect_id') },
success: function(data){ success: function(data) {
decrementRequestsCounter(); AspectEdit.decrementRequestsCounter();
} }
}); });
}; }
if (dropzone.attr('data-aspect_id') != person.attr('data-aspect_id')) {
if( dropzone.attr('data-aspect_id') != person.attr('data-aspect_id' )){
$.ajax({ $.ajax({
url: "/aspects/move_friend/", url: "/aspects/move_friend/",
data: {"friend_id" : person.attr('data-guid'), data: {"friend_id" : person.attr('data-guid'),
"from" : person.attr('data-aspect_id'), "from" : person.attr('data-aspect_id'),
"to" : { "to" : dropzone.attr('data-aspect_id') }}, "to" : { "to" : dropzone.attr('data-aspect_id') }},
success: function(data){ success: function(data) {
person.attr('data-aspect_id', dropzone.attr('data-aspect_id')); person.attr('data-aspect_id', dropzone.attr('data-aspect_id'));
}}); }});
} }
dropzone.closest("ul").append(person); dropzone.closest("ul").append(person);
} },
});
$(".aspect_remove ul").droppable({
hoverClass: 'active',
drop: function(event, ui) {
onDropDelete: function(event, ui) {
var person = ui.draggable; var person = ui.draggable;
if ( person.attr('data-guid').length == 1 ) { if (person.attr('data-guid').length == 1) {
alert("You can not remove the person from the last aspect"); alert("You can not remove the person from the last aspect");
} else { } else {
if( !person.hasClass('request') ){ if (!person.hasClass('request')) {
$.ajax({ $.ajax({
type: "POST", type: "POST",
@ -92,53 +88,17 @@ $(function() {
'aspect_id' : person.attr('data-aspect_id') } 'aspect_id' : person.attr('data-aspect_id') }
}); });
} }
person.fadeOut(400, function(){person.remove();}); person.fadeOut(400, function() {
} person.remove();
}
});
});
// Person deletion
$(".delete").live("click", function() {
var person = $(this).closest("li.person");
if (person.hasClass('request')){
if( confirm("Ignore request?") ){
var request_id = person.attr("data-guid");
$.ajax({
type: "DELETE",
url: "/requests/" + request_id,
success: function () {
decrementRequestsCounter();
}
}); });
} }
},
} else { changeName: function() {
if( confirm("Remove this person from all aspects?") ){
var person_id = $(this).closest("li.person").attr('data-guid');
$.ajax({
type: "DELETE",
url: "/people/" + person_id,
success: function() {
person.fadeOut(200);
}
});
}
}
});
// Editing aspect name
$(".aspect h3").live('focus', function() {
var $this = $(this); var $this = $(this);
var id = $this.closest("li.aspect").attr("data-guid"); var id = $this.closest("li.aspect").attr("data-guid");
var link = "/aspects/"+ id; var link = "/aspects/" + id;
$this.keypress(function(e) { $this.keypress(function(e) {
if (e.which == 13) { if (e.which == 13) {
@ -154,7 +114,56 @@ $(".aspect h3").live('focus', function() {
} }
//update all other aspect links //update all other aspect links
$this.keyup(function(e) { $this.keyup(function(e) {
$("#aspect_nav a[href='"+link+"']").text($this.text()); $("#aspect_nav a[href='" + link + "']").text($this.text());
}); });
}); });
}); },
deletePerson: function() {
var person = $(this).closest("li.person");
if (person.hasClass('request')) {
if (confirm("Ignore request?")) {
var request_id = person.attr("data-guid");
$.ajax({
type: "DELETE",
url: "/requests/" + request_id,
success: function () {
AspectEdit.decrementRequestsCounter();
}
});
}
} else {
if (confirm("Remove this person from all aspects?")) {
var person_id = $(this).closest("li.person").attr('data-guid');
$.ajax({
type: "DELETE",
url: "/people/" + person_id,
success: function() {
person.fadeOut(200);
}
});
}
}
},
decrementRequestsCounter: function() {
var $new_requests = $(".new_requests");
var request_html = $new_requests.html();
var old_request_count = request_html.match(/\d+/);
if (old_request_count == 1) {
$new_requests.html(
request_html.replace(/ \(\d+\)/, '')
);
} else {
$new_requests.html(
request_html.replace(/\d+/, old_request_count - 1)
);
}
}
};
$(document).ready(AspectEdit.initialize);

View file

@ -1,27 +1,30 @@
describe("editing aspects", function() { describe("AspectEdit", function() {
// describe("initialize", function() {
//
// });
describe("decrementRequestsCounter", function() { describe("decrementRequestsCounter", function() {
describe("when there is one request", function() { describe("when there is one request", function() {
it("removes the counter from the new requests div", function() { it("removes the counter from the new requests div", function() {
$('#jasmine_content').html("<div class='new_requests'>Requests (1)</div>"); $('#jasmine_content').html("<div class='new_requests'>Requests (1)</div>");
decrementRequestsCounter(); AspectEdit.decrementRequestsCounter();
expect($('.new_requests').first().html()).toEqual("Requests"); expect($('.new_requests').first().html()).toEqual("Requests");
}); });
}); });
describe("when there is more than one request", function() { describe("when there is more than one request", function() {
it("decrements the request counter", function() { it("decrements the request counter", function() {
$('#jasmine_content').html("<div class='new_requests'>Requests (67)</div>"); $('#jasmine_content').html("<div class='new_requests'>Requests (67)</div>");
decrementRequestsCounter(); AspectEdit.decrementRequestsCounter();
expect($('.new_requests').first().html()).toEqual("Requests (66)"); expect($('.new_requests').first().html()).toEqual("Requests (66)");
}); });
}); });
describe("error cases", function() { describe("error cases", function() {
it("fails silently if there are no requests", function() { it("fails silently if there are no requests", function() {
$('#jasmine_content').html("<div class='new_requests'>Requests</div>"); $('#jasmine_content').html("<div class='new_requests'>Requests</div>");
decrementRequestsCounter(); AspectEdit.decrementRequestsCounter();
expect($('.new_requests').first().html()).toEqual("Requests"); expect($('.new_requests').first().html()).toEqual("Requests");
}); });
}); });
}); });
}); });

View file

@ -12,6 +12,7 @@
# #
src_files: src_files:
- public/javascripts/jquery142.js - public/javascripts/jquery142.js
- public/javascripts/jquery-ui-1.8.4.custom.min.js
- public/javascripts/aspect-edit.js - public/javascripts/aspect-edit.js
# stylesheets # stylesheets