Project

General

Profile

Download (5.99 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 USAGE:
3

    
4
  A) one progressbar for multiple processes:
5

    
6
  <script src="jquery.js" type="text/javascript" />
7
  <script src="{cdm-webserver-base}/js/cdm_ws_progress.js" type="text/javascript" />
8

    
9
  <script type="text/javascript">
10
  if (Drupal.jsEnabled) {
11
  $(document).ready(function() {
12
  $('.index-trigger').cdm_ws_progress('#index_progress');
13
  });
14
  }
15
  </script>
16

    
17
  <h3>Operations:</h3>
18
  <a class="index-trigger" href="http://127.0.0.1:8080/manage/purge?frontendBaseUrl=http%3A%2F%2F127.0.0.1%3A8080%2F">Purge</a>
19
  <a class="index-trigger" href="http://127.0.0.1:8080/manage/reindex?frontendBaseUrl=http%3A%2F%2F127.0.0.1%3A8080%2F">Reindex</a>
20
  <div id="index_progress" />
21

    
22

    
23
  B) one progressbar for each processes:
24

    
25
  <script src="jquery.js" type="text/javascript" />
26
  <script src="{cdm-webserver-base}/js/cdm_ws_progress.js" type="text/javascript" />
27
  <script type="text/javascript">
28
  if (Drupal.jsEnabled) {
29
  $(document).ready(function() {
30
  $('#reindex').cdm_ws_progress('#reindex_progress');
31
  });
32
  }
33

    
34
  <script type="text/javascript">
35
  if (Drupal.jsEnabled) {
36
  $(document).ready(function() {
37
  $('#purge').cdm_ws_progress('#purge_progress');
38
  });
39
  }
40
 </script>
41

    
42
  <h3>Operations:</h3>
43
  <a id="purge" href="http://127.0.0.1:8080/manage/purge?frontendBaseUrl=http%3A%2F%2F127.0.0.1%3A8080%2F">Purge</a>
44
  <a id="reindex" href="http://127.0.0.1:8080/manage/reindex?frontendBaseUrl=http%3A%2F%2F127.0.0.1%3A8080%2F">Reindex</a>
45
  <div id="reindex_progress" />
46
  <div id="purge_progress" />
47

    
48
 *
49
 */
50
(function($){
51

    
52
  $.fn.cdm_ws_progress = function(progress_container_selector, options) {
53

    
54
    var opts = $.extend({},$.fn.cdm_ws_progress.defaults, options);
55

    
56
    var pollInterval_ms = 2000; // 2 seconds
57

    
58
    // defining some jQuery dom objects in the scope of this function, to be referenced in the sub functions
59
    var $progress_container, $ws_progress_outer, $progress_bar_value, $progress_bar_indicator, $progress_status, $progress_titel;
60

    
61
    var monitorUrl;
62

    
63
    var isRunning = false;
64

    
65
    var startProcess = function(event) {
66

    
67
      //Cancel the default action (navigation) of the click.
68
      event.preventDefault();
69

    
70
      // prevent from starting again if isBlocking flag is set
71
      if(!opts.isBlocking || !isRunning){
72

    
73
        isRunning = true;
74

    
75
        var url = $(this).attr('href');
76
        $.ajax({
77
          url: addFileExtension(url, 'json'),
78
          dataType: "jsonp",
79
          success: function(data){
80
            monitorProgess(data);
81
          }
82
        });
83

    
84
        // show progress indicator by showing the progress outer div
85
        $ws_progress_outer.css('display', 'block');
86
      }  // END !isRunning
87
    };
88

    
89
    var monitorProgess = function(jsonpRedirect){
90
      if(jsonpRedirect !== undefined){
91
        monitorUrl = jsonpRedirect.redirectURL;
92
      }
93
      $.ajax({
94
        url: monitorUrl,
95
        dataType: "jsonp",
96
        success: function(data){
97
          showProgress(data);
98
        }
99
      });
100
    };
101

    
102
    var showProgress = function(monitor){
103
      $progress_titel.text(monitor.taskName);
104
      var percentTwoDecimalDigits = Math.round(monitor.percentage * 100) / 100;
105
      $progress_bar_value.text(percentTwoDecimalDigits + "%");
106
      $progress_bar_indicator.css('width', percentTwoDecimalDigits + "%");
107
      if(monitor.failed){
108
        $progress_status.text("An error occurred");
109
      } else if (monitor.done) {
110
        $progress_status.text("Done");
111
        isRunning = false;
112
      } else {
113
        $progress_status.text(monitor.subTask + " [work ticks: " + (Math.round(monitor.workDone * 100) / 100) + "/" + monitor.totalWork + "]");
114
      }
115
      window.setTimeout(monitorProgess, pollInterval_ms);
116
    };
117

    
118

    
119
    var addFileExtension = function(url, extension){
120
      var new_url;
121
      if(url.indexOf('?') > 0){
122
        new_url = url.substring(0, url.indexOf('?'));
123
        new_url +=  "." + extension;
124
        new_url += url.substring(url.indexOf('?'));
125
      } else  if(url.indexOf('#') > 0){
126
        new_url = url.substring(0, url.indexOf('#'));
127
        new_url +=  "." + extension;
128
        new_url += url.substring(url.indexOf('#'));
129
      } else {
130
        new_url =  url + "." + extension;
131
      }
132
      return new_url;
133
    };
134

    
135
    $progress_container = $(progress_container_selector);
136

    
137
    return this.each(function(index) {
138

    
139
      // creating progressbar and other display lements
140
      $progress_bar_value = $('<div class="progress_bar_value">0%</div>');
141
      $progress_bar_indicator = $('<div class="progress_bar_indicator"></div>');
142
      $progress_bar = $('<div class="progress_bar"></div>').append($progress_bar_indicator).append($progress_bar_value);
143
      $progress_titel = $('<h4 class="progress_title">CDM REST service progress</h4>');
144
      $progress_status = $('<div class="progress_status">waiting ...</div>');
145
      $ws_progress_outer = $('<div class="cdm_ws_progress" id="cdm_ws_progress_' + progress_container_selector.substring(1) + '_' + index + '"></div>').append($progress_titel).append($progress_bar).append($progress_status);
146

    
147
      // styling element
148
      $progress_bar.css('with', opts.width).css('background-color', opts.background_color).css('height', opts.bar_height);
149
      $progress_bar_indicator.css('background-color', opts.indicator_color).css('height', opts.bar_height);
150
      $progress_bar_value.css('text-align', 'center').css('vertical-align', 'middle').css('margin-top', '-'+opts.bar_height);
151
      $ws_progress_outer.css('border', opts.border).css('padding', opts.padding);
152
      // >>> DEBUG
153
      $progress_bar_indicator.css('width', '0%');
154
      $ws_progress_outer.css('display', 'none');
155
      // <<<<
156

    
157
      //finally append the progress widget to the container
158
      $progress_container.append($ws_progress_outer);
159

    
160
      // register onClick for each of the elements
161
      $(this).click(startProcess);
162

    
163
    });
164

    
165
  };
166

    
167
  $.fn.cdm_ws_progress.defaults = {// set up default options
168
      background_color:	"#F3F3F3",
169
      indicator_color:	"#D9EAF5",
170
      width: 				"100%",
171
      bar_height: 		"1.5em",
172
      border:				"1px solid #D9EAF5",
173
      padding:			"1em",
174
      isBlocking:			false
175
  };
176

    
177
})(jQuery);
(1-1/7)