Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | function basePageInit(ctx) { |
| 4 | |
| 5 | var newBuildButton = $("#new-build-button"); |
| 6 | var newBuildTargetInput; |
| 7 | var newBuildTargetBuildBtn; |
| 8 | var projectNameForm = $("#project-name-change-form"); |
| 9 | var projectName = $("#project-name"); |
| 10 | var projectNameFormToggle = $("#project-change-form-toggle"); |
| 11 | var projectNameChangeCancel = $("#project-name-change-cancel"); |
| 12 | |
| 13 | /* initially the current project is used unless overridden by the new build |
| 14 | * button in top right nav |
| 15 | */ |
| 16 | var selectedProject = libtoaster.ctx; |
| 17 | |
| 18 | var selectedTarget; |
| 19 | |
| 20 | var newBuildProjectInput = $("#new-build-button #project-name-input"); |
| 21 | var newBuildProjectSaveBtn = $("#new-build-button #save-project-button"); |
| 22 | |
| 23 | /* Project name change functionality */ |
| 24 | projectNameFormToggle.click(function(e){ |
| 25 | e.preventDefault(); |
| 26 | |
| 27 | $(this).add(projectName).hide(); |
| 28 | projectNameForm.fadeIn(); |
| 29 | }); |
| 30 | |
| 31 | projectNameChangeCancel.click(function(e){ |
| 32 | e.preventDefault(); |
| 33 | |
| 34 | projectNameForm.hide(); |
| 35 | projectName.add(projectNameFormToggle).fadeIn(); |
| 36 | }); |
| 37 | |
| 38 | $("#project-name-change-btn").click(function(e){ |
| 39 | var newProjectName = $("#project-name-change-input").val(); |
| 40 | |
| 41 | libtoaster.editCurrentProject({ projectName: newProjectName },function (){ |
| 42 | |
| 43 | projectName.text(newProjectName); |
| 44 | libtoaster.ctx.projectName = newProjectName; |
| 45 | projectNameChangeCancel.click(); |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | _checkProjectBuildable(); |
| 50 | |
| 51 | $("#project-topbar .nav li a").each(function(){ |
| 52 | if (window.location.pathname === $(this).attr('href')) |
| 53 | $(this).parent().addClass('active'); |
| 54 | else |
| 55 | $(this).parent().removeClass('active'); |
| 56 | }); |
| 57 | |
| 58 | if ($(".total-builds").length !== 0){ |
| 59 | libtoaster.getProjectInfo(libtoaster.ctx.projectPageUrl, function(prjInfo){ |
| 60 | if (prjInfo.builds) |
| 61 | $(".total-builds").text(prjInfo.builds.length); |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | /* Hide the button if we're on the project,newproject or importlyaer page |
| 66 | * or if there are no projects yet defined |
| 67 | * only show if there isn't already a build-target-input already |
| 68 | */ |
| 69 | if (ctx.numProjects > 0 && |
| 70 | ctx.currentUrl.search('newproject') < 0 && |
| 71 | $(".build-target-input").length === 1) { |
| 72 | |
| 73 | newBuildTargetInput = $("#new-build-button .build-target-input"); |
| 74 | newBuildTargetBuildBtn = $("#new-build-button").find(".build-button"); |
| 75 | |
| 76 | _setupNewBuildButton(); |
| 77 | newBuildButton.show(); |
| 78 | } else if ($(".build-target-input").length > 0) { |
| 79 | newBuildTargetInput = $("#project-topbar .build-target-input"); |
| 80 | newBuildTargetBuildBtn = $("#project-topbar .build-button"); |
| 81 | } else { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | /* Hide the change project icon when there is only one project */ |
| 86 | if (ctx.numProjects === 1) { |
| 87 | $('#project .icon-pencil').hide(); |
| 88 | } |
| 89 | |
| 90 | /* If we have a project setup the typeahead */ |
| 91 | if (selectedProject.recipesTypeAheadUrl){ |
| 92 | libtoaster.makeTypeahead(newBuildTargetInput, selectedProject.recipesTypeAheadUrl, { format: "json" }, function (item) { |
| 93 | selectedTarget = item; |
| 94 | newBuildTargetBuildBtn.removeAttr("disabled"); |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | newBuildTargetInput.on('input', function () { |
| 99 | if ($(this).val().length === 0) { |
| 100 | newBuildTargetBuildBtn.attr("disabled", "disabled"); |
| 101 | } else { |
| 102 | newBuildTargetBuildBtn.removeAttr("disabled"); |
| 103 | } |
| 104 | }); |
| 105 | |
| 106 | newBuildTargetBuildBtn.click(function (e) { |
| 107 | e.preventDefault(); |
| 108 | |
| 109 | if (!newBuildTargetInput.val()) { |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | /* We use the value of the input field so as to maintain any command also |
| 114 | * added e.g. core-image-minimal:clean |
| 115 | */ |
| 116 | selectedTarget = { name: newBuildTargetInput.val() }; |
| 117 | |
| 118 | /* Fire off the build */ |
| 119 | libtoaster.startABuild(selectedProject.projectBuildsUrl, |
| 120 | selectedProject.projectId, selectedTarget.name, function(){ |
| 121 | window.location.replace(selectedProject.projectBuildsUrl); |
| 122 | }, null); |
| 123 | }); |
| 124 | |
| 125 | function _checkProjectBuildable() { |
| 126 | if (selectedProject.projectId === undefined) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | libtoaster.getProjectInfo(selectedProject.projectPageUrl, |
| 131 | function (data) { |
| 132 | if (data.machine === null || data.machine.name === undefined || data.layers.length === 0) { |
| 133 | /* we can't build anything with out a machine and some layers */ |
| 134 | $("#new-build-button #targets-form").hide(); |
| 135 | $("#new-build-button .alert").show(); |
| 136 | } else { |
| 137 | $("#new-build-button #targets-form").show(); |
| 138 | $("#new-build-button .alert").hide(); |
| 139 | |
| 140 | /* we can build this project; enable input fields */ |
| 141 | newBuildTargetInput.removeAttr("disabled"); |
| 142 | } |
| 143 | }, null); |
| 144 | } |
| 145 | |
| 146 | /* Setup New build button in the top nav bar */ |
| 147 | function _setupNewBuildButton() { |
| 148 | |
| 149 | /* If we don't have a current project then present the set project |
| 150 | * form. |
| 151 | */ |
| 152 | if (selectedProject.projectId === undefined) { |
| 153 | $('#change-project-form').show(); |
| 154 | $('#project .icon-pencil').hide(); |
| 155 | } |
| 156 | |
| 157 | libtoaster.makeTypeahead(newBuildProjectInput, selectedProject.projectsTypeAheadUrl, { format : "json" }, function (item) { |
| 158 | /* successfully selected a project */ |
| 159 | newBuildProjectSaveBtn.removeAttr("disabled"); |
| 160 | selectedProject = item; |
| 161 | }); |
| 162 | |
| 163 | /* Any typing in the input apart from enter key is going to invalidate |
| 164 | * the value that has been set by selecting a suggestion from the typeahead |
| 165 | */ |
| 166 | newBuildProjectInput.on('input', function (event) { |
| 167 | if (event.keyCode === 13) { |
| 168 | return; |
| 169 | } |
| 170 | newBuildProjectSaveBtn.attr("disabled", "disabled"); |
| 171 | }); |
| 172 | |
| 173 | |
| 174 | newBuildProjectSaveBtn.click(function () { |
| 175 | selectedProject.projectId = selectedProject.id; |
| 176 | /* Update the typeahead project_id paramater */ |
| 177 | _checkProjectBuildable(); |
| 178 | |
| 179 | newBuildTargetInput.removeAttr("disabled"); |
| 180 | |
| 181 | /* We've got a new project so now we need to update the |
| 182 | * target urls. We can get this from the new project's info |
| 183 | */ |
| 184 | $.getJSON(selectedProject.projectPageUrl, { format: "json" }, |
| 185 | function(projectInfo){ |
| 186 | /* Update the typeahead to use the new selectedProject */ |
| 187 | selectedProject = projectInfo; |
| 188 | |
| 189 | libtoaster.makeTypeahead(newBuildTargetInput, selectedProject.recipesTypeAheadUrl, { format: "json" }, function (item) { |
| 190 | /* successfully selected a target */ |
| 191 | selectedTarget = item; |
| 192 | newBuildTargetBuildBtn.removeAttr("disabled"); |
| 193 | }); |
| 194 | |
| 195 | }); |
| 196 | newBuildTargetInput.val(""); |
| 197 | |
| 198 | /* set up new form aspect */ |
| 199 | $("#new-build-button #project a").text(selectedProject.name).attr('href', selectedProject.projectPageUrl); |
| 200 | $("#new-build-button .alert a").attr('href', selectedProject.projectPageUrl); |
| 201 | $("#project .icon-pencil").show(); |
| 202 | |
| 203 | $("#change-project-form").slideUp({ 'complete' : function () { |
| 204 | $("#new-build-button #project").show(); |
| 205 | }}); |
| 206 | }); |
| 207 | |
| 208 | $('#new-build-button #project .icon-pencil').click(function () { |
| 209 | newBuildProjectSaveBtn.attr("disabled", "disabled"); |
| 210 | newBuildProjectInput.val($("#new-build-button #project a").text()); |
| 211 | $("#cancel-change-project").show(); |
| 212 | $(this).parent().hide(); |
| 213 | $("#change-project-form").slideDown(); |
| 214 | }); |
| 215 | |
| 216 | $("#new-build-button #cancel-change-project").click(function () { |
| 217 | $("#change-project-form").hide(function () { |
| 218 | $('#new-build-button #project').show(); |
| 219 | }); |
| 220 | |
| 221 | newBuildProjectInput.val(""); |
| 222 | newBuildProjectSaveBtn.attr("disabled", "disabled"); |
| 223 | }); |
| 224 | |
| 225 | /* Keep the dropdown open even unless we click outside the dropdown area */ |
| 226 | $(".new-build").click (function (event) { |
| 227 | event.stopPropagation(); |
| 228 | }); |
| 229 | }; |
| 230 | |
| 231 | } |