From 543586462b66434741f47f2884b4ccdeda5397b5 Mon Sep 17 00:00:00 2001 From: Michael Wood Date: Mon, 28 Sep 2015 21:45:24 -0700 Subject: [PATCH] toaster: Add Image customisation frontend feature Add the Image customisation front end feature to Toaster. Caveat - This feature is currently in development and should not be enabled by default. Signed-off-by: Michael Wood Signed-off-by: brian avery Signed-off-by: Richard Purdie --- .../toastergui/static/js/customrecipe.js | 50 ++++++ lib/toaster/toastergui/static/js/layerBtn.js | 13 ++ .../toastergui/static/js/newcustomimage.js | 49 ++++++ .../toastergui/templates/baseprojectpage.html | 7 +- .../toastergui/templates/customise_btn.html | 9 ++ .../toastergui/templates/customrecipe.html | 142 ++++++++++++++++++ .../toastergui/templates/newcustomimage.html | 54 +++++++ .../toastergui/templates/pkg_add_rm_btn.html | 16 ++ lib/toaster/toastergui/templates/project.html | 2 +- .../toastergui/templates/projecttopbar.html | 9 +- lib/toaster/toastergui/urls.py | 17 ++- lib/toaster/toastergui/views.py | 9 ++ 12 files changed, 368 insertions(+), 9 deletions(-) create mode 100644 lib/toaster/toastergui/static/js/customrecipe.js create mode 100644 lib/toaster/toastergui/static/js/newcustomimage.js create mode 100644 lib/toaster/toastergui/templates/customise_btn.html create mode 100644 lib/toaster/toastergui/templates/customrecipe.html create mode 100644 lib/toaster/toastergui/templates/newcustomimage.html create mode 100644 lib/toaster/toastergui/templates/pkg_add_rm_btn.html diff --git a/lib/toaster/toastergui/static/js/customrecipe.js b/lib/toaster/toastergui/static/js/customrecipe.js new file mode 100644 index 00000000000..4f6b304dd62 --- /dev/null +++ b/lib/toaster/toastergui/static/js/customrecipe.js @@ -0,0 +1,50 @@ +"use strict"; + +function customRecipePageInit(ctx) { + + var urlParams = libtoaster.parseUrlParams(); + + (function notificationRequest(){ + if (urlParams.hasOwnProperty('notify') && urlParams.notify === 'new'){ + $("#image-created-notification").show(); + } + })(); + + $("#recipeselection").on('table-done', function(e, total, tableParams){ + /* Table is done so now setup the click handler for the package buttons */ + $(".add-rm-package-btn").click(function(e){ + e.preventDefault(); + addRemovePackage($(this), tableParams); + }); + }); + + function addRemovePackage(pkgBtn, tableParams){ + var pkgBtnData = pkgBtn.data(); + var method; + var buttonToShow; + + if (pkgBtnData.directive == 'add') { + method = 'PUT'; + buttonToShow = '#package-rm-btn-' + pkgBtnData.package; + } else if (pkgBtnData.directive == 'remove') { + method = 'DELETE'; + buttonToShow = '#package-add-btn-' + pkgBtnData.package; + } else { + throw("Unknown package directive: should be add or remove"); + } + + $.ajax({ + type: method, + url: pkgBtnData.packageUrl, + headers: { 'X-CSRFToken' : $.cookie('csrftoken')}, + success: function(data){ + /* Invalidate the Add | Rm package table's current cache */ + tableParams.nocache = true; + $.get(ctx.tableApiUrl, tableParams); + /* Swap the buttons around */ + pkgBtn.hide(); + $(buttonToShow).show(); + } + }); + } +} diff --git a/lib/toaster/toastergui/static/js/layerBtn.js b/lib/toaster/toastergui/static/js/layerBtn.js index a0509f9aa36..da0241c62b1 100644 --- a/lib/toaster/toastergui/static/js/layerBtn.js +++ b/lib/toaster/toastergui/static/js/layerBtn.js @@ -68,6 +68,19 @@ function layerBtnsInit(ctx) { }); }); + + $(".customise-btn").unbind('click'); + $(".customise-btn").click(function(e){ + e.preventDefault(); + var imgCustomModal = $("#new-custom-image-modal"); + + if (imgCustomModal.length == 0) + throw("Modal new-custom-image not found"); + + imgCustomModal.data('recipe', $(this).data('recipe')); + imgCustomModal.modal('show'); + }); + /* Setup the initial state of the buttons */ for (var i in ctx.projectLayers){ diff --git a/lib/toaster/toastergui/static/js/newcustomimage.js b/lib/toaster/toastergui/static/js/newcustomimage.js new file mode 100644 index 00000000000..935b21ede84 --- /dev/null +++ b/lib/toaster/toastergui/static/js/newcustomimage.js @@ -0,0 +1,49 @@ +"use strict"; + +function newCustomImagePageInit(ctx){ + + var newCustomImgBtn = $("#create-new-custom-image-btn"); + var imgCustomModal = $("#new-custom-image-modal"); + + newCustomImgBtn.click(function(e){ + e.preventDefault(); + + var name = imgCustomModal.find('input').val(); + var baseRecipeId = imgCustomModal.data('recipe'); + + if (name.length > 0) { + createCustomRecipe(name, baseRecipeId); + imgCustomModal.modal('hide'); + } else { + console.warn("TODO No name supplied"); + } + }); + + function createCustomRecipe(name, baseRecipeId){ + var data = { + 'name' : name, + 'project' : libtoaster.ctx.projectId, + 'base' : baseRecipeId, + }; + + $.ajax({ + type: "POST", + url: ctx.xhrCustomRecipeUrl, + data: data, + headers: { 'X-CSRFToken' : $.cookie('csrftoken')}, + success: function (ret) { + if (ret.error !== "ok") { + console.warn(ret.error); + } else { + window.location.replace(ret.url + '?notify=new'); + } + }, + error: function (ret) { + console.warn("Call failed"); + console.warn(ret); + } + }); + } + + +} diff --git a/lib/toaster/toastergui/templates/baseprojectpage.html b/lib/toaster/toastergui/templates/baseprojectpage.html index 668e0bf5ef5..88bf8599e4b 100644 --- a/lib/toaster/toastergui/templates/baseprojectpage.html +++ b/lib/toaster/toastergui/templates/baseprojectpage.html @@ -23,8 +23,11 @@