|
| 1 | +(function(){ |
| 2 | + var PROGRESS_HALF_LIFE = 1000; |
| 3 | + var setup_roster_update_cable, |
| 4 | + progress_asymptotically, |
| 5 | + display_message, |
| 6 | + set_progress, |
| 7 | + display_progress_bar, |
| 8 | + initialize_progress; |
| 9 | + |
| 10 | + var progress_complete = false; |
| 11 | + setup_roster_update_cable = function(){ |
| 12 | + var roster_id = $("#current_roster_id").val(); |
| 13 | + var user_id = $("#current_user_id").val(); |
| 14 | + App.add_students_to_roster = App.cable.subscriptions.create({ |
| 15 | + channel: "AddStudentsToRosterChannel", |
| 16 | + roster_id: roster_id, |
| 17 | + user_id: user_id |
| 18 | + }, |
| 19 | + { |
| 20 | + connected: function() { |
| 21 | + // Called when the subscription is ready for use on the server |
| 22 | + }, |
| 23 | + |
| 24 | + disconnected: function() { |
| 25 | + // Called when the subscription has been terminated by the server |
| 26 | + }, |
| 27 | + |
| 28 | + received: function(data) { |
| 29 | + // Called when there's incoming data on the websocket for this channel |
| 30 | + if(data.status == "completed"){ |
| 31 | + progress_complete = true; |
| 32 | + set_progress(100); |
| 33 | + display_message(data.message); |
| 34 | + } |
| 35 | + } |
| 36 | + }); |
| 37 | + }; |
| 38 | + |
| 39 | + progress_asymptotically = function() { |
| 40 | + recursive_progress_asymptotically = function(recursive_callback, counter) { |
| 41 | + var progress; |
| 42 | + var remaining = 100/counter; |
| 43 | + progress = 100 - (100/counter); |
| 44 | + $(document) |
| 45 | + .find(".roster-update-progress-bar") |
| 46 | + .animate( |
| 47 | + { width: progress.toFixed() + "%" }, |
| 48 | + { duration: PROGRESS_HALF_LIFE * counter } |
| 49 | + ); |
| 50 | + setTimeout(function() { |
| 51 | + if(!progress_complete){ |
| 52 | + recursive_callback(recursive_callback, counter + 1); |
| 53 | + } |
| 54 | + }, |
| 55 | + PROGRESS_HALF_LIFE * counter |
| 56 | + ); |
| 57 | + }; |
| 58 | + recursive_progress_asymptotically(recursive_progress_asymptotically, 1); |
| 59 | +}; |
| 60 | + |
| 61 | +display_message = function(message){ |
| 62 | + $(".roster-update-message").removeAttr("hidden"); |
| 63 | + $("#roster-progress").text(message); |
| 64 | +}; |
| 65 | + |
| 66 | +set_progress = function(percent) { |
| 67 | + $(".roster-update-progress-bar").stop(true, false); |
| 68 | + if (percent === 0) { |
| 69 | + $(".roster-update-progress-bar").css("width", 0); |
| 70 | + } else if (percent) { |
| 71 | + $(".roster-update-progress-bar").animate({width: percent + "%"}); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +toggle_roster_form = function(disable_fields){ |
| 76 | + var text_area = $("#entries-field"); |
| 77 | + var csv_button = $("#file-upload"); |
| 78 | + if(disable_fields){ |
| 79 | + text_area.attr("disabled", "disabled"); |
| 80 | + csv_button.addClass("disabled"); |
| 81 | + }else{ |
| 82 | + text_area.removeAttr("disabled"); |
| 83 | + csv_button.removeClass("disabled"); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +ready = (function(){ |
| 88 | + $("#add-students-roster-form").on("ajax:beforeSend", function(){ |
| 89 | + toggle_roster_form(true); |
| 90 | + $('.roster-update-progress').removeAttr("hidden"); |
| 91 | + progress_asymptotically(); |
| 92 | + }); |
| 93 | + |
| 94 | + $("#add-students-roster-form").on("ajax:complete", function(){ |
| 95 | + toggle_roster_form(false); |
| 96 | + $("#entries-field").val(""); |
| 97 | + }); |
| 98 | + |
| 99 | + $(document).on('closing', '[data-remodal-id=new-student-modal]', function (e) { |
| 100 | + set_progress("0"); |
| 101 | + $('.roster-update-progress').attr("hidden", "hidden"); |
| 102 | + }); |
| 103 | + |
| 104 | + setup_roster_update_cable(); |
| 105 | +}); |
| 106 | + |
| 107 | +$(document).ready(ready); |
| 108 | + |
| 109 | +}).call(this); |
0 commit comments