Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | function 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 | /* Project name change functionality */ |
| 18 | projectNameFormToggle.click(function(e){ |
| 19 | e.preventDefault(); |
| 20 | projectNameContainer.hide(); |
| 21 | projectNameForm.fadeIn(); |
| 22 | }); |
| 23 | |
| 24 | projectNameChangeCancel.click(function(e){ |
| 25 | e.preventDefault(); |
| 26 | projectNameForm.hide(); |
| 27 | projectNameContainer.fadeIn(); |
| 28 | }); |
| 29 | |
| 30 | $("#project-name-change-btn").click(function(){ |
| 31 | var newProjectName = $("#project-name-change-input").val(); |
| 32 | |
| 33 | libtoaster.editCurrentProject({ projectName: newProjectName }, function (){ |
| 34 | projectName.html(newProjectName); |
| 35 | libtoaster.ctx.projectName = newProjectName; |
| 36 | projectNameChangeCancel.click(); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | /* Nav bar activate state switcher */ |
| 41 | $("#project-topbar .nav li a").each(function(){ |
| 42 | if (window.location.pathname === $(this).attr('href')) |
| 43 | $(this).parent().addClass('active'); |
| 44 | else |
| 45 | $(this).parent().removeClass('active'); |
| 46 | }); |
| 47 | |
| 48 | if (!newBuildTargetInput.length) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | /* the following only applies for non-command-line projects */ |
| 53 | |
| 54 | /* Recipe build input functionality */ |
| 55 | if (ctx.numProjectLayers > 0 && ctx.machine){ |
| 56 | newBuildTargetInput.removeAttr("disabled"); |
| 57 | } |
| 58 | |
| 59 | libtoaster.makeTypeahead(newBuildTargetInput, |
| 60 | libtoaster.ctx.recipesTypeAheadUrl, {}, function (item) { |
| 61 | selectedTarget = item; |
| 62 | newBuildTargetBuildBtn.removeAttr("disabled"); |
| 63 | }); |
| 64 | |
| 65 | newBuildTargetInput.on('input', function () { |
| 66 | if ($(this).val().length === 0) { |
| 67 | newBuildTargetBuildBtn.attr("disabled", "disabled"); |
| 68 | } else { |
| 69 | newBuildTargetBuildBtn.removeAttr("disabled"); |
| 70 | } |
| 71 | }); |
| 72 | |
| 73 | newBuildTargetBuildBtn.click(function (e) { |
| 74 | e.preventDefault(); |
| 75 | if (!newBuildTargetInput.val()) { |
| 76 | return; |
| 77 | } |
| 78 | /* We use the value of the input field so as to maintain any command also |
| 79 | * added e.g. core-image-minimal:clean and because we can build targets |
| 80 | * that toaster doesn't yet know about |
| 81 | */ |
| 82 | selectedTarget = { name: newBuildTargetInput.val() }; |
| 83 | |
| 84 | /* Fire off the build */ |
| 85 | libtoaster.startABuild(null, selectedTarget.name, |
| 86 | function(){ |
| 87 | window.location.replace(libtoaster.ctx.projectBuildsUrl); |
| 88 | }, null); |
| 89 | }); |
| 90 | } |