aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/js/application.js
blob: 00723b0fe60d3b34254659af71b0d9737fb3c336 (plain)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
function showMessage(div_id, message) {
  $j('#' + div_id + 'msg').html(message);
  $j('#' + div_id).show();
}
function error(message) {
  showMessage('error', message);
}
function warning(message) {
  showMessage('warning', message);
}
function info(message) {
  showMessage('info', message);
}
function toggleFav(resourceId, elt) {
  $j.ajax({type: 'POST', dataType: 'json', url: baseUrl + '/favourites/toggle/' + resourceId,
    success: function (data) {
      var star = $j(elt);
      star.removeClass('icon-favorite icon-not-favorite');
      star.addClass(data['css']);
      star.attr('title', data['title']);
    }});
}

function dashboardParameters() {
  var queryString = window.location.search;
  var parameters = "";

  var matchDashboard = queryString.match(/did=\d+/);
  if (matchDashboard && $j('#is-project-dashboard').length === 1) {
    parameters += (matchDashboard[0] + "&");
  }

  var matchPeriod = queryString.match(/period=\d+/);
  if (matchPeriod) {
    // If we have a match for period, check that it is not project-specific
    var period = parseInt(/period=(\d+)/.exec(queryString)[1]);
    if (period <= 3) {
      parameters += matchPeriod[0] + "&";
    }
  }

  if (parameters !== "") {
    parameters = "?" + parameters;
  }
  return parameters;
}

function resourceViewerOnBulkIssues() {
  var issuesTab = 'tab=issues';
  if (window.location.search.indexOf('tab=') >= 0) {
    // If a tab is already selected
    if (window.location.search.indexOf(issuesTab) >= 0) {
      // If tab is issues, keep it and reload page
      window.location.reload();
    } else {
      // Else, switch to issues tab
      window.location.search = window.location.search.replace(/tab=\w+/, issuesTab);
    }
  } else {
    // No tab selected, see how to add tab parameter
    if (window.location.search.startsWith('?')) {
      window.location.search += ('&' + issuesTab);
    } else {
      window.location.search += ('?' + issuesTab);
    }
  }
}

var SelectBox = {
  cache: {},
  init: function (id) {
    var box = document.getElementById(id);
    var node;
    SelectBox.cache[id] = [];
    var cache = SelectBox.cache[id];
    for (var i = 0; (node = box.options[i]); i++) {
      cache.push({value: node.value, text: node.text, displayed: 1});
    }
  },
  redisplay: function (id) {
    // Repopulate HTML select box from cache
    var box = document.getElementById(id);
    // clear all options
    box.options.length = 0;
    for (var i = 0, j = SelectBox.cache[id].length; i < j; i++) {
      var node = SelectBox.cache[id][i];
      if (node.displayed) {
        box.options[box.options.length] = new Option(node.text, node.value, false, false);
      }
    }
  },
  filter: function (id, text) {
    // Redisplay the HTML select box, displaying only the choices containing ALL
    // the words in text. (It's an AND search.)
    var tokens = text.toLowerCase().split(/\s+/);
    for (var i = 0, n = SelectBox.cache[id].length; i < n; i++) {
      var node = SelectBox.cache[id][i];
      node.displayed = 1;
      for (var j = 0, k = tokens.length; j < k; j++) {
        var token = tokens[j];
        if (node.text.toLowerCase().indexOf(token) == -1) {
          node.displayed = 0;
        }
      }
    }
    SelectBox.redisplay(id);
  },
  delete_from_cache: function (id, value) {
    var delete_index = null;
    for (var i = 0, n = SelectBox.cache[id].length; i < n; i++) {
      var node = SelectBox.cache[id][i];
      if (node.value === value) {
        delete_index = i;
        break;
      }
    }
    var j = SelectBox.cache[id].length - 1;
    for (i = delete_index; i < j; i++) {
      SelectBox.cache[id][i] = SelectBox.cache[id][i + 1];
    }
    SelectBox.cache[id].length--;
  },
  add_to_cache: function (id, option) {
    SelectBox.cache[id].push({value: option.value, text: option.text, displayed: 1});
  },
  cache_contains: function (id, value) {
    // Check if an item is contained in the cache
    for (var i = 0, j = SelectBox.cache[id].length; i < j; i++) {
      var node = SelectBox.cache[id][i];
      if (node.value === value) {
        return true;
      }
    }
    return false;
  },
  move: function (from, to) {
    var from_box = document.getElementById(from);
    for (var i = 0, j = from_box.options.length; i < j; i++) {
      var option = from_box.options[i];
      if (option.selected && SelectBox.cache_contains(from, option.value)) {
        SelectBox.add_to_cache(to, {value: option.value, text: option.text, displayed: 1});
        SelectBox.delete_from_cache(from, option.value);
      }
    }
    SelectBox.redisplay(from);
    SelectBox.redisplay(to);
  },
  move_all: function (from, to) {
    var from_box = document.getElementById(from);
    for (var i = 0, j = from_box.options.length; i < j; i++) {
      var option = from_box.options[i];
      if (SelectBox.cache_contains(from, option.value)) {
        SelectBox.add_to_cache(to, {value: option.value, text: option.text, displayed: 1});
        SelectBox.delete_from_cache(from, option.value);
      }
    }
    SelectBox.redisplay(from);
    SelectBox.redisplay(to);
  },
  sort: function (id) {
    SelectBox.cache[id].sort(function (a, b) {
      a = a.text.toLowerCase();
      b = b.text.toLowerCase();
      try {
        if (a > b) {
          return 1;
        }
        if (a < b) {
          return -1;
        }
      }
      catch (e) {
        // silently fail on IE 'unknown' exception
      }
      return 0;
    });
  },
  select_all: function (id) {
    var box = document.getElementById(id);
    for (var i = 0; i < box.options.length; i++) {
      box.options[i].selected = 'selected';
    }
  }
};


