1"use strict";
2
3function layerBtnsInit() {
4
5  /* Remove any current bindings to avoid duplicated binds */
6  $(".layerbtn").unbind('click');
7
8  $(".layerbtn").click(function (){
9    var layerObj = $(this).data("layer");
10    var add = ($(this).data('directive') === "add");
11    var thisBtn = $(this);
12
13    libtoaster.addRmLayer(layerObj, add, function (layerDepsList){
14      libtoaster.showChangeNotification(libtoaster.makeLayerAddRmAlertMsg(layerObj, layerDepsList, add));
15
16      /* In-cell notification */
17      var notification = $('<div id="temp-inline-notify" style="display: none; font-size: 11px; line-height: 1.3;" class="tooltip-inner"></div>');
18      thisBtn.parent().append(notification);
19
20      if (add){
21        if (layerDepsList.length > 0)
22          notification.text(String(layerDepsList.length + 1) + " layers added");
23        else
24          notification.text("1 layer added");
25
26        var layerBtnsFadeOut = $();
27        var layerExistsBtnFadeIn = $();
28
29        layerBtnsFadeOut = layerBtnsFadeOut.add(".layer-add-" + layerObj.id);
30        layerExistsBtnFadeIn = layerExistsBtnFadeIn.add(".layer-exists-" + layerObj.id);
31
32        for (var i in layerDepsList){
33          layerBtnsFadeOut = layerBtnsFadeOut.add(".layer-add-" + layerDepsList[i].id);
34          layerExistsBtnFadeIn = layerExistsBtnFadeIn.add(".layer-exists-" + layerDepsList[i].id);
35        }
36
37        layerBtnsFadeOut.fadeOut().promise().done(function(){
38          notification.fadeIn().delay(500).fadeOut(function(){
39            /* Fade in the buttons */
40            layerExistsBtnFadeIn.fadeIn();
41            notification.remove();
42          });
43        });
44      } else {
45        notification.text("1 layer removed");
46        /* Deleting a layer we only hanlde the one button */
47        thisBtn.fadeOut(function(){
48          notification.fadeIn().delay(500).fadeOut(function(){
49            $(".layer-add-" + layerObj.id).fadeIn();
50            notification.remove();
51          });
52        });
53      }
54
55    });
56  });
57
58  $("td .build-recipe-btn").unbind('click');
59  $("td .build-recipe-btn").click(function(e){
60    e.preventDefault();
61    var recipe = $(this).data('recipe-name');
62
63    libtoaster.startABuild(null, recipe,
64      function(){
65        /* Success */
66        window.location.replace(libtoaster.ctx.projectBuildsUrl);
67      });
68  });
69
70  $("td .set-default-recipe-btn").unbind('click');
71  $("td .set-default-recipe-btn").click(function(e){
72    e.preventDefault();
73    var recipe = $(this).data('recipe-name');
74
75    libtoaster.setDefaultImage(null, recipe,
76      function(){
77        /* Success */
78        window.location.replace(libtoaster.ctx.projectSpecificPageUrl);
79      });
80  });
81
82
83  $(".customise-btn").unbind('click');
84  $(".customise-btn").click(function(e){
85    e.preventDefault();
86    var imgCustomModal = $("#new-custom-image-modal");
87
88    if (imgCustomModal.length == 0)
89      throw("Modal new-custom-image not found");
90
91    var recipe = {id: $(this).data('recipe'), name: null}
92    newCustomImageModalSetRecipes([recipe]);
93    imgCustomModal.modal('show');
94  });
95}
96