1'use strict';
2
3function projectTopBarInit(ctx) {
4
5  var projectNameForm = $("#project-name-change-form");
6  var projectNameContainer = $("#project-name-container");
7  var projectName = $(".project-name");
8  var projectNameFormToggle = $("#project-change-form-toggle");
9  var projectNameChangeCancel = $("#project-name-change-cancel");
10
11  // this doesn't exist for command-line builds
12  var newBuildTargetInput = $("#build-input");
13
14  var newBuildTargetBuildBtn = $("#build-button");
15  var selectedTarget;
16
17  var updateProjectBtn = $("#update-project-button");
18  var cancelProjectBtn = $("#cancel-project-button");
19
20  /* Project name change functionality */
21  projectNameFormToggle.click(function(e){
22    e.preventDefault();
23    projectNameContainer.hide();
24    projectNameForm.fadeIn();
25  });
26
27  projectNameChangeCancel.click(function(e){
28    e.preventDefault();
29    projectNameForm.hide();
30    projectNameContainer.fadeIn();
31    $("#project-name-change-input").val(projectName.first().text());
32  });
33
34  $("#project-name-change-btn").click(function(){
35    var newProjectName = $("#project-name-change-input").val();
36
37    libtoaster.editCurrentProject({ projectName: newProjectName }, function (){
38      projectName.text(newProjectName);
39      libtoaster.ctx.projectName = newProjectName;
40      projectNameChangeCancel.click();
41    });
42  });
43
44  /* Nav bar activate state switcher */
45  $("#project-topbar .nav li a").each(function(){
46    if (window.location.pathname === $(this).attr('href'))
47      $(this).parent().addClass('active');
48    else
49      $(this).parent().removeClass('active');
50  });
51
52  if (!newBuildTargetInput.length) {
53    return;
54  }
55
56  /* the following only applies for non-command-line projects */
57
58  /* Recipe build input functionality */
59  if (ctx.numProjectLayers > 0 && ctx.machine){
60    newBuildTargetInput.removeAttr("disabled");
61  }
62
63  libtoaster.makeTypeahead(newBuildTargetInput,
64    libtoaster.ctx.recipesTypeAheadUrl, {}, function (item) {
65     selectedTarget = item;
66     newBuildTargetBuildBtn.removeAttr("disabled");
67  });
68
69  newBuildTargetInput.on('input', function () {
70    if ($(this).val().length === 0) {
71      newBuildTargetBuildBtn.attr("disabled", "disabled");
72    } else {
73      newBuildTargetBuildBtn.removeAttr("disabled");
74    }
75  });
76
77  newBuildTargetBuildBtn.click(function (e) {
78    e.preventDefault();
79    if (!newBuildTargetInput.val().trim()) {
80      return;
81    }
82    /* We use the value of the input field so as to maintain any command also
83     * added e.g. core-image-minimal:clean and because we can build targets
84     * that toaster doesn't yet know about
85     */
86    selectedTarget = { name: newBuildTargetInput.val().trim() };
87
88    /* Fire off the build */
89    libtoaster.startABuild(null, selectedTarget.name,
90      function(){
91        window.location.replace(libtoaster.ctx.projectBuildsUrl);
92    }, null);
93  });
94
95  updateProjectBtn.click(function (e) {
96    e.preventDefault();
97
98    selectedTarget = { name: "_PROJECT_PREPARE_" };
99
100    /* Save current default build image, fire off the build */
101    libtoaster.updateProject(null, selectedTarget.name, newBuildTargetInput.val().trim(),
102      function(){
103        window.location.replace(libtoaster.ctx.projectSpecificPageUrl);
104    }, null);
105  });
106
107  cancelProjectBtn.click(function (e) {
108    e.preventDefault();
109
110    /* redirect to 'done/canceled' landing page */
111    window.location.replace(libtoaster.ctx.landingSpecificCancelURL);
112  });
113
114  /* Call makeProjectNameValidation function */
115  libtoaster.makeProjectNameValidation($("#project-name-change-input"),
116      $("#hint-error-project-name"), $("#validate-project-name"),
117      $("#project-name-change-btn"));
118
119}
120