var treemaps = {};

function treemapById(id) {
  return treemaps[id];
}
var TreemapContext = function (rid, label) {
  this.rid = rid;
  this.label = label;
};

/**
 * HTML elements :
 * tm-#{id} : required treemap container
 * tm-bc-#{id} : required breadcrumb
 * tm-loading-#{id} : optional loading icon
 */
var Treemap = function (id, sizeMetric, colorMetric, heightPercents) {
  this.id = id;
  this.sizeMetric = sizeMetric;
  this.colorMetric = colorMetric;
  this.breadcrumb = [];
  treemaps[id] = this;
  this.rootNode().height(this.rootNode().width() * heightPercents / 100);
  this.initNodes();

};
Treemap.prototype.currentContext = function () {
  if (this.breadcrumb.length > 0) {
    return this.breadcrumb[this.breadcrumb.length - 1];
  }
  return null;
};
Treemap.prototype.load = function () {
  var context = this.currentContext();
  var output = '';
  this.breadcrumb.forEach(function (ctx) {
    output += ctx.label + '&nbsp;/&nbsp;';
  });
  $j('#tm-bc-' + this.id).html(output);
  $j('#tm-loading-' + this.id).show();
  var self = this;
  $j.ajax({
    type: "GET",
    url: baseUrl + '/treemap/index?html_id=' + this.id + '&size_metric=' + this.sizeMetric + '&color_metric=' + this.colorMetric + '&resource=' + context.rid,
    dataType: "html",
    success: function (data) {
      if (data.length > 1) {
        self.rootNode().html(data);
        self.initNodes();
      } else {
        // SONAR-3524
        // When data is empty, do not display it, revert breadcrumb state and display a message to user
        self.breadcrumb.pop();
        $j("#tm-bottom-level-reached-msg-" + self.id).show();
      }
      $j("#tm-loading-" + self.id).hide();
    }
  });
};
Treemap.prototype.rootNode = function () {
  return $j('#tm-' + this.id);
};

Treemap.prototype.initNodes = function () {
  var self = this;
  $j('#tm-' + this.id).find('a').each(function () {
    $j(this).on("click", function (event) {
      event.stopPropagation();
    });
  });
  $j('#tm-' + this.id).find('[rid]').each(function () {
    $j(this).on("contextmenu", function (event) {
      event.stopPropagation();
      event.preventDefault();
      $j("#tm-bottom-level-reached-msg-" + self.id).hide();
      // right click
      if (self.breadcrumb.length > 1) {
        self.breadcrumb.pop();
        self.load();
      } else if (self.breadcrumb.length === 1) {
        $j("#tm-loading-" + self.id).show();
        location.reload();
      }
      return false;
    });
    $j(this).on("click", function () {
        var source = $j(this);
        var rid = source.attr('rid');
        var context = new TreemapContext(rid, source.text());
        self.breadcrumb.push(context);
        self.load();
      }
    );
  });
};

