aboutsummaryrefslogtreecommitdiffstats
path: root/lib/toaster/toastergui/static/js/projecttopbar.js
blob: b6ad380c19f4cfdc8f8b025fba5de0a58184d820 (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
'use strict';

function projectTopBarInit(ctx) {

  var projectNameForm = $("#project-name-change-form");
  var projectNameContainer = $("#project-name-container");
  var projectName = $("#project-name");
  var projectNameFormToggle = $("#project-change-form-toggle");
  var projectNameChangeCancel = $("#project-name-change-cancel");
  var newBuildTargetInput = $("#build-input");
  var newBuildTargetBuildBtn = $("#build-button");
  var selectedTarget;

  /* Project name change functionality */
  projectNameFormToggle.click(function(e){
    e.preventDefault();
    projectNameContainer.hide();
    projectNameForm.fadeIn();
  });

  projectNameChangeCancel.click(function(e){
    e.preventDefault();
    projectNameForm.hide();
    projectNameContainer.fadeIn();
  });

  $("#project-name-change-btn").click(function(){
    var newProjectName = $("#project-name-change-input").val();

    libtoaster.editCurrentProject({ projectName: newProjectName }, function (){
      projectName.html(newProjectName);
      libtoaster.ctx.projectName = newProjectName;
      projectNameChangeCancel.click();
    });
  });

  /* Nav bar activate state switcher */
  $("#project-topbar .nav li a").each(function(){
    if (window.location.pathname === $(this).attr('href'))
      $(this).parent().addClass('active');
    else
      $(this).parent().removeClass('active');
  });

  /* Recipe build input functionality */
  if (ctx.numProjectLayers > 0 && ctx.machine){
    newBuildTargetInput.removeAttr("disabled");
  }

  libtoaster.makeTypeahead(newBuildTargetInput,
    libtoaster.ctx.recipesTypeAheadUrl, {}, function (item) {
     selectedTarget = item;
     newBuildTargetBuildBtn.removeAttr("disabled");
  });

  newBuildTargetInput.on('input', function () {
    if ($(this).val().length === 0) {
      newBuildTargetBuildBtn.attr("disabled", "disabled");
    } else {
      newBuildTargetBuildBtn.removeAttr("disabled");
    }
  });

  newBuildTargetBuildBtn.click(function (e) {
    e.preventDefault();
    if (!newBuildTargetInput.val()) {
      return;
    }
    /* We use the value of the input field so as to maintain any command also
     * added e.g. core-image-minimal:clean and because we can build targets
     * that toaster doesn't yet know about
     */
    selectedTarget = { name: newBuildTargetInput.val() };

    /* Fire off the build */
    libtoaster.startABuild(libtoaster.ctx.projectBuildsUrl,
      null, selectedTarget.name, function(){
      window.location.replace(libtoaster.ctx.projectBuildsUrl);
    }, null);
  });
}