blob: 45367036496b4b9ef4c59098e8d955a53732f368 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"use strict";
2
3function projectPageInit(ctx) {
4
5 var layerAddInput = $("#layer-add-input");
6 var layersInPrjList = $("#layers-in-project-list");
7 var layerAddBtn = $("#add-layer-btn");
8
9 var machineChangeInput = $("#machine-change-input");
10 var machineChangeBtn = $("#machine-change-btn");
11 var machineForm = $("#select-machine-form");
12 var machineChangeFormToggle = $("#change-machine-toggle");
13 var machineNameTitle = $("#project-machine-name");
14 var machineChangeCancel = $("#cancel-machine-change");
15
16 var freqBuildBtn = $("#freq-build-btn");
17 var freqBuildList = $("#freq-build-list");
18
19 var releaseChangeFormToggle = $("#release-change-toggle");
20 var releaseTitle = $("#project-release-title");
21 var releaseForm = $("#change-release-form");
22 var releaseModal = $("#change-release-modal");
23 var cancelReleaseChange = $("#cancel-release-change");
24
25 var currentLayerAddSelection;
Patrick Williamsf1e5d692016-03-30 15:21:19 -050026 var currentMachineAddSelection = "";
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027
28 var urlParams = libtoaster.parseUrlParams();
29
Patrick Williamsc0f7c042017-02-23 20:41:17 -060030 libtoaster.getProjectInfo(libtoaster.ctx.xhrProjectUrl, function(prjInfo){
Patrick Williamsc124f4f2015-09-15 14:41:29 -050031 updateProjectLayers(prjInfo.layers);
32 updateFreqBuildRecipes(prjInfo.freqtargets);
33 updateProjectRelease(prjInfo.release);
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034
35 /* If we're receiving a machine set from the url and it's different from
36 * our current machine then activate set machine sequence.
37 */
38 if (urlParams.hasOwnProperty('setMachine') &&
39 urlParams.setMachine !== prjInfo.machine.name){
Patrick Williamsf1e5d692016-03-30 15:21:19 -050040 machineChangeInput.val(urlParams.setMachine);
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041 machineChangeBtn.click();
42 } else {
43 updateMachineName(prjInfo.machine.name);
44 }
45
46 /* Now we're really ready show the page */
47 $("#project-page").show();
Patrick Williamsc0f7c042017-02-23 20:41:17 -060048
49 /* Set the project name in the delete modal */
50 $("#delete-project-modal .project-name").text(prjInfo.name);
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051 });
52
Patrick Williamsc0f7c042017-02-23 20:41:17 -060053 if (urlParams.hasOwnProperty('notify') && urlParams.notify === 'new-project'){
54 $("#project-created-notification").show();
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 }
56
57 /* Add/Rm layer functionality */
58
59 libtoaster.makeTypeahead(layerAddInput, libtoaster.ctx.layersTypeAheadUrl, { include_added: "false" }, function(item){
60 currentLayerAddSelection = item;
61 layerAddBtn.removeAttr("disabled");
62 });
63
Patrick Williamsf1e5d692016-03-30 15:21:19 -050064 layerAddInput.keyup(function() {
65 if ($(this).val().length == 0) {
66 layerAddBtn.attr("disabled", "disabled")
67 }
68 });
69
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070 layerAddBtn.click(function(e){
71 e.preventDefault();
72 var layerObj = currentLayerAddSelection;
73
74 addRmLayer(layerObj, true);
75 /* Reset the text input */
76 layerAddInput.val("");
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050077 /* Disable the add layer button*/
78 layerAddBtn.attr("disabled", "disabled");
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079 });
80
81 function addRmLayer(layerObj, add){
82
83 libtoaster.addRmLayer(layerObj, add, function(layerDepsList){
84 if (add){
85 updateProjectLayers([layerObj]);
86 updateProjectLayers(layerDepsList);
87 }
88
89 /* Show the alert message */
90 var message = libtoaster.makeLayerAddRmAlertMsg(layerObj, layerDepsList, add);
91 libtoaster.showChangeNotification(message);
92 });
93 }
94
95 function updateProjectLayers(layers){
96
97 /* No layers to add */
98 if (layers.length === 0){
99 updateLayersCount();
100 return;
101 }
102
103 for (var i in layers){
104 var layerObj = layers[i];
105
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600106 var projectLayer = $("<li><a></a><span class=\"glyphicon glyphicon-trash\" data-toggle=\"tooltip\" title=\"Remove\"></span></li>");
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107
108 projectLayer.data('layer', layerObj);
109 projectLayer.children("span").tooltip();
110
111 var link = projectLayer.children("a");
112
113 link.attr("href", layerObj.layerdetailurl);
114 link.text(layerObj.name);
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600115
116 if (layerObj.local_source_dir) {
117 link.tooltip({title: layerObj.local_source_dir, placement: "right"});
118 } else {
119 link.tooltip({title: layerObj.vcs_url + " | "+ layerObj.vcs_reference, placement: "right"});
120 }
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500121
122 var trashItem = projectLayer.children("span");
123 trashItem.click(function (e) {
124 e.preventDefault();
125 var layerObjToRm = $(this).parent().data('layer');
126
127 addRmLayer(layerObjToRm, false);
128
129 $(this).parent().fadeOut(function (){
130 $(this).remove();
131 updateLayersCount();
132 });
133 });
134
135 layersInPrjList.append(projectLayer);
136
137 updateLayersCount();
138 }
139 }
140
141 function updateLayersCount(){
142 var count = $("#layers-in-project-list").children().length;
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500143 var noLayerMsg = $("#no-layers-in-project");
144 var buildInput = $("#build-input");
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500146
147 if (count === 0) {
148 noLayerMsg.fadeIn();
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 $("#no-layers-in-project").fadeIn();
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500150 buildInput.attr("disabled", "disabled");
151 } else {
152 noLayerMsg.hide();
153 buildInput.removeAttr("disabled");
154 }
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155
156 $("#project-layers-count").text(count);
157
158 return count;
159 }
160
161 /* Frequent builds functionality */
162 function updateFreqBuildRecipes(recipes) {
163 var noMostBuilt = $("#no-most-built");
164
165 if (recipes.length === 0){
166 noMostBuilt.show();
167 freqBuildBtn.hide();
168 } else {
169 noMostBuilt.hide();
170 freqBuildBtn.show();
171 }
172
173 for (var i in recipes){
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600174 var freqTargetCheck = $('<li><div class="checkbox"><label><input type="checkbox" /><span class="freq-target-name"></span></label></li>');
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500175 freqTargetCheck.find(".freq-target-name").text(recipes[i]);
176 freqTargetCheck.find("input").val(recipes[i]);
177 freqTargetCheck.click(function(){
178 if (freqBuildList.find(":checked").length > 0)
179 freqBuildBtn.removeAttr("disabled");
180 else
181 freqBuildBtn.attr("disabled", "disabled");
182 });
183
184 freqBuildList.append(freqTargetCheck);
185 }
186 }
187
188 freqBuildBtn.click(function(e){
189 e.preventDefault();
190
191 var toBuild = "";
192 freqBuildList.find(":checked").each(function(){
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500193 toBuild += $(this).val() + ' ';
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500194 });
195
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500196 toBuild = toBuild.trim();
197
198 libtoaster.startABuild(null, toBuild,
199 function(){
200 /* Build request started */
201 window.location.replace(libtoaster.ctx.projectBuildsUrl);
202 },
203 function(){
204 /* Build request failed */
205 console.warn("Build request failed to be created");
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500206 });
207 });
208
209
210 /* Change machine functionality */
211
212 machineChangeFormToggle.click(function(){
213 machineForm.slideDown();
214 machineNameTitle.hide();
215 $(this).hide();
216 });
217
218 machineChangeCancel.click(function(){
219 machineForm.slideUp(function(){
220 machineNameTitle.show();
221 machineChangeFormToggle.show();
222 });
223 });
224
225 function updateMachineName(machineName){
226 machineChangeInput.val(machineName);
227 machineNameTitle.text(machineName);
228 }
229
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600230 libtoaster.makeTypeahead(machineChangeInput,
231 libtoaster.ctx.machinesTypeAheadUrl,
232 { }, function(item){
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500233 currentMachineAddSelection = item.name;
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500234 machineChangeBtn.removeAttr("disabled");
235 });
236
237 machineChangeBtn.click(function(e){
238 e.preventDefault();
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500239 /* We accept any value regardless of typeahead selection or not */
240 if (machineChangeInput.val().length === 0)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241 return;
242
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500243 currentMachineAddSelection = machineChangeInput.val();
244
245 libtoaster.editCurrentProject(
246 { machineName : currentMachineAddSelection },
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500247 function(){
248 /* Success machine changed */
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500249 updateMachineName(currentMachineAddSelection);
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250 machineChangeCancel.click();
251
252 /* Show the alert message */
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600253 var message = $('<span>You have changed the machine to: <strong><span id="notify-machine-name"></span></strong></span>');
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500254 message.find("#notify-machine-name").text(currentMachineAddSelection);
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255 libtoaster.showChangeNotification(message);
256 },
257 function(){
258 /* Failed machine changed */
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500259 console.warn("Failed to change machine");
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500260 });
261 });
262
263
264 /* Change release functionality */
265 function updateProjectRelease(release){
266 releaseTitle.text(release.description);
267 }
268
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500269
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600270 $("#delete-project-confirmed").click(function(e){
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500271 e.preventDefault();
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600272 libtoaster.disableAjaxLoadingTimer();
273 $(this).find('[data-role="submit-state"]').hide();
274 $(this).find('[data-role="loading-state"]').show();
275 $(this).attr("disabled", "disabled");
276 $('#delete-project-modal [data-dismiss="modal"]').hide();
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500277
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600278 $.ajax({
279 type: 'DELETE',
280 url: libtoaster.ctx.xhrProjectUrl,
281 headers: { 'X-CSRFToken' : $.cookie('csrftoken')},
282 success: function (data) {
283 if (data.error !== "ok") {
284 console.warn(data.error);
285 } else {
286 var msg = $('<span>You have deleted <strong>1</strong> project: <strong id="project-deleted"></strong></span>');
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500287
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600288 msg.find("#project-deleted").text(libtoaster.ctx.projectName);
289 libtoaster.setNotification("project-deleted", msg.html());
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500290
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600291 window.location.replace(data.gotoUrl);
292 }
293 },
294 error: function (data) {
295 console.warn(data);
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500296 }
297 });
298 });
299
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500300}