function openModalWindow(url, options) {
  var width = (options && options['width']) || 540;
  var $dialog = $j('#modal');
  if (!$dialog.length) {
    $dialog = $j('<div id="modal" class="ui-widget-overlay ui-front"></div>').appendTo('body');
  }
  $j.get(url,function (html) {
    $dialog.removeClass('ui-widget-overlay');
    $dialog.html(html);
    $dialog
      .dialog({
        dialogClass: "no-close",
        width: width,
        draggable: false,
        autoOpen: false,
        modal: true,
        minHeight: 50,
        resizable: false,
        title: null,
        close: function () {
          $j('#modal').remove();
        }
      });
    $dialog.dialog("open");
  }).fail(function () {
      alert("Server error. Please contact your administrator.");
    }).always(function () {
      $dialog.removeClass('ui-widget-overlay');
    });
  return false;
}

(function ($j) {
  $j.fn.extend({
    openModal: function() {
      return this.each(function () {
        var obj = $j(this);
        var url = obj.attr('modal-url') || obj.attr('href');
        return openModalWindow(url, {'width': obj.attr('modal-width')});
      });
    },
    modal: function () {
      return this.each(function () {
        var obj = $j(this);
        obj.unbind('click');
        var $link = obj.bind('click', function () {
          $link.openModal();
          return false;
        });
      });
    },
    modalForm: function (ajax_options) {
      return this.each(function () {
        var obj = $j(this);
        obj.submit(function () {
          $j('input[type=submit]', this).attr('disabled', 'disabled');
          $j.ajax($j.extend({
            type: 'POST',
            url: obj.attr('action'),
            data: obj.serialize(),
            success: function () {
              window.location.reload();
            },
            error: function (xhr) {
              // If the modal window has defined a modal-error element, then returned text must be displayed in it
              var errorElt = obj.find(".modal-error");
              if (errorElt.length) {
                // Hide all loading images
                $j('.loading-image').addClass("hidden");
                // Re activate submit button
                $j('input[type=submit]', obj).removeAttr('disabled');
                errorElt.show();
                errorElt.html(xhr.responseText);
              } else {
                // otherwise replace modal window by the returned text
                $j("#modal").html(xhr.responseText);
              }
            }
          }, ajax_options));
          return false;
        });
      });
    }
  });
})(jQuery);

function closeModalWindow() {
  $j('#modal').dialog('close');
  return false;
}

function supportsHTML5Storage() {
  try {
    return 'localStorage' in window && window['localStorage'] !== null;
  } catch (e) {
    return false;
  }
}

//******************* HANDLING OF ACCORDION NAVIGATION [BEGIN] ******************* //

