aboutsummaryrefslogtreecommitdiffstats
path: root/bitbake/lib/toaster/toastergui/static/js/customrecipe.js
blob: 8b1c190df994dcb4ecf0cb9444a5573b0f7742ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"use strict";

function customRecipePageInit(ctx) {

  var urlParams = libtoaster.parseUrlParams();
  var customiseTable = $("#selectpackagestable");
  var addPkgDepsModalBtn = $("#add-package-deps-modal-btn");
  var rmdPkgReverseDepsModalBtn = $("#rm-package-reverse-deps-modal-btn");

  if (urlParams.hasOwnProperty('notify') && urlParams.notify === 'new'){
    $("#image-created-notification").show();
  }

  customiseTable.on('table-done', function(e, total){
    /* Table is done so now setup the click handler for the package buttons */
    $(".add-rm-package-btn").click(function(e){
      e.preventDefault();
      var targetPkg = $(this).data();

       checkPackageDeps(targetPkg, function(pkgData){
         if (targetPkg.directive === 'add'){
           /* If we're adding a package we may need to show the modal to advise
            * on dependencies for this package.
            */
           if (pkgData.unsatisfied_dependencies.length === 0){
             addRemovePackage(targetPkg);
           } else {
             showPackageDepsModal(targetPkg, pkgData);
           }
         } else if (targetPkg.directive === 'remove') {
           if (pkgData.reverse_dependencies.length === 0){
             addRemovePackage(targetPkg);
           } else {
             showPackageReverseDepsModal(targetPkg, pkgData);
           }
           }
        });
    });
  });

  function checkPackageDeps(targetPkg, doneCb){
    $.ajax({
        type: 'GET',
        url: targetPkg.packageUrl,
        headers: { 'X-CSRFToken' : $.cookie('csrftoken')},
        success: function(data){
          if (data.error !== 'ok'){
            console.warn(data.error);
            return;
          }
          doneCb(data);
        }
    });
  }

  function showPackageDepsModal(targetPkg, pkgData){
    var modal = $("#package-deps-modal");
    var depsList = modal.find("#package-add-dep-list");
    var deps = pkgData.unsatisfied_dependencies;

    modal.find(".package-to-add-name").text(targetPkg.name);

    depsList.text("");

    for (var i in deps){
      var li = $('<li></li>').text(deps[i].name);
      li.append($('<span></span>').text(" ("+
            deps[i].size_formatted+")"));
      depsList.append(li);
    }

    modal.find("#package-deps-total-size").text(
      pkgData.unsatisfied_dependencies_size_formatted);

    targetPkg.depsAdded = deps;

    addPkgDepsModalBtn.data(targetPkg);
    modal.modal('show');
  }

  addPkgDepsModalBtn.click(function(e){
    e.preventDefault();

    addRemovePackage($(this).data(), null);
  });

  function showPackageReverseDepsModal(targetPkg, pkgData){
    var modal = $("#package-reverse-deps-modal");
    var depsList = modal.find("#package-reverse-dep-list");
    var deps = pkgData.reverse_dependencies;

    var depsCount = deps.length;
    var vDepends = "depends";
    var vPackage = "package";
    var vThis = "this";
    if (depsCount > 1) {
      vDepends = "depend";
      vPackage = "packages";
      vThis = "these";
    }
    modal.find(".package-to-rm-name").text(targetPkg.name);
    modal.find(".reverse-deps-count").text(depsCount);
    modal.find(".reverse-deps-count-plus1").text((depsCount+1) + " packages");
    modal.find(".reverse-deps-depends").text(vDepends);
    modal.find(".reverse-deps-package").text(vPackage);
    modal.find(".reverse-deps-this").text(vThis);

    depsList.text("");

    for (var i in deps){
      var li = $('<li></li>').text(deps[i].name);
      li.append($('<span></span>').text(" ("+
            deps[i].size_formatted+")"));
      depsList.append(li);
    }

    modal.find("#package-reverse-deps-total-size").text(
      pkgData.reverse_dependencies_size_formatted);

    targetPkg.depsRemoved = deps;

    rmdPkgReverseDepsModalBtn.data(targetPkg);
    modal.modal('show');
  }

  rmdPkgReverseDepsModalBtn.click(function(e){
    e.preventDefault();

    addRemovePackage($(this).data(), null);
  });


  function addRemovePackage(targetPkg, tableParams){
    var method;
    var msg = "You have ";

    var btnCell = $("#package-btn-cell-" + targetPkg.id);
    var inlineNotify = btnCell.children(".inline-notification");

    var i;
    var dep;
    var depBtnCell;

    if (targetPkg.directive === 'add') {
      method = 'PUT';
      /* If the package had dependencies also notify that they were added */
      if (targetPkg.hasOwnProperty('depsAdded') &&
        targetPkg.depsAdded.length > 0) {

        msg += "added ";
        msg += "<strong>" + (targetPkg.depsAdded.length + 1) + "</strong>";
        msg += " packages to " + ctx.recipe.name + ": ";
        msg += "<strong>" + targetPkg.name + "</strong> and its dependencies";

        for (i in targetPkg.depsAdded){
          dep = targetPkg.depsAdded[i];

          msg += " <strong>" + dep.name + "</strong>";

          /* Add any cells currently in view to the list of cells which get
           * an list-inline notification inside them and which change add/rm state
           */
          depBtnCell = $("#package-btn-cell-" + dep.pk);
          btnCell = btnCell.add(depBtnCell);

          inlineNotify = inlineNotify.add(
            depBtnCell.children(".inline-notification"));
        }

        inlineNotify.text(
          (targetPkg.depsAdded.length + 1) + " packages added");

      } else {
        msg += "added <strong>1</strong>";
        msg += " package to " + ctx.recipe.name + ": ";
        msg += "<strong>" + targetPkg.name + "</strong>";
        inlineNotify.text("1 package added");
      }

    } else if (targetPkg.directive === 'remove') {
      method = 'DELETE';
      var numPackageString = "1 package ";
      var revDepList = "";
      if (targetPkg.hasOwnProperty('depsRemoved') &&
              targetPkg.depsRemoved.length > 0) {
        var depsRemovedLength = targetPkg.depsRemoved.length;
        var ending = "y: ";
        var maxRevDepsDisplayed  = 5;
        var d = 0;
        if (depsRemovedLength > 1) {
            ending = "ies: ";
        }
        numPackageString = (depsRemovedLength + 1) + " packages";
        revDepList = " and its " + depsRemovedLength + " reverse dependenc" + ending;
        for (i in targetPkg.depsRemoved){
          /* include up to maxRevDepsDisplayed rev deps on the page notification */
          var notShownCount = depsRemovedLength - maxRevDepsDisplayed;
          dep = targetPkg.depsRemoved[i];
          if (d < maxRevDepsDisplayed) {
            if (d > 0) {
                revDepList += ", ";
            }
            revDepList += dep.name;
            d++;
            if ((d === maxRevDepsDisplayed) && (notShownCount > 0)) {
                revDepList += " and " + notShownCount + " more";
            }
          }

          /* Add any cells currently in view to the list of cells which get
           * an list-inline notification inside them and which change add/rm state
           */
          depBtnCell = $("#package-btn-cell-" + dep.pk);
          btnCell = btnCell.add(depBtnCell);

          inlineNotify = inlineNotify.add(
            depBtnCell.children(".inline-notification"));
        }
      }
      msg+= "removed " + numPackageString + " from " + ctx.recipe.name + ":";
      msg += " <strong>" + targetPkg.name + "</strong>";
      msg += revDepList;

      inlineNotify.text(numPackageString + " removed");
    } else {
      throw("Unknown package directive: should be add or remove");
    }

    $.ajax({
        type: method,
        url: targetPkg.packageUrl,
        headers: { 'X-CSRFToken' : $.cookie('csrftoken')},
        success: function(data){
          if (data.error !== 'ok'){
            console.warn(data.error);
            return;
          }

          libtoaster.showChangeNotification(msg);

          /* do the in-cell/inline notification to swap buttoms from add to
           * remove
           */
          btnCell.children("button").fadeOut().promise().done(function(){
            inlineNotify.fadeIn().delay(500).fadeOut(function(){
              if (targetPkg.directive === 'add')
                btnCell.children("button[data-directive=remove]").fadeIn();
              else
                btnCell.children("button[data-directive=add]").fadeIn();
            });
          });

          /* Update the total num packages */
          $.ajax({
            type: "GET",
            url: ctx.recipe.xhrPackageListUrl,
            headers: { 'X-CSRFToken' : $.cookie('csrftoken')},
            success: function(data){
              console.log(data);
              $("#total-num-packages").text(data.total);
              $("#total-size-packages").text(data.total_size_formatted);
            }
          });
        }
    });
  }

  $("#no-results-show-all-packages").click(function(){
    $(".no-results-search-input").val("");
  });

  $("#no-results-remove-search-btn").click(function(){
      $(".no-results-search-input").val("");
      $(this).hide();
  });

  /* Trigger a build of your custom image */
  $(".build-custom-image").click(function(){
    libtoaster.startABuild(null, ctx.recipe.name,
      function(){
        window.location.replace(libtoaster.ctx.projectBuildsUrl);
    });
  });

  $("#delete-custom-recipe-confirmed").click(function(e){
    e.preventDefault();
    libtoaster.disableAjaxLoadingTimer();
    $(this).find('[data-role="submit-state"]').hide();
    $(this).find('[data-role="loading-state"]').show();
    $(this).attr("disabled", "disabled");

    $.ajax({
        type: 'DELETE',
        url: ctx.recipe.xhrCustomRecipeUrl,
        headers: { 'X-CSRFToken' : $.cookie('csrftoken')},
        success: function (data) {
          if (data.error !== "ok") {
            console.warn(data.error);
          } else {
            var msg = $('<span>You have deleted <strong>1</strong> custom image: <strong id="deleted-custom-image-name"></strong></span>');
            msg.find("#deleted-custom-image-name").text(ctx.recipe.name);

            libtoaster.setNotification("custom-image-recipe-deleted",
                                       msg.html());

            window.location.replace(data.gotoUrl);
          }
        },
        error: function (data) {
          console.warn(data);
        }
    });
  });

 /* Stop the download link from working if it is in disabled state
  * http://getbootstrap.com/css/#forms-disabled-fieldsets
  */
 $("a[disabled=disabled]").click(function(e){
   e.preventDefault();
 });

}