blob: aee693bf1a013b83b09ca202cc362ec3f35dca48 [file] [log] [blame]
Timo Tijhof51516cd2013-04-09 01:32:29 +02001// Client script for Zuul status page
2//
3// Copyright 2012 OpenStack Foundation
4// Copyright 2013 Timo Tijhof
5// Copyright 2013 Wikimedia Foundation
Joshua Hesketh6b1a2182014-03-21 14:40:04 +11006// Copyright 2014 Rackspace Australia
Timo Tijhof51516cd2013-04-09 01:32:29 +02007//
8// Licensed under the Apache License, Version 2.0 (the "License"); you may
9// not use this file except in compliance with the License. You may obtain
10// a copy of the License at
11//
12// http://www.apache.org/licenses/LICENSE-2.0
13//
14// Unless required by applicable law or agreed to in writing, software
15// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17// License for the specific language governing permissions and limitations
18// under the License.
Monty Taylor860bb0a2014-03-22 09:41:25 -070019'use strict';
Timo Tijhof51516cd2013-04-09 01:32:29 +020020
21(function ($) {
Joshua Hesketh4863b602014-03-21 14:19:06 +110022 var $container, $msg, $indicator, $queueInfo, $queueEventsNum,
Joshua Heskethcbdcca12014-03-20 16:06:25 +110023 $queueResultsNum, $pipelines, $jq;
Joshua Hesketh298c4912014-03-20 16:06:25 +110024 var xhr, zuul,
Joshua Heskethace48892014-03-22 17:18:31 +110025 current_filter = '',
Timo Tijhof51516cd2013-04-09 01:32:29 +020026 demo = location.search.match(/[?&]demo=([^?&]*)/),
Joshua Hesketh668700b2014-03-21 14:25:53 +110027 source_url = location.search.match(/[?&]source_url=([^?&]*)/),
Timo Tijhof51516cd2013-04-09 01:32:29 +020028 source = demo ?
29 './status-' + (demo[1] || 'basic') + '.json-sample' :
James E. Blair7c7ed7a2013-05-15 13:13:26 -070030 'status.json';
Monty Taylor860bb0a2014-03-22 09:41:25 -070031 source = source_url ? source_url[1] : source;
Timo Tijhof51516cd2013-04-09 01:32:29 +020032
Joshua Heskethace48892014-03-22 17:18:31 +110033 function set_cookie(name, value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070034 document.cookie = name + '=' + value + '; path=/';
Joshua Heskethace48892014-03-22 17:18:31 +110035 }
36
37 function read_cookie(name, default_value) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070038 var nameEQ = name + '=';
Joshua Heskethace48892014-03-22 17:18:31 +110039 var ca = document.cookie.split(';');
40 for(var i=0;i < ca.length;i++) {
41 var c = ca[i];
Monty Taylor860bb0a2014-03-22 09:41:25 -070042 while (c.charAt(0) === ' ') {
43 c = c.substring(1, c.length);
44 }
45 if (c.indexOf(nameEQ) === 0) {
Joshua Heskethace48892014-03-22 17:18:31 +110046 return c.substring(nameEQ.length, c.length);
47 }
48 }
49 return default_value;
50 }
51
52
Timo Tijhof51516cd2013-04-09 01:32:29 +020053 zuul = {
54 enabled: true,
Joshua Heskethdb8046e2014-03-21 18:42:25 +110055 collapsed_exceptions: [],
Timo Tijhof51516cd2013-04-09 01:32:29 +020056
57 schedule: function () {
58 if (!zuul.enabled) {
59 setTimeout(zuul.schedule, 5000);
60 return;
61 }
62 zuul.update().complete(function () {
63 setTimeout(zuul.schedule, 5000);
64 });
65 },
66
67 /** @return {jQuery.Promise} */
68 update: function () {
69 // Cancel the previous update if it hasn't completed yet.
70 if (xhr) {
71 xhr.abort();
72 }
73
74 zuul.emit('update-start');
75
Joshua Hesketh298c4912014-03-20 16:06:25 +110076 xhr = $.getJSON(source)
77 .done(function (data) {
78 if ('message' in data) {
Joshua Hesketh4863b602014-03-21 14:19:06 +110079 $msg.removeClass('alert-danger').addClass('alert-info');
Joshua Hesketh298c4912014-03-20 16:06:25 +110080 $msg.text(data.message);
Joshua Hesketh4863b602014-03-21 14:19:06 +110081 $msg.show();
Joshua Hesketh298c4912014-03-20 16:06:25 +110082 } else {
83 $msg.empty();
Joshua Hesketh4863b602014-03-21 14:19:06 +110084 $msg.hide();
Joshua Hesketh298c4912014-03-20 16:06:25 +110085 }
Timo Tijhof51516cd2013-04-09 01:32:29 +020086
Joshua Hesketh298c4912014-03-20 16:06:25 +110087 if ('zuul_version' in data) {
Monty Taylor860bb0a2014-03-22 09:41:25 -070088 $('#zuul-version-span').text(data.zuul_version);
Joshua Hesketh298c4912014-03-20 16:06:25 +110089 }
90 if ('last_reconfigured' in data) {
91 var last_reconfigured =
Monty Taylor860bb0a2014-03-22 09:41:25 -070092 new Date(data.last_reconfigured);
Joshua Hesketh298c4912014-03-20 16:06:25 +110093 $('#last-reconfigured-span').text(
94 last_reconfigured.toString());
95 }
96
97 $pipelines.html('');
98 $.each(data.pipelines, function (i, pipeline) {
99 $pipelines.append(zuul.format.pipeline(pipeline));
100 });
101
102 $queueEventsNum.text(
103 data.trigger_event_queue ?
104 data.trigger_event_queue.length : '0'
105 );
106 $queueResultsNum.text(
107 data.result_event_queue ?
108 data.result_event_queue.length : '0'
109 );
110 })
111 .fail(function (err, jqXHR, errMsg) {
112 $msg.text(source + ': ' + errMsg).show();
Monty Taylor860bb0a2014-03-22 09:41:25 -0700113 $msg.removeClass('zuul-msg-wrap-off');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100114 })
115 .complete(function () {
116 xhr = undefined;
117 zuul.emit('update-end');
Timo Tijhof51516cd2013-04-09 01:32:29 +0200118 });
119
Timo Tijhof51516cd2013-04-09 01:32:29 +0200120 return xhr;
121 },
122
123 format: {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100124 job: function(job) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700125 var $job_line;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100126 if (job.url !== null) {
127 $job_line = $('<a href="' + job.url + '" />');
128 }
129 else{
130 $job_line = $('<span />');
131 }
132 $job_line.text(job.name)
133 .append(zuul.format.job_status(job));
134
135 if (job.voting === false) {
136 $job_line.append(
137 $(' <small />').text(' (non-voting)')
138 );
139 }
140 return $job_line;
141 },
142
143 job_status: function(job) {
144 var result = job.result ? job.result.toLowerCase() : null;
145 if (result === null) {
146 result = job.url ? 'in progress' : 'queued';
147 }
148
Monty Taylor860bb0a2014-03-22 09:41:25 -0700149 if (result === 'in progress') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100150 return zuul.format.job_progress_bar(job.elapsed_time,
151 job.remaining_time);
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100152 }
153 else {
154 return zuul.format.status_label(result);
155 }
156 },
157
158 status_label: function(result) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700159 var $status = $('<span />');
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100160 $status.addClass('zuul-job-result label');
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100161
162 switch (result) {
163 case 'success':
164 $status.addClass('label-success');
165 break;
166 case 'failure':
167 $status.addClass('label-danger');
168 break;
169 case 'unstable':
170 $status.addClass('label-warning');
171 break;
172 case 'in progress':
173 case 'queued':
174 case 'lost':
175 $status.addClass('label-default');
176 break;
177 }
178 $status.text(result);
179 return $status;
180 },
181
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100182 job_progress_bar: function(elapsed_time, remaining_time) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100183 var progress_percent = 100 * (elapsed_time / (elapsed_time +
184 remaining_time));
185 var $bar_inner = $('<div />')
186 .addClass('progress-bar')
187 .attr('role', 'progressbar')
188 .attr('aria-valuenow', 'progressbar')
189 .attr('aria-valuemin', progress_percent)
190 .attr('aria-valuemin', '0')
191 .attr('aria-valuemax', '100')
192 .css('width', progress_percent + '%');
193
194 var $bar_outter = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100195 .addClass('progress zuul-job-result')
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100196 .append($bar_inner);
197
198 return $bar_outter;
199 },
200
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100201 enqueue_time: function(ms) {
202 // Special format case for enqueue time to add style
203 var hours = 60 * 60 * 1000;
204 var now = Date.now();
205 var delta = now - ms;
Monty Taylor860bb0a2014-03-22 09:41:25 -0700206 var status = 'text-success';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100207 var text = zuul.format.time(delta, true);
208 if (delta > (4 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700209 status = 'text-danger';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100210 } else if (delta > (2 * hours)) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700211 status = 'text-warning';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100212 }
213 return '<span class="' + status + '">' + text + '</span>';
214 },
215
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100216 time: function(ms, words) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700217 if (typeof(words) === 'undefined') {
218 words = false;
219 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100220 var seconds = (+ms)/1000;
221 var minutes = Math.floor(seconds/60);
222 var hours = Math.floor(minutes/60);
223 seconds = Math.floor(seconds % 60);
224 minutes = Math.floor(minutes % 60);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700225 var r = '';
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100226 if (words) {
227 if (hours) {
228 r += hours;
229 r += ' hr ';
230 }
231 r += minutes + ' min';
232 } else {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700233 if (hours < 10) {
234 r += '0';
235 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100236 r += hours + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700237 if (minutes < 10) {
238 r += '0';
239 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100240 r += minutes + ':';
Monty Taylor860bb0a2014-03-22 09:41:25 -0700241 if (seconds < 10) {
242 r += '0';
243 }
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100244 r += seconds;
245 }
246 return r;
247 },
248
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100249 change_total_progress_bar: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700250 var job_percent = Math.floor(100 / change.jobs.length);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100251 var $bar_outter = $('<div />')
252 .addClass('progress zuul-change-total-result');
253
254 $.each(change.jobs, function (i, job) {
255 var result = job.result ? job.result.toLowerCase() : null;
256 if (result === null) {
257 result = job.url ? 'in progress' : 'queued';
258 }
259
Monty Taylor860bb0a2014-03-22 09:41:25 -0700260 if (result !== 'queued') {
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100261 var $bar_inner = $('<div />')
262 .addClass('progress-bar');
263
264 switch (result) {
265 case 'success':
266 $bar_inner.addClass('progress-bar-success');
267 break;
268 case 'lost':
269 case 'failure':
270 $bar_inner.addClass('progress-bar-danger');
271 break;
272 case 'unstable':
273 $bar_inner.addClass('progress-bar-warning');
274 break;
275 case 'in progress':
276 case 'queued':
277 break;
278 }
279 $bar_inner.attr('title', job.name)
280 .css('width', job_percent + '%');
281 $bar_outter.append($bar_inner);
282 }
283 });
284 return $bar_outter;
285 },
286
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100287 change_header: function(change) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700288 var change_id = change.id || 'NA';
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100289 if (change_id.length === 40) {
290 change_id = change_id.substr(0, 7);
Joshua Hesketh298c4912014-03-20 16:06:25 +1100291 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200292
Monty Taylor860bb0a2014-03-22 09:41:25 -0700293 var $change_link = $('<small />');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100294 if (change.url !== null) {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100295 $change_link.append(
Monty Taylor860bb0a2014-03-22 09:41:25 -0700296 $('<a />').attr('href', change.url).text(change.id)
Joshua Hesketh298c4912014-03-20 16:06:25 +1100297 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200298 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100299 else {
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100300 $change_link.text(change_id);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200301 }
Timo Tijhof51516cd2013-04-09 01:32:29 +0200302
Monty Taylor860bb0a2014-03-22 09:41:25 -0700303 var $change_progress_row_left = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100304 .addClass('col-xs-3')
305 .append($change_link);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700306 var $change_progress_row_right = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100307 .addClass('col-xs-9')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700308 .append(zuul.format.change_total_progress_bar(change));
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100309
Monty Taylor860bb0a2014-03-22 09:41:25 -0700310 var $change_progress_row = $('<div />')
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100311 .addClass('row')
312 .append($change_progress_row_left)
Monty Taylor860bb0a2014-03-22 09:41:25 -0700313 .append($change_progress_row_right);
Joshua Heskethf1b06ca2014-03-21 17:01:12 +1100314
Monty Taylor860bb0a2014-03-22 09:41:25 -0700315 var $project_span = $('<span />')
Joshua Heskethace48892014-03-22 17:18:31 +1100316 .addClass('change_project')
317 .text(change.project);
318
Monty Taylor860bb0a2014-03-22 09:41:25 -0700319 var $left = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100320 .addClass('col-xs-8')
Joshua Heskethace48892014-03-22 17:18:31 +1100321 .append($project_span, $('<br />'), $change_progress_row);
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100322
Monty Taylor860bb0a2014-03-22 09:41:25 -0700323 var remaining_time = zuul.format.time(
324 change.remaining_time, true);
325 var enqueue_time = zuul.format.enqueue_time(
326 change.enqueue_time);
327 var $remaining_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100328 .attr('title', 'Remaining Time').html(remaining_time);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700329 var $enqueue_time = $('<small />').addClass('time')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100330 .attr('title', 'Elapsed Time').html(enqueue_time);
331
Monty Taylor860bb0a2014-03-22 09:41:25 -0700332 var $right = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100333 .addClass('col-xs-4 text-right')
334 .append($remaining_time, $('<br />'), $enqueue_time);
335
Monty Taylor860bb0a2014-03-22 09:41:25 -0700336 var $header = $('<div />')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100337 .addClass('row')
338 .append($left, $right);
339 return $header;
340 },
341
342 change_list: function(jobs) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100343 var $list = $('<ul />')
344 .addClass('list-group');
345
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100346 $.each(jobs, function (i, job) {
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100347 var $item = $('<li />')
348 .addClass('list-group-item')
349 .addClass('zuul-change-job')
350 .append(zuul.format.job(job));
Joshua Hesketh298c4912014-03-20 16:06:25 +1100351 $list.append($item);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200352 });
353
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100354 return $list;
355 },
356
357 change_panel: function (change) {
358 var $header = $('<div />')
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100359 .addClass('panel-heading patchset-header')
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100360 .append(zuul.format.change_header(change));
361
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100362 var panel_id = change.id ? change.id.replace(',', '_')
363 : change.project.replace('/', '_') +
Monty Taylor860bb0a2014-03-22 09:41:25 -0700364 '-' + change.enqueue_time;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100365 var $panel = $('<div />')
Monty Taylor860bb0a2014-03-22 09:41:25 -0700366 .attr('id', panel_id)
Joshua Hesketh0ca1e2e2014-03-21 16:49:05 +1100367 .addClass('panel panel-default zuul-change')
368 .append($header)
369 .append(zuul.format.change_list(change.jobs));
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100370
371 $header.click(zuul.toggle_patchset);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100372 return $panel;
Timo Tijhof51516cd2013-04-09 01:32:29 +0200373 },
374
375 pipeline: function (pipeline) {
Joshua Hesketh298c4912014-03-20 16:06:25 +1100376 var $html = $('<div />')
Joshua Hesketh4863b602014-03-21 14:19:06 +1100377 .addClass('zuul-pipeline col-md-4')
Joshua Hesketh298c4912014-03-20 16:06:25 +1100378 .append($('<h3 />').text(pipeline.name));
379
Timo Tijhof51516cd2013-04-09 01:32:29 +0200380 if (typeof pipeline.description === 'string') {
Joshua Hesketh298c4912014-03-20 16:06:25 +1100381 $html.append(
382 $('<p />').append(
383 $('<small />').text(pipeline.description)
384 )
385 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200386 }
387
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100388 $.each(pipeline.change_queues,
389 function (queueNum, changeQueue) {
Timo Tijhof51516cd2013-04-09 01:32:29 +0200390 $.each(changeQueue.heads, function (headNum, changes) {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100391 if (pipeline.change_queues.length > 1 &&
392 headNum === 0) {
Timo Tijhof51516cd2013-04-09 01:32:29 +0200393 var name = changeQueue.name;
Joshua Hesketh6b1a2182014-03-21 14:40:04 +1100394 var short_name = name;
395 if (short_name.length > 32) {
396 short_name = short_name.substr(0, 32) + '...';
Timo Tijhof51516cd2013-04-09 01:32:29 +0200397 }
Joshua Hesketh298c4912014-03-20 16:06:25 +1100398 $html.append(
399 $('<p />')
400 .text('Queue: ')
401 .append(
402 $('<abbr />')
403 .attr('title', name)
404 .text(short_name)
405 )
406 );
Timo Tijhof51516cd2013-04-09 01:32:29 +0200407 }
408 $.each(changes, function (changeNum, change) {
Joshua Heskethace48892014-03-22 17:18:31 +1100409 var $panel = zuul.format.change_panel(change);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700410 $html.append($panel);
Joshua Heskethace48892014-03-22 17:18:31 +1100411 zuul.display_patchset($panel);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200412 });
413 });
414 });
Joshua Hesketh298c4912014-03-20 16:06:25 +1100415 return $html;
Joshua Heskethace48892014-03-22 17:18:31 +1100416 },
417
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100418 filter_form_group: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100419 // Update the filter form with a clear button if required
420
421 var $label = $('<label />')
422 .addClass('control-label')
423 .attr('for', 'filter_string')
424 .text('Filters')
425 .css('padding-right', '0.5em');
426
427 var $input = $('<input />')
428 .attr('type', 'text')
429 .attr('id', 'filter_string')
430 .addClass('form-control')
431 .attr('title',
432 'project(s), pipeline(s) or review(s) comma ' +
433 'separated')
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100434 .attr('value', current_filter);
Joshua Heskethace48892014-03-22 17:18:31 +1100435
436 $input.change(zuul.handle_filter_change);
437
438 var $clear_icon = $('<span />')
439 .addClass('form-control-feedback')
440 .addClass('glyphicon glyphicon-remove-circle')
441 .attr('id', 'filter_form_clear_box')
442 .attr('title', 'clear filter')
443 .css('cursor', 'pointer');
444
445 $clear_icon.click(function() {
446 $('#filter_string').val('').change();
447 });
448
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100449 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100450 $clear_icon.hide();
451 }
452
453 var $form_group = $('<div />')
454 .addClass('form-group has-feedback')
455 .append($label, $input, $clear_icon);
456 return $form_group;
457 },
458
Joshua Heskethae230f62014-03-22 22:14:44 +1100459 expand_form_group: function() {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700460 var expand_by_default = (
Joshua Heskethae230f62014-03-22 22:14:44 +1100461 read_cookie('zuul_expand_by_default', false) === 'true');
462
Monty Taylor860bb0a2014-03-22 09:41:25 -0700463 var $checkbox = $('<input />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100464 .attr('type', 'checkbox')
465 .attr('id', 'expand_by_default')
466 .prop('checked', expand_by_default)
467 .change(zuul.handle_expand_by_default);
468
Monty Taylor860bb0a2014-03-22 09:41:25 -0700469 var $label = $('<label />')
Joshua Heskethae230f62014-03-22 22:14:44 +1100470 .css('padding-left', '1em')
471 .html('Expand by default: ')
472 .append($checkbox);
473
474 var $form_group = $('<div />')
475 .addClass('checkbox')
476 .append($label);
477 return $form_group;
478 },
479
480 control_form: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100481 // Build the filter form filling anything from cookies
482
Monty Taylor860bb0a2014-03-22 09:41:25 -0700483 var $control_form = $('<form />')
Joshua Heskethace48892014-03-22 17:18:31 +1100484 .attr('role', 'form')
485 .addClass('form-inline')
486 .submit(zuul.handle_filter_change);
487
Joshua Heskethae230f62014-03-22 22:14:44 +1100488 $control_form
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100489 .append(zuul.format.filter_form_group())
Joshua Heskethae230f62014-03-22 22:14:44 +1100490 .append(zuul.format.expand_form_group());
491
492 return $control_form;
Joshua Heskethace48892014-03-22 17:18:31 +1100493 },
Timo Tijhof51516cd2013-04-09 01:32:29 +0200494 },
495
496 emit: function () {
497 $jq.trigger.apply($jq, arguments);
498 return this;
499 },
500 on: function () {
501 $jq.on.apply($jq, arguments);
502 return this;
503 },
504 one: function () {
505 $jq.one.apply($jq, arguments);
506 return this;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100507 },
508
509 toggle_patchset: function(e) {
510 // Toggle showing/hiding the patchset when the header is clicked
511 // Grab the patchset panel
512 var $panel = $(e.target).parents('.zuul-change');
513 var $body = $panel.children(':not(.patchset-header)');
514 $body.toggle(200);
515 var collapsed_index = zuul.collapsed_exceptions.indexOf(
516 $panel.attr('id'));
Monty Taylor860bb0a2014-03-22 09:41:25 -0700517 if (collapsed_index === -1 ) {
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100518 // Currently not an exception, add it to list
519 zuul.collapsed_exceptions.push($panel.attr('id'));
520 }
521 else {
522 // Currently an except, remove from exceptions
523 zuul.collapsed_exceptions.splice(collapsed_index, 1);
524 }
525 },
526
Joshua Heskethace48892014-03-22 17:18:31 +1100527 display_patchset: function($panel, animate) {
528 // Determine if to show or hide the patchset and/or the results
529 // when loaded
530
531 // See if we should hide the body/results
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100532 var $body = $panel.children(':not(.patchset-header)');
Joshua Heskethae230f62014-03-22 22:14:44 +1100533 var expand_by_default = $('#expand_by_default').prop('checked');
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100534 var collapsed_index = zuul.collapsed_exceptions.indexOf(
535 $panel.attr('id'));
Monty Taylor860bb0a2014-03-22 09:41:25 -0700536 if (expand_by_default && collapsed_index === -1 ||
537 !expand_by_default && collapsed_index !== -1) {
Joshua Heskethae230f62014-03-22 22:14:44 +1100538 // Expand by default, or is an exception
539 $body.show(animate);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100540 }
541 else {
Joshua Heskethae230f62014-03-22 22:14:44 +1100542 $body.hide(animate);
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100543 }
Joshua Heskethace48892014-03-22 17:18:31 +1100544
545 // Check if we should hide the whole panel
546 var panel_project = $panel.find('.change_project').text()
547 .toLowerCase();
548 var panel_pipeline = $panel.parents('.zuul-pipeline')
549 .children('h3').text().toLowerCase();
550 var panel_change = $panel.attr('id');
Monty Taylor860bb0a2014-03-22 09:41:25 -0700551 if (current_filter !== '') {
552 var show_panel = false;
553 var filter = current_filter.trim().split(/[\s,]+/);
Joshua Heskethace48892014-03-22 17:18:31 +1100554 $.each(filter, function(index, f_val) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700555 if (f_val !== '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100556 f_val = f_val.toLowerCase();
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100557 if (panel_project.indexOf(f_val) !== -1 ||
558 panel_pipeline.indexOf(f_val) !== -1 ||
559 panel_change.indexOf(f_val) !== -1) {
Joshua Heskethace48892014-03-22 17:18:31 +1100560 show_panel = true;
561 }
562 }
563 });
Monty Taylor860bb0a2014-03-22 09:41:25 -0700564 if (show_panel === true) {
Joshua Heskethace48892014-03-22 17:18:31 +1100565 $panel.show(animate);
566 }
567 else {
568 $panel.hide(animate);
569 }
570 }
571 else {
572 $panel.show(animate);
573 }
574 },
575
Monty Taylor860bb0a2014-03-22 09:41:25 -0700576 handle_filter_change: function() {
Joshua Heskethace48892014-03-22 17:18:31 +1100577 // Update the filter and save it to a cookie
578 current_filter = $('#filter_string').val();
579 set_cookie('zuul_filter_string', current_filter);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700580 if (current_filter === '') {
Joshua Heskethace48892014-03-22 17:18:31 +1100581 $('#filter_form_clear_box').hide();
582 }
583 else {
584 $('#filter_form_clear_box').show();
585 }
586
587 $('.zuul-change').each(function(index, obj) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700588 var $panel = $(obj);
Joshua Heskethace48892014-03-22 17:18:31 +1100589 zuul.display_patchset($panel, 200);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700590 });
Joshua Heskethace48892014-03-22 17:18:31 +1100591 return false;
Joshua Heskethdb8046e2014-03-21 18:42:25 +1100592 },
Joshua Heskethae230f62014-03-22 22:14:44 +1100593
594 handle_expand_by_default: function(e) {
595 // Handle toggling expand by default
596 set_cookie('zuul_expand_by_default', e.target.checked);
597 zuul.collapsed_exceptions = [];
598 $('.zuul-change').each(function(index, obj) {
Monty Taylor860bb0a2014-03-22 09:41:25 -0700599 var $panel = $(obj);
Joshua Heskethae230f62014-03-22 22:14:44 +1100600 zuul.display_patchset($panel, 200);
Monty Taylor860bb0a2014-03-22 09:41:25 -0700601 });
Joshua Heskethae230f62014-03-22 22:14:44 +1100602 },
Timo Tijhof51516cd2013-04-09 01:32:29 +0200603 };
604
Joshua Heskethace48892014-03-22 17:18:31 +1100605 current_filter = read_cookie('zuul_filter_string', current_filter);
606
Timo Tijhof51516cd2013-04-09 01:32:29 +0200607 $jq = $(zuul);
608
609 $jq.on('update-start', function () {
610 $container.addClass('zuul-container-loading');
611 $indicator.addClass('zuul-spinner-on');
612 });
613
614 $jq.on('update-end', function () {
615 $container.removeClass('zuul-container-loading');
616 setTimeout(function () {
617 $indicator.removeClass('zuul-spinner-on');
Joshua Hesketh876ca512014-03-21 18:44:22 +1100618 }, 500);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200619 });
620
621 $jq.one('update-end', function () {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100622 // Do this asynchronous so that if the first update adds a message, it
623 // will not animate while we fade in the content. Instead it simply
624 // appears with the rest of the content.
Timo Tijhof51516cd2013-04-09 01:32:29 +0200625 setTimeout(function () {
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100626 // Fade in the content
627 $container.addClass('zuul-container-ready');
Timo Tijhof51516cd2013-04-09 01:32:29 +0200628 });
629 });
630
631 $(function ($) {
Joshua Hesketh4863b602014-03-21 14:19:06 +1100632 $msg = $('<div />').addClass('alert').hide();
Monty Taylor860bb0a2014-03-22 09:41:25 -0700633 $indicator = $('<button class="btn pull-right zuul-spinner">' +
634 'updating ' +
635 '<span class="glyphicon glyphicon-refresh"></span>' +
636 '</button>');
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100637 $queueInfo = $('<p>Queue lengths: <span>0</span> events, ' +
638 '<span>0</span> results.</p>');
Joshua Hesketh298c4912014-03-20 16:06:25 +1100639 $queueEventsNum = $queueInfo.find('span').eq(0);
640 $queueResultsNum = $queueEventsNum.next();
Joshua Heskethace48892014-03-22 17:18:31 +1100641
Monty Taylor860bb0a2014-03-22 09:41:25 -0700642 var $control_form = zuul.format.control_form();
Joshua Heskethace48892014-03-22 17:18:31 +1100643
Joshua Hesketh1ed6f9d2014-03-31 22:53:06 +1100644 $pipelines = $('<div class="row"></div>');
Monty Taylor860bb0a2014-03-22 09:41:25 -0700645 var $zuulVersion = $('<p>Zuul version: <span id="zuul-version-span">' +
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100646 '</span></p>');
Monty Taylor860bb0a2014-03-22 09:41:25 -0700647 var $lastReconf = $('<p>Last reconfigured: ' +
Joshua Heskethcbdcca12014-03-20 16:06:25 +1100648 '<span id="last-reconfigured-span"></span></p>');
Timo Tijhof51516cd2013-04-09 01:32:29 +0200649
Joshua Hesketh4863b602014-03-21 14:19:06 +1100650 $container = $('#zuul-container').append($msg, $indicator,
Joshua Heskethae230f62014-03-22 22:14:44 +1100651 $queueInfo, $control_form,
Joshua Heskethace48892014-03-22 17:18:31 +1100652 $pipelines, $zuulVersion,
653 $lastReconf);
Timo Tijhof51516cd2013-04-09 01:32:29 +0200654
655 zuul.schedule();
656
657 $(document).on({
658 'show.visibility': function () {
659 zuul.enabled = true;
660 zuul.update();
661 },
662 'hide.visibility': function () {
663 zuul.enabled = false;
664 }
665 });
666 });
667}(jQuery));