Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(function(){
- /* Projects */
- $(".add-task").click(function(){ Task.newForm($(this).attr("href"), $(this)); return false; });
- /* Todo List */
- $("ul.todos li input[type=checkbox]").click(function() { if($(this).attr('checked')) Todo.close($(this)) });
- /* Invoice */
- $(".line-items-list li.line-item input").live("blur", function() {Invoice.updateLineItemTotal($(this).parents('li.line-item'))});
- $("#new_line_item").click(function() { Invoice.addLineItem($(".line-items-list li:last")); return false; });
- $("#invoice-delivery-form-link, .delivery-form-cancel").click(function() { Invoice.toggleDeliveryForm(); return false; });
- $("#invoice-payment-form-link, .invoice-payment-form-cancel").click(function() { Invoice.toggleInvoicePaymentForm(); return false; });
- $(".tasks-list .task a.task-name").click(function() { Task.toggleDetails($(this)); return false; });
- });
- // Always send auth token
- $(document).ajaxSend(function(event, request, settings) {
- if ( settings.type == 'post') {
- settings.data = (settings.data ? settings.data + "&" : "")
- + $("meta[name=csrf-token]").attr("content") + "=" + $("meta[name=csrf-param]").attr("content"); // "authenticity_token=" + encodeURIComponent( AUTH_TOKEN );
- }
- });
- Kludge = {
- /*
- Method of adding in dynamic / ajax content
- var html = HTML code to be added
- var callback = A callback function
- */
- insertDynamicContent: function(html, callback) {
- $div = $("<div class='dynamic-content'><a href='#' class='close'>⊗</a>" + html + "</div>");
- $("#stage").before($div);
- $(".dynamic-content").slideDown(function() {
- $(this).find(".close").show('slide', {direction: 'right'}, 75, function(){
- if(callback)
- callback();
- });
- });
- $("#stage").animate({"opacity": .3}, 500);
- $(".dynamic-content .close").click(function(){ Kludge.removeDynamicContent(); return false; });
- },
- removeDynamicContent: function() {
- $(".dynamic-content").slideUp(function(){ $(this).remove() });
- $("#stage").animate({"opacity": 1}, 500);
- }
- }
- Invoice = {
- addLineItem: function(line_item) {
- $new_row = $(line_item).clone();
- $form_elms = $new_row.find("input, textarea, select").each(function(){
- $(this).attr("name", $(this).attr('name').replace(/\d+/, function (x) {return parseInt(x)+1;})).val("");
- });
- $(line_item).after($new_row);
- return false;
- },
- toggleDeliveryForm: function() {
- if($("#invoice-deliver-form").is(':hidden')) {
- $("#invoice").animate({"opacity": .3}, 500);
- $("#invoice-deliver-form").slideDown();
- $("#invoice-delivery-form-link").html("Hide Delivery E-mail");
- } else {
- $("#invoice").animate({"opacity": 1}, 500);
- $("#invoice-deliver-form").slideUp();
- $("#invoice-delivery-form-link").html("Send Invoice");
- }
- },
- toggleInvoicePaymentForm: function() {
- if($("#invoice-payment-form").is(':hidden')) {
- $("#invoice").animate({"opacity": .3}, 500);
- $("#invoice-payment-form").slideDown();
- $("#invoice-payment-form-link").html("Hide Invoice Payment");
- } else {
- $("#invoice").animate({"opacity": 1}, 500);
- $("#invoice-payment-form").slideUp();
- $("#invoice-payment-form-link").html("Add Payment");
- }
- },
- updateLineItemTotal: function(line_item) {
- quantity = $(line_item).find('.quantity input').val();
- price = $(line_item).find('.price input').val();
- line_item.find('span.total').html("$" + formatAsMoney(price * quantity));
- if(price != "")
- $(line_item).find('.price input').val(formatAsMoney(price));
- return false;
- }
- }
- Task = {
- newForm: function(url) {
- Kludge.removeDynamicContent();
- url = "/projects/5/tasks/new";
- $.get(url, function(data){
- Kludge.insertDynamicContent(data, function(){
- $("#new_task").ajaxForm({
- success: function(data) {
- Kludge.removeDynamicContent();
- $li = $(data).hide();
- $(".tasks-list").prepend($li).find("li:first").slideDown(function() { $(this).effect("highlight", {color: "#CEF2FF"}, 1500) } );
- }
- });
- });
- });
- },
- toggleDetails: function(elm) {
- $(elm).next('.task-details').slideToggle("fast");
- }
- }
- Todo = {
- close: function(checkbox) {
- url = "/todos";
- $.ajax({
- type: "PUT",
- url: url + "/" + $(checkbox).attr("name"),
- data: { "todo[is_active]": "0" },
- success: function(response) {
- if(response == 0)
- $(checkbox).parents('li').fadeOut();
- }
- })
- }
- }
- /* Functions */
- function formatAsMoney(mnt) {
- mnt -= 0;
- mnt = (Math.round(mnt*100))/100;
- return (mnt == Math.floor(mnt)) ? mnt + '.00'
- : ( (mnt*10 == Math.floor(mnt*10)) ?
- mnt + '0' : mnt);
- }
Add Comment
Please, Sign In to add comment