summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/toaster/toastergui/static/js/base.js
blob: 9424b6c3284e552cb4d70e6a96d175087fbb3ba2 (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
function basePageInit (ctx) {

  var newBuildButton = $("#new-build-button");
  /* Hide the button if we're on the project,newproject or importlyaer page
   * or if there are no projects yet defined
   */
  if (ctx.numProjects == 0 || ctx.currentUrl.search('newproject|project/\\d$|importlayer$') > 0){
      newBuildButton.hide();
      return;
  }

  var currentProjectId = libtoaster.ctx.projectId;

  /* Hide the change project icon when there is only one project */
  if (ctx.numProjects == 1){
     $('#project .icon-pencil').hide();
  }

  newBuildButton.show().removeAttr("disabled");


  _checkProjectBuildable()
  _setupNewBuildButton();


  function _checkProjectBuildable(){
    if (libtoaster.ctx.projectId == undefined)
      return;

    libtoaster.getProjectInfo(ctx.projectInfoUrl, libtoaster.ctx.projectId,
      function(data){
        if (data.machine.name == undefined || data.layers.length == 0) {
          /* we can't build anything with out a machine and some layers */
          $("#new-build-button #targets-form").hide();
          $("#new-build-button .alert").show();
        } else {
          $("#new-build-button #targets-form").show();
          $("#new-build-button .alert").hide();
        }
    }, null);
  }

  function _setupNewBuildButton() {
    /* Setup New build button */
    var newBuildProjectInput = $("#new-build-button #project-name-input");
    var newBuildTargetBuildBtn = $("#new-build-button #build-button");
    var newBuildTargetInput = $("#new-build-button #build-target-input");
    var newBuildProjectSaveBtn = $("#new-build-button #save-project-button");
    var selectedTarget;
    var selectedProject;

    /* If we don't have a current project then present the set project
     * form.
     */
    if (libtoaster.ctx.projectId == undefined) {
      $('#change-project-form').show();
      $('#project .icon-pencil').hide();
    }


    libtoaster.makeTypeahead(newBuildProjectInput, libtoaster.ctx.projectsUrl, { format : "json" }, function(item){
        /* successfully selected a project */
        newBuildProjectSaveBtn.removeAttr("disabled");
        selectedProject = item;
    });

    /* Any typing in the input apart from enter key is going to invalidate
     * the value that has been set by selecting a suggestion from the typeahead
     */
    newBuildProjectInput.on('input', function(event) {
        if (event.keyCode == 13)
          return;
        newBuildProjectSaveBtn.attr("disabled", "disabled");
    });

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

    newBuildTargetBuildBtn.click(function() {
      if (!newBuildTargetInput.val())
        return;

      if (!selectedTarget)
        selectedTarget = { name: newBuildTargetInput.val() };
      /* fire and forget */
      libtoaster.startABuild(ctx.projectBuildUrl, libtoaster.ctx.projectId, selectedTarget.name, null, null);
      window.location.replace(libtoaster.ctx.projectPageUrl);
    });

    newBuildProjectSaveBtn.click(function() {
      libtoaster.ctx.projectId = selectedProject.pk
      /* Update the typeahead project_id paramater */
      _checkProjectBuildable();

      /* we set the effective context of the page to the currently selected project */
      /* TBD: do we override even if we already have a context project ?? */
      /* TODO: replace global library context with references to the "selected" project */
      libtoaster.ctx.projectPageUrl = selectedProject.projectPageUrl;
      libtoaster.ctx.xhrProjectEditUrl = selectedProject.xhrProjectEditUrl;
      libtoaster.ctx.projectName = selectedProject.name;
      libtoaster.ctx.projectId = selectedProject.id;

      ctx.projectBuildUrl = selectedProject.projectBuildUrl;

      /* we can create a target typeahead only after we have a project selected */
      newBuildTargetInput.prop("disabled", false);
      newBuildTargetBuildBtn.prop("disabled", false);

      libtoaster.makeTypeahead(newBuildTargetInput, selectedProject.xhrProjectDataTypeaheadUrl, { type : "targets" }, function(item){
        /* successfully selected a target */
        selectedTarget = item;
	    });

      newBuildTargetInput.val("");

      /* set up new form aspect */
      $("#new-build-button #project a").text(selectedProject.name).attr('href', libtoaster.ctx.projectPageUrl);
      $("#new-build-button .alert a").attr('href', libtoaster.ctx.projectPageUrl);
      $("#project .icon-pencil").show();

      $("#change-project-form").slideUp({ 'complete' : function() {
          $("#new-build-button #project").show();
      }});
    });

    $('#new-build-button #project .icon-pencil').click(function() {
      newBuildProjectSaveBtn.attr("disabled", "disabled");
      newBuildProjectInput.val($("#new-build-button #project a").text());
      $("#cancel-change-project").show();
      $(this).parent().hide();
      $("#change-project-form").slideDown();
    });

    $("#new-build-button #cancel-change-project").click(function() {
      $("#change-project-form").hide(function(){
        $('#new-build-button #project').show();
      });

      newBuildProjectInput.val("");
      newBuildProjectSaveBtn.attr("disabled", "disabled");
    });

    /* Keep the dropdown open even unless we click outside the dropdown area */
    $(".new-build").click (function(event) {
      event.stopPropagation();
    });
  };

}