benaskins (owner)

Fork Of

gist: 220201 by anonymous

Forks

Revisions

gist: 220204 Download_button fork
public
Public Clone URL: git://gist.github.com/220204.git
Embed All Files: show embed
snippet.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function GAImporter() {
  this.busy = false;
  this.poller = setInterval(this.pollJob, 5000);
  
  this.pollJob = function() {
    if (!this.busy) {
      this.busy = true;
      this.checkJob();
      this.busy = false;
    }
  };
  
  this.checkJob = function() {
    $.getJSON('/admin/play_golf_offers/check_job/', {
        play_golf_offer_spreadsheet_id: id,
        t: (new Date()).getTime()
      },
      function(json){
        // If the job isn't running anymore
        if (json.state !== "running" && json.state !== "pending") {
          // Stop polling
          clearInterval(this.poll);
        }
        this.showJobStatus(json);
      });
  };
  
  this.showJobStatus = function(json) {
    var activity = $("#activity"),
        html = "";
    switch(json.state) {
      case "running":
        html += '<h2><img src="/images/spinner.gif" alt="" width="16" height="16" /> Processing data&hellip;</h2>';
        break;
      case "pending":
        html += '<h2><img src="/images/spinner.gif" alt="" width="16" height="16" /> Import will begin shortly&hellip;</h2>';
        break;
      case "failed":
        html += '<h2><img src="/images/exclamation.png" alt="" width="16" height="16" /> Import failed</h2>\
<p>' + json.message + '</p>\
<a href="/admin/play_golf_offers/" title="">Return to offers listing</a>';
        break;
      case "finished":
        html += '<h2><img src="/images/tick.png" alt="" width="16" height="16" /> Finished processing data</h2>\
<p>' + json.message + '</p>\
<a href="/admin/play_golf_offers/" title="">Return to offers listing</a>';
        break;
      default:
        html += '<a href="/admin/play_golf_offers/" title="">Return to offers listing</a>';
    }
    // Append the update
    $(activity).html(html);
    // Log for more info's sake
    console.log(json);
  };
 
};
 
 
$(function() {
  var importer = new GAImporter();
});