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