Guest User

Untitled

a guest
Aug 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. $(function(){
  2. /* Projects */
  3. $(".add-task").click(function(){ Task.newForm($(this).attr("href"), $(this)); return false; });
  4. /* Todo List */
  5. $("ul.todos li input[type=checkbox]").click(function() { if($(this).attr('checked')) Todo.close($(this)) });
  6.  
  7. /* Invoice */
  8. $(".line-items-list li.line-item input").live("blur", function() {Invoice.updateLineItemTotal($(this).parents('li.line-item'))});
  9. $("#new_line_item").click(function() { Invoice.addLineItem($(".line-items-list li:last")); return false; });
  10. $("#invoice-delivery-form-link, .delivery-form-cancel").click(function() { Invoice.toggleDeliveryForm(); return false; });
  11. $("#invoice-payment-form-link, .invoice-payment-form-cancel").click(function() { Invoice.toggleInvoicePaymentForm(); return false; });
  12. $(".tasks-list .task a.task-name").click(function() { Task.toggleDetails($(this)); return false; });
  13. });
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. // Always send auth token
  22. $(document).ajaxSend(function(event, request, settings) {
  23. if ( settings.type == 'post') {
  24. settings.data = (settings.data ? settings.data + "&" : "")
  25. + $("meta[name=csrf-token]").attr("content") + "=" + $("meta[name=csrf-param]").attr("content"); // "authenticity_token=" + encodeURIComponent( AUTH_TOKEN );
  26. }
  27. });
  28.  
  29.  
  30. Kludge = {
  31. /*
  32. Method of adding in dynamic / ajax content
  33. var html = HTML code to be added
  34. var callback = A callback function
  35. */
  36. insertDynamicContent: function(html, callback) {
  37. $div = $("<div class='dynamic-content'><a href='#' class='close'>&otimes;</a>" + html + "</div>");
  38. $("#stage").before($div);
  39. $(".dynamic-content").slideDown(function() {
  40. $(this).find(".close").show('slide', {direction: 'right'}, 75, function(){
  41. if(callback)
  42. callback();
  43. });
  44. });
  45. $("#stage").animate({"opacity": .3}, 500);
  46. $(".dynamic-content .close").click(function(){ Kludge.removeDynamicContent(); return false; });
  47. },
  48.  
  49. removeDynamicContent: function() {
  50. $(".dynamic-content").slideUp(function(){ $(this).remove() });
  51. $("#stage").animate({"opacity": 1}, 500);
  52. }
  53. }
  54.  
  55. Invoice = {
  56. addLineItem: function(line_item) {
  57. $new_row = $(line_item).clone();
  58. $form_elms = $new_row.find("input, textarea, select").each(function(){
  59. $(this).attr("name", $(this).attr('name').replace(/\d+/, function (x) {return parseInt(x)+1;})).val("");
  60. });
  61. $(line_item).after($new_row);
  62. return false;
  63. },
  64.  
  65. toggleDeliveryForm: function() {
  66. if($("#invoice-deliver-form").is(':hidden')) {
  67. $("#invoice").animate({"opacity": .3}, 500);
  68. $("#invoice-deliver-form").slideDown();
  69. $("#invoice-delivery-form-link").html("Hide Delivery E-mail");
  70. } else {
  71. $("#invoice").animate({"opacity": 1}, 500);
  72. $("#invoice-deliver-form").slideUp();
  73. $("#invoice-delivery-form-link").html("Send Invoice");
  74. }
  75. },
  76.  
  77. toggleInvoicePaymentForm: function() {
  78. if($("#invoice-payment-form").is(':hidden')) {
  79. $("#invoice").animate({"opacity": .3}, 500);
  80. $("#invoice-payment-form").slideDown();
  81. $("#invoice-payment-form-link").html("Hide Invoice Payment");
  82. } else {
  83. $("#invoice").animate({"opacity": 1}, 500);
  84. $("#invoice-payment-form").slideUp();
  85. $("#invoice-payment-form-link").html("Add Payment");
  86. }
  87. },
  88.  
  89. updateLineItemTotal: function(line_item) {
  90. quantity = $(line_item).find('.quantity input').val();
  91. price = $(line_item).find('.price input').val();
  92.  
  93. line_item.find('span.total').html("$" + formatAsMoney(price * quantity));
  94. if(price != "")
  95. $(line_item).find('.price input').val(formatAsMoney(price));
  96.  
  97. return false;
  98. }
  99. }
  100.  
  101. Task = {
  102. newForm: function(url) {
  103. Kludge.removeDynamicContent();
  104. url = "/projects/5/tasks/new";
  105. $.get(url, function(data){
  106. Kludge.insertDynamicContent(data, function(){
  107. $("#new_task").ajaxForm({
  108. success: function(data) {
  109. Kludge.removeDynamicContent();
  110. $li = $(data).hide();
  111. $(".tasks-list").prepend($li).find("li:first").slideDown(function() { $(this).effect("highlight", {color: "#CEF2FF"}, 1500) } );
  112. }
  113. });
  114. });
  115. });
  116. },
  117.  
  118. toggleDetails: function(elm) {
  119. $(elm).next('.task-details').slideToggle("fast");
  120. }
  121. }
  122.  
  123. Todo = {
  124. close: function(checkbox) {
  125. url = "/todos";
  126.  
  127. $.ajax({
  128. type: "PUT",
  129. url: url + "/" + $(checkbox).attr("name"),
  130. data: { "todo[is_active]": "0" },
  131. success: function(response) {
  132. if(response == 0)
  133. $(checkbox).parents('li').fadeOut();
  134. }
  135. })
  136. }
  137. }
  138.  
  139.  
  140.  
  141. /* Functions */
  142. function formatAsMoney(mnt) {
  143. mnt -= 0;
  144. mnt = (Math.round(mnt*100))/100;
  145. return (mnt == Math.floor(mnt)) ? mnt + '.00'
  146. : ( (mnt*10 == Math.floor(mnt*10)) ?
  147. mnt + '0' : mnt);
  148. }
Add Comment
Please, Sign In to add comment