Tyler Scott | f912dea | 2016-08-09 21:50:34 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * This function allows the app to pass in a callback function for updating the progress and a function to call if the progress it cancelled. |
| 3 | * @param func - A function accepting two functions as parameters, the first will be called with one parameter (which is a callback function |
| 4 | * for progress updates) when the overlay is ready. |
| 5 | * @param cancel - A function to call if the overlay is cancelled. |
| 6 | * |
| 7 | * Example: |
| 8 | * openLoadingOverlay(function(update){ |
| 9 | * //Begin progress, call update if progress is in chunks, otherwise ignore. |
| 10 | * }, function(){ |
| 11 | * //Cancelled |
| 12 | * }); |
| 13 | */ |
| 14 | var openLoadingOverlay = (function(){ |
| 15 | "use strict"; |
| 16 | |
| 17 | var isOpen = false; |
| 18 | |
| 19 | return function(func, cancel){ |
| 20 | |
| 21 | var overlay = $('#loading'); |
| 22 | var progress = overlay.find('.progress-bar'); |
| 23 | var cancelButton = overlay.find('#loading-cancel'); |
| 24 | |
| 25 | if (isOpen){ |
| 26 | console.warn("Two overlays are not permitted at the same time. The second will have dummy callbacks"); |
| 27 | func(function(){}); |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | isOpen = true; |
| 32 | |
| 33 | var update = function(done, current, total){ |
| 34 | if (current && total){ |
| 35 | progress.text(current + '/' + total) |
| 36 | .animate({ |
| 37 | width: Math.round(current/total) + '%' |
| 38 | }, |
| 39 | 200, |
| 40 | 'linear', |
| 41 | function(){ |
| 42 | if (done){ |
| 43 | reset(); |
| 44 | } |
| 45 | } |
| 46 | ); |
| 47 | } else { |
| 48 | if (done){ |
| 49 | reset(); |
| 50 | } |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | var reset = function(){ |
| 55 | overlay.modal('hide'); |
| 56 | progress.text('Loading...').css('width', '100%'); |
| 57 | overlay.removeClass('cancelled').addClass('loading'); |
| 58 | cancelButton.removeClass('disabled'); |
| 59 | }; |
| 60 | |
| 61 | cancelButton.one('click', function(){ |
| 62 | |
| 63 | cancelButton.addClass('disabled'); |
| 64 | overlay.addClass('cancelled').removeClass('loading'); |
| 65 | |
| 66 | setTimeout(reset, 2000); |
| 67 | progress.text('Cancelling...').css('width', 0).animate({ |
| 68 | width: '100%' |
| 69 | }, 2000); |
| 70 | |
| 71 | }); |
| 72 | |
| 73 | overlay.modal('show'); |
| 74 | |
| 75 | func(update); |
| 76 | |
| 77 | }; |
| 78 | })(); |