1/*
2 * layer: Object representing the parent layer { id: .. name: ... url }
3 * dependencies: array of dependency layer objects { id: .. name: ..}
4 * title: optional override for title
5 * body: optional override for body
6 * addToProject: Whether to add layers to project on accept
7 * successAdd: function to run on success
8 */
9function showLayerDepsModal(layer,
10                            dependencies,
11                            title,
12                            body,
13                            addToProject,
14                            successAdd) {
15
16  if ($("#dependencies-modal").length === 0) {
17    $.get(libtoaster.ctx.htmlUrl + "/layer_deps_modal.html", function(html){
18      $("body").append(html);
19      setupModal();
20    });
21  } else {
22    setupModal();
23  }
24
25  function setupModal(){
26
27    if (title) {
28      $('#dependencies-modal #title').text(title);
29    } else {
30      $('#dependencies-modal #title').text(layer.name);
31    }
32
33    if (body) {
34      $("#dependencies-modal #body-text").html(body);
35    } else {
36      $("#dependencies-modal #layer-name").text(layer.name);
37    }
38
39    var deplistHtml = "";
40    for (var i = 0; i < dependencies.length; i++) {
41      deplistHtml += "<li><div class=\"checkbox\"><label><input name=\"dependencies\" value=\"";
42      deplistHtml += dependencies[i].id;
43      deplistHtml +="\" type=\"checkbox\" checked=\"checked\"/>";
44      deplistHtml += dependencies[i].name;
45      deplistHtml += "</label></div></li>";
46    }
47    $('#dependencies-list').html(deplistHtml);
48
49    $("#dependencies-modal").data("deps", dependencies);
50
51    /* Clear any alert notifications before showing the modal */
52    $(".alert").fadeOut(function(){
53      $('#dependencies-modal').modal('show');
54    });
55
56    /* Discard the old submission function */
57    $("#dependencies-modal-form").unbind('submit');
58
59    $("#dependencies-modal-form").submit(function (e) {
60      e.preventDefault();
61      var selectedLayerIds = [];
62      var selectedLayers = [];
63
64      $("input[name='dependencies']:checked").each(function () {
65        selectedLayerIds.push(parseInt($(this).val()));
66      });
67
68      /* -1 is a special dummy Id which we use when the layer isn't yet in the
69       * system, normally we would add the current layer to the selection.
70       */
71      if (layer.id != -1)
72        selectedLayerIds.push(layer.id);
73
74      /* Find the selected layer objects from our original list */
75      for (var i = 0; i < selectedLayerIds.length; i++) {
76        for (var j = 0; j < dependencies.length; j++) {
77          if (dependencies[j].id == selectedLayerIds[i]) {
78            selectedLayers.push(dependencies[j]);
79          }
80        }
81      }
82
83      if (addToProject) {
84        libtoaster.editCurrentProject({ 'layerAdd': selectedLayerIds.join(",") }, function () {
85          if (successAdd) {
86            successAdd(selectedLayers);
87          }
88        }, function () {
89          console.warn("Adding layers to project failed");
90        });
91      } else {
92        successAdd(selectedLayers);
93      }
94
95      $('#dependencies-modal').modal('hide');
96    });
97  }
98}
99