summaryrefslogtreecommitdiffstats
path: root/lib/toaster/toastergui/static/js/importlayer.js
blob: 59652b9a3649e75be9ecfde382a9923ae483045a (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
"use strict"

function importLayerPageInit (ctx) {

  var layerDepBtn = $("#add-layer-dependency-btn");
  var importAndAddBtn = $("#import-and-add-btn");
  var layerNameInput = $("#import-layer-name");
  var vcsURLInput = $("#layer-git-repo-url");
  var gitRefInput = $("#layer-git-ref");
  var layerDepInput = $("#layer-dependency");
  var layerNameCtrl = $("#layer-name-ctrl");
  var duplicatedLayerName = $("#duplicated-layer-name-hint");
  var localDirPath = $("#local-dir-path");

  var layerDeps = {};
  var layerDepsDeps = {};
  var currentLayerDepSelection;
  var validLayerName = /^(\w|-)+$/;

  libtoaster.makeTypeahead(layerDepInput,
                           libtoaster.ctx.layersTypeAheadUrl,
                           { include_added: "true" }, function(item){
    currentLayerDepSelection = item;
    layerDepBtn.removeAttr("disabled");
  });

  layerDepInput.on("typeahead:select", function(event, data){
    currentLayerDepSelection = data;
  });

  // Disable local dir repo when page is loaded.
  $('#local-dir').hide();

  // disable the "Add layer" button when the layer input typeahead is empty
  // or not in the typeahead choices
  layerDepInput.on("input change", function(){
    layerDepBtn.attr("disabled","disabled");
  });

  /* We automatically add "openembedded-core" layer for convenience as a
   * dependency as pretty much all layers depend on this one
   */
  $.getJSON(libtoaster.ctx.layersTypeAheadUrl,
    { include_added: "true" , search: "openembedded-core" },
    function(layer) {
    if (layer.results.length > 0) {
      currentLayerDepSelection = layer.results[0];
        layerDepBtn.click();
    }
  });

  layerDepBtn.click(function(){
    if (currentLayerDepSelection == undefined)
      return;

    layerDeps[currentLayerDepSelection.id] = currentLayerDepSelection;

    /* Make a list item for the new layer dependency */
    var newLayerDep = $("<li><a></a><span class=\"glyphicon glyphicon-trash\" data-toggle=\"tooltip\" title=\"Remove\"></span></li>");

    newLayerDep.data('layer-id', currentLayerDepSelection.id);
    newLayerDep.children("span").tooltip();

    var link = newLayerDep.children("a");
    link.attr("href", currentLayerDepSelection.layerdetailurl);
    link.text(currentLayerDepSelection.name);
    link.tooltip({title: currentLayerDepSelection.tooltip, placement: "right"});

    var trashItem = newLayerDep.children("span");
    trashItem.click(function () {
      var toRemove = $(this).parent().data('layer-id');
      delete layerDeps[toRemove];
      $(this).parent().fadeOut(function (){
        $(this).remove();
      });
    });

    $("#layer-deps-list").append(newLayerDep);

    libtoaster.getLayerDepsForProject(currentLayerDepSelection.layerdetailurl,
                                      function (data){
        /* These are the dependencies of the layer added as a dependency */
        if (data.list.length > 0) {
          currentLayerDepSelection.url = currentLayerDepSelection.layerdetailurl;
          layerDeps[currentLayerDepSelection.id].deps = data.list;
        }

        /* Clear the current selection */
        layerDepInput.val("");
        currentLayerDepSelection = undefined;
        layerDepBtn.attr("disabled","disabled");
      }, null);
  });

  importAndAddBtn.click(function(e){
    e.preventDefault();
    /* This is a list of the names from layerDeps for the layer deps
     * modal dialog body
     */
    var depNames = [];

    /* arrray of all layer dep ids includes parent and child deps */
    var allDeps = [];

    /* temporary object to use to do a reduce on the dependencies for each
     * layer dependency added
     */
    var depDeps = {};

    /* the layers that have dependencies have an extra property "deps"
     * look in this for each layer and reduce this to a unquie object
     * of deps.
     */
    for (var key in layerDeps){
      if (layerDeps[key].hasOwnProperty('deps')){
        for (var dep in layerDeps[key].deps){
          var layer = layerDeps[key].deps[dep];
          depDeps[layer.id] = layer;
        }
      }
      depNames.push(layerDeps[key].name);
      allDeps.push(layerDeps[key].id);
    }

    /* we actually want it as an array so convert it now */
    var depDepsArray = [];
    for (var key in depDeps)
      depDepsArray.push (depDeps[key]);

    if (depDepsArray.length > 0) {
      var layer = { name: layerNameInput.val(), url: "#", id: -1 };
      var title = "Layer";
      var body = "<strong>"+layer.name+"</strong>'s dependencies ("+
        depNames.join(", ")+"</span>) require some layers that are not added to your project. Select the ones you want to add:</p>";

      showLayerDepsModal(layer,
                         depDepsArray,
                         title, body, false, function(layerObsList){
        /* Add the accepted layer dependencies' ids to the allDeps array */
        for (var key in layerObsList){
          allDeps.push(layerObsList[key].id);
        }
        import_and_add ();
      });
    } else {
      import_and_add ();
    }

    function import_and_add () {
      /* convert to a csv of all the deps to be added */
      var layerDepsCsv = allDeps.join(",");

      var layerData = {
        name: layerNameInput.val(),
        vcs_url: vcsURLInput.val(),
        git_ref: gitRefInput.val(),
        dir_path: $("#layer-subdir").val(),
        project_id: libtoaster.ctx.projectId,
        layer_deps: layerDepsCsv,
        local_source_dir: $('#local-dir-path').val(),
        add_to_project: true,
      };

      if ($('input[name=repo]:checked').val() == "git") {
        layerData.local_source_dir = "";
      } else {
        layerData.vcs_url = "";
        layerData.git_ref = "";
      }

      $.ajax({
          type: "PUT",
          url: ctx.xhrLayerUrl,
          data: JSON.stringify(layerData),
          headers: { 'X-CSRFToken' : $.cookie('csrftoken')},
          success: function (data) {
            if (data.error != "ok") {
              console.log(data.error);
              /* let the user know why nothing happened */
              alert(data.error)
            } else {
              createImportedNotification(data);
              window.location.replace(libtoaster.ctx.projectPageUrl);
            }
          },
          error: function (data) {
            console.log("Call failed");
            console.log(data);
          }
      });
    }
  });

  /* Layer imported notification */
  function createImportedNotification(imported){
    var message = "Layer imported";

    if (imported.deps_added.length === 0) {
      message = "You have imported <strong><a class=\"alert-link\" href=\""+imported.imported_layer.layerdetailurl+"\">"+imported.imported_layer.name+"</a></strong> and added it to your project.";
    } else {

      var links = "<a href=\""+imported.imported_layer.layerdetailurl+"\">"+imported.imported_layer.name+"</a>, ";

      imported.deps_added.map (function(item, index){
        links +='<a href="'+item.layerdetailurl+'">'+item.name+'</a>';
        /*If we're at the last element we don't want the trailing comma */
        if (imported.deps_added[index+1] !== undefined)
          links += ', ';
      });

      /* Length + 1 here to do deps + the imported layer */
      message = 'You have imported <strong><a href="'+imported.imported_layer.layerdetailurl+'">'+imported.imported_layer.name+'</a></strong> and added <strong>'+(imported.deps_added.length+1)+'</strong> layers to your project: <strong>'+links+'</strong>';
    }

    libtoaster.setNotification("layer-imported", message);
  }

  function enable_import_btn(enabled) {
    var importAndAddHint = $("#import-and-add-hint");

    if (enabled) {
      importAndAddBtn.removeAttr("disabled");
      importAndAddHint.hide();
      return;
    }

    importAndAddBtn.attr("disabled", "disabled");
    importAndAddHint.show();
  }

  function check_form() {
    var valid = false;
    var inputs = $("input:required");
    var inputStr = inputs.val().split("");

    for (var i=0; i<inputs.val().length; i++){
      if (!(valid = inputStr[i])){
        enable_import_btn(false);
        break;
      }
    }

    if (valid) {
      if ($("#local-dir-radio").prop("checked") &&
          localDirPath.val().length > 0) {
        enable_import_btn(true);
      }

      if ($("#git-repo-radio").prop("checked")) {
        if (gitRefInput.val().length > 0 &&
            gitRefInput.val() == 'HEAD') {
          $('#invalid-layer-revision-hint').show();
          $('#layer-revision-ctrl').addClass('has-error');
          enable_import_btn(false);
        } else if (vcsURLInput.val().length > 0 &&
                   gitRefInput.val().length > 0) {
          $('#invalid-layer-revision-hint').hide();
          $('#layer-revision-ctrl').removeClass('has-error');
          enable_import_btn(true);
        }
      }
    }

    if (inputs.val().length == 0)
      enable_import_btn(false);
  }

  function layerExistsError(layer){
    var dupLayerInfo = $("#duplicate-layer-info");

    if (layer.local_source_dir) {
      $("#git-layer-dup").hide();
      $("#local-layer-dup").fadeIn();
      dupLayerInfo.find(".dup-layer-name").text(layer.name);
      dupLayerInfo.find(".dup-layer-link").attr("href", layer.layerdetailurl);
      dupLayerInfo.find("#dup-local-source-dir-name").text(layer.local_source_dir);
    } else {
      $("#git-layer-dup").fadeIn();
      $("#local-layer-dup").hide();
      dupLayerInfo.find(".dup-layer-name").text(layer.name);
      dupLayerInfo.find(".dup-layer-link").attr("href", layer.layerdetailurl);
      dupLayerInfo.find("#dup-layer-vcs-url").text(layer.vcs_url);
      dupLayerInfo.find("#dup-layer-revision").text(layer.vcs_reference);
    }
    $(".fields-apart-from-layer-name").fadeOut(function(){

      dupLayerInfo.fadeIn();
    });
  }

  layerNameInput.on('blur', function() {
    if (!$(this).val()){
      return;
    }
    var name = $(this).val();

    /* Check if the layer name exists */
    $.getJSON(libtoaster.ctx.layersTypeAheadUrl,
        { include_added: "true" , search: name, format: "json" },
        function(layer) {
          if (layer.results.length > 0) {
            for (var i in layer.results){
              if (layer.results[i].name == name) {
                layerExistsError(layer.results[i]);
              }
            }
          }
      });
  });

  vcsURLInput.on('input', function() {
    check_form();
  });

  gitRefInput.on('input', function() {
    check_form();
  });

  layerNameInput.on('input', function() {
    if ($(this).val() && !validLayerName.test($(this).val())){
      layerNameCtrl.addClass("has-error")
      $("#invalid-layer-name-hint").show();
      enable_import_btn(false);
      return;
    }

    if ($("#duplicate-layer-info").css("display") != "None"){
      $("#duplicate-layer-info").fadeOut(function(){
      $(".fields-apart-from-layer-name").show();
      radioDisplay();
    });

  }

    radioDisplay();

    /* Don't remove the error class if we're displaying the error for another
     * reason.
     */
    if (!duplicatedLayerName.is(":visible"))
      layerNameCtrl.removeClass("has-error")

    $("#invalid-layer-name-hint").hide();
    check_form();
  });

  /* Setup 'blank' typeahead */
  libtoaster.makeTypeahead(gitRefInput,
                           ctx.xhrGitRevTypeAheadUrl,
                           { git_url: null }, function(){});


  vcsURLInput.focusout(function (){
    if (!$(this).val())
      return;

    /* If we a layer name specified don't overwrite it or if there isn't a
     * url typed in yet return
     */
    if (!layerNameInput.val() && $(this).val().search("/")){
      var urlPts = $(this).val().split("/");
      /* Add a suggestion of the layer name */
      var suggestion = urlPts[urlPts.length-1].replace(".git","");
      layerNameInput.val(suggestion);
    }

    /* Now actually setup the typeahead properly with the git url entered */
    gitRefInput._typeahead('destroy');

    libtoaster.makeTypeahead(gitRefInput,
                             ctx.xhrGitRevTypeAheadUrl,
                             { git_url: $(this).val() },
                             function(selected){
                               gitRefInput._typeahead("close");
                             });

  });

  function radioDisplay() {
    if ($('input[name=repo]:checked').val() == "local") {
      $('#git-repo').hide();
      $('#import-git-layer-and-add-hint').hide();
      $('#local-dir').fadeIn();
      $('#import-local-dir-and-add-hint').fadeIn();
    } else {
      $('#local-dir').hide();
      $('#import-local-dir-and-add-hint').hide();
      $('#git-repo').fadeIn();
      $('#import-git-layer-and-add-hint').fadeIn();
    }
  }

  $('input:radio[name="repo"]').change(function() {
    radioDisplay();
    if ($("#local-dir-radio").prop("checked")) {
      if (localDirPath.val().length > 0) {
        enable_import_btn(true);
      } else {
        enable_import_btn(false);
      }
    }
    if ($("#git-repo-radio").prop("checked")) {
      if (vcsURLInput.val().length > 0 && gitRefInput.val().length > 0) {
        enable_import_btn(true);
      } else {
        enable_import_btn(false);
      }
    }
  });

  localDirPath.on('input', function(){
    if ($(this).val().trim().length == 0) {
      $('#import-and-add-btn').attr("disabled","disabled");
      $('#local-dir').addClass('has-error');
      $('#hintError-dir-abs-path').show();
      $('#hintError-dir-path-starts-with-slash').show();
    } else {
      var input = $(this);
      var reBeginWithSlash = /^\//;
      var reCheckVariable = /^\$/;
      var re = /([ <>\\|":\.%\?\*]+)/;

      var invalidDir = re.test(input.val());
      var invalidSlash = reBeginWithSlash.test(input.val());
      var invalidVar = reCheckVariable.test(input.val());

      if (!invalidSlash && !invalidVar) {
        $('#local-dir').addClass('has-error');
        $('#import-and-add-btn').attr("disabled","disabled");
        $('#hintError-dir-abs-path').show();
        $('#hintError-dir-path-starts-with-slash').show();
      } else if (invalidDir) {
        $('#local-dir').addClass('has-error');
        $('#import-and-add-btn').attr("disabled","disabled");
        $('#hintError-dir-path').show();
      } else {
        $('#local-dir').removeClass('has-error');
        if (layerNameInput.val().length > 0) {
          $('#import-and-add-btn').removeAttr("disabled");
        }
        $('#hintError-dir-abs-path').hide();
        $('#hintError-dir-path-starts-with-slash').hide();
        $('#hintError-dir-path').hide();
      }
    }
  });
}