function openAccordionItem(url, elt, updateCurrentElement) {
  var htmlClass = 'accordion-item';
  var currentElement = $j(elt).closest('.'+ htmlClass);

  // Create loading image
  var loadingImg = new Image();
  loadingImg.src = baseUrl + "/images/loading.gif";
  loadingImg.className = 'accordion-loading';
  var loading = $j(loadingImg);
  var existingLoading = currentElement.find('.accordion-loading');
  if (updateCurrentElement && existingLoading.length) {
    existingLoading.show();
    loading.hide();
  }

  // Remove elements under current element
  if (currentElement.length) {
    // Fix the height in order to not change the position on the screen when removing elements under current element
    var elementToRemove = currentElement.nextAll('.'+ htmlClass);
    if (elementToRemove.height()) {
      $j("#accordion-panel").height($j("#accordion-panel").height() + elementToRemove.height());
    }
    // Remove all accordion items after current element
    elementToRemove.remove();
    // Display loading image only if not already displayed (if previous call was not finished)
    if (currentElement.next('.accordion-loading').length === 0) {
      loading.insertAfter(currentElement);
    }
  } else {
    // Current element is not in a working view, remove all working views
    $j('.'+ htmlClass).remove();
    // Display loading image only if not already displayed (if previous call was not finished)
    if ($j("#accordion-panel").next('.accordion-loading').length === 0) {
      loading.insertAfter($j("#accordion-panel"));
    }
  }

  // Get content from url
  var ajaxRequest = $j.ajax({
      url: url
      }).fail(function (jqXHR, textStatus) {
        var error = "Server error. Please contact your administrator. The status of the error is : "+ jqXHR.status + ", textStatus is : "+ textStatus;
        console.log(error);
        $j("#accordion-panel").append($j('<div class="error">').append(error));
      }).done(function (html) {
        if (currentElement.length) {
          var body = currentElement.find('.accordion-item-body');
          if (!updateCurrentElement && !body.hasClass('accordion-item-body-medium')) {
            body.addClass("accordion-item-body-medium");
            elt.scrollIntoView(false);
          }
        } else {
          $j("#accordion-panel").height('auto');

          // Current element is not in a working view, remove again all working views to purge elements that could be added just before this one
          $j('.'+ htmlClass).remove();
        }

        if (updateCurrentElement) {
          // Fix the height in order to not change the position on the screen
          var prevHeight = $j("#accordion-panel").height();
          currentElement.html(html);
          $j("#accordion-panel").height('auto');
          var newHeight = $j("#accordion-panel").height();
          if (prevHeight > newHeight) {
            $j("#accordion-panel").height(prevHeight);
          } else {
            $j("#accordion-panel").height(newHeight);
          }
        } else {
          // Add new item add the end of the panel and restore the height param
          var nbElement = $j("."+htmlClass).size();
          var newElement = $j('<div id="'+ htmlClass + nbElement +'" class="'+ htmlClass +'">');
          $j("#accordion-panel").append(newElement);

          // Add html after having adding the new element in the page in order to scripts (for instance for GWT) to be well executed
          newElement.append(html);
          $j("#accordion-panel").height('auto');

          // Set the focus on the top of the current item with animation
          if (currentElement.length) {
            $j('html, body').animate({
              scrollTop: currentElement.offset().top}, 500
            );
          }
        }
        loading.remove();
      });
  return ajaxRequest;
}


function expandAccordionItem(elt) {
  var currentElement = $j(elt).closest('.accordion-item');
  currentElement.find('.accordion-item-body').removeClass("accordion-item-body-medium");
}

//******************* HANDLING OF ACCORDION NAVIGATION [END] ******************* //


//******************* HANDLING OF DROPDOWN MENUS [BEGIN] ******************* //

var currentlyDisplayedDropdownMenu;

var hideCurrentDropdownMenu = function () {
  if (currentlyDisplayedDropdownMenu) {
    currentlyDisplayedDropdownMenu.hide();
  }
  $j(document).unbind('mouseup', hideCurrentDropdownMenu);
};

var clickOnDropdownMenuLink = function (event) {
  var link = $j(event.target).children('a');
  if (link) {
    var href = link.attr('href');
    if (href && href.length > 1) {
      // there's a real link, not a href="#"
      window.location = href;
    } else {
      // otherwise, this means that the link is handled with an onclick event (for Ajax calls)
      link.click();
    }
  }
};

function showDropdownMenu(menuId) {
  showDropdownMenuOnElement($j('#' + menuId));
}

function showDropdownMenuOnElement(elt) {
  var dropdownElt = $j(elt);

  if (dropdownElt === currentlyDisplayedDropdownMenu) {
    currentlyDisplayedDropdownMenu = "";
  } else {
    currentlyDisplayedDropdownMenu = dropdownElt;
    $j(document).mouseup(hideCurrentDropdownMenu);

    var dropdownChildren = dropdownElt.find('li');
    dropdownChildren.unbind('click');
    dropdownChildren.click(clickOnDropdownMenuLink);
    dropdownElt.show();
  }
}

//******************* HANDLING OF DROPDOWN MENUS [END] ******************* //

function openPopup(url, popupId) {
  window.open(url,popupId,'height=800,width=900,scrollbars=1,resizable=1');
  return false;
}


jQuery(function() {

  // Initialize top search
  jQuery('#searchInput').topSearch({
    minLength: 2,
    results: '#searchResourcesResults',
    spinner: '#searchingResources'
  });


  // Process login link in order to add the anchor
  jQuery('#login-link').on('click', function(e) {
    e.preventDefault();
    var href = jQuery(this).prop('href'),
        hash = window.location.hash;
    if (hash.length > 0) {
      href += decodeURIComponent(hash);
    }
    window.location = href;
  });


  // Define global shortcuts
  key('s', function() {
    jQuery('#searchInput').focus();
    return false;
  });
});