2014-11-28 20:12:18 +00:00
|
|
|
"use strict"
|
|
|
|
|
|
|
|
function importLayerPageInit (ctx) {
|
|
|
|
|
|
|
|
var layerDepBtn = $("#add-layer-dependency-btn");
|
|
|
|
var importAndAddBtn = $("#import-and-add-btn");
|
2014-12-08 15:26:27 +00:00
|
|
|
var layerNameInput = $("#import-layer-name");
|
2014-11-28 20:12:18 +00:00
|
|
|
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 layerDeps = {};
|
|
|
|
var layerDepsDeps = {};
|
|
|
|
var currentLayerDepSelection;
|
|
|
|
var validLayerName = /^(\w|-)+$/;
|
|
|
|
|
2015-06-08 12:36:56 +00:00
|
|
|
libtoaster.makeTypeahead(layerDepInput, libtoaster.ctx.projectLayersUrl, { include_added: "true" }, function(item){
|
2014-11-28 20:12:18 +00:00
|
|
|
currentLayerDepSelection = item;
|
|
|
|
|
|
|
|
layerDepBtn.removeAttr("disabled");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/* We automatically add "openembedded-core" layer for convenience as a
|
|
|
|
* dependency as pretty much all layers depend on this one
|
|
|
|
*/
|
2015-07-31 12:09:05 +00:00
|
|
|
$.getJSON(libtoaster.ctx.projectLayersUrl,
|
|
|
|
{ include_added: "true" , search: "openembedded-core", format: "json" },
|
|
|
|
function(layer) {
|
|
|
|
if (layer.rows.length > 0) {
|
|
|
|
currentLayerDepSelection = layer.rows[0];
|
2014-11-28 20:12:18 +00:00
|
|
|
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=\"icon-trash\" data-toggle=\"tooltip\" title=\"Delete\"></span></li>");
|
|
|
|
|
|
|
|
newLayerDep.data('layer-id', currentLayerDepSelection.id);
|
|
|
|
newLayerDep.children("span").tooltip();
|
|
|
|
|
|
|
|
var link = newLayerDep.children("a");
|
2015-07-31 12:09:07 +00:00
|
|
|
link.attr("href", currentLayerDepSelection.layerdetailurl);
|
2014-11-28 20:12:18 +00:00
|
|
|
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);
|
|
|
|
|
2015-07-31 12:09:07 +00:00
|
|
|
libtoaster.getLayerDepsForProject(currentLayerDepSelection.layerdetailurl, function (data){
|
2014-11-28 20:12:18 +00:00
|
|
|
/* These are the dependencies of the layer added as a dependency */
|
|
|
|
if (data.list.length > 0) {
|
2015-07-31 12:09:07 +00:00
|
|
|
currentLayerDepSelection.url = currentLayerDepSelection.layerdetailurl;
|
2015-06-08 14:22:01 +00:00
|
|
|
layerDeps[currentLayerDepSelection.id].deps = data.list;
|
2014-11-28 20:12:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear the current selection */
|
|
|
|
layerDepInput.val("");
|
|
|
|
currentLayerDepSelection = undefined;
|
|
|
|
layerDepBtn.attr("disabled","disabled");
|
|
|
|
}, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
importAndAddBtn.click(function(){
|
2014-12-05 17:25:51 +00:00
|
|
|
/* This is a list of the names from layerDeps for the layer deps
|
|
|
|
* modal dialog body
|
|
|
|
*/
|
|
|
|
var depNames = [];
|
|
|
|
|
2014-11-28 20:12:18 +00:00
|
|
|
/* arrray of all layer dep ids includes parent and child deps */
|
|
|
|
var allDeps = [];
|
2014-12-05 17:25:51 +00:00
|
|
|
|
2014-11-28 20:12:18 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 17:25:51 +00:00
|
|
|
depNames.push(layerDeps[key].name);
|
2014-11-28 20:12:18 +00:00
|
|
|
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 };
|
2014-12-05 17:25:51 +00:00
|
|
|
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>";
|
|
|
|
|
2015-04-21 10:59:37 +00:00
|
|
|
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);
|
2014-11-28 20:12:18 +00:00
|
|
|
}
|
|
|
|
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(),
|
2015-04-10 17:15:03 +00:00
|
|
|
project_id: libtoaster.ctx.projectId,
|
2014-11-28 20:12:18 +00:00
|
|
|
layer_deps: layerDepsCsv,
|
|
|
|
};
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: ctx.xhrImportLayerUrl,
|
|
|
|
data: layerData,
|
|
|
|
headers: { 'X-CSRFToken' : $.cookie('csrftoken')},
|
|
|
|
success: function (data) {
|
|
|
|
if (data.error != "ok") {
|
|
|
|
console.log(data.error);
|
|
|
|
} else {
|
|
|
|
/* Success layer import now go to the project page */
|
2014-12-08 15:29:01 +00:00
|
|
|
$.cookie('layer-imported-alert', JSON.stringify(data), { path: '/'});
|
2015-07-31 12:09:12 +00:00
|
|
|
window.location.replace(libtoaster.ctx.projectPageUrl+'?notify=layer-imported');
|
2014-11-28 20:12:18 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (data) {
|
|
|
|
console.log("Call failed");
|
|
|
|
console.log(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-02 15:00:49 +00:00
|
|
|
function enable_import_btn(enabled) {
|
2014-11-28 20:12:18 +00:00
|
|
|
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");
|
|
|
|
|
|
|
|
for (var i=0; i<inputs.length; i++){
|
|
|
|
if (!(valid = inputs[i].value)){
|
|
|
|
enable_import_btn(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid)
|
|
|
|
enable_import_btn(true);
|
|
|
|
}
|
|
|
|
|
2015-03-02 15:00:49 +00:00
|
|
|
function layerExistsError(layer){
|
|
|
|
var dupLayerInfo = $("#duplicate-layer-info");
|
|
|
|
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.giturl);
|
|
|
|
dupLayerInfo.find("#dup-layer-revision").text(layer.revision);
|
|
|
|
|
|
|
|
$(".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 */
|
2015-07-31 12:09:05 +00:00
|
|
|
$.getJSON(libtoaster.ctx.projectLayersUrl,
|
|
|
|
{ include_added: "true" , search: name, format: "json" },
|
|
|
|
function(layer) {
|
|
|
|
if (layer.list.length > 0) {
|
|
|
|
for (var i in layer.list){
|
|
|
|
if (layer.list[i].name == name) {
|
|
|
|
console.log(layer.list[i])
|
|
|
|
layerExistsError(layer.list[i]);
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 15:00:49 +00:00
|
|
|
}
|
2015-07-31 12:09:05 +00:00
|
|
|
});
|
2015-03-02 15:00:49 +00:00
|
|
|
});
|
|
|
|
|
2015-02-06 19:18:19 +00:00
|
|
|
vcsURLInput.on('input', function() {
|
2014-11-28 20:12:18 +00:00
|
|
|
check_form();
|
|
|
|
});
|
|
|
|
|
2015-02-06 19:18:19 +00:00
|
|
|
gitRefInput.on('input', function() {
|
2014-11-28 20:12:18 +00:00
|
|
|
check_form();
|
|
|
|
});
|
|
|
|
|
2015-02-06 19:18:19 +00:00
|
|
|
layerNameInput.on('input', function() {
|
2014-11-28 20:12:18 +00:00
|
|
|
if ($(this).val() && !validLayerName.test($(this).val())){
|
|
|
|
layerNameCtrl.addClass("error")
|
|
|
|
$("#invalid-layer-name-hint").show();
|
|
|
|
enable_import_btn(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-02 15:00:49 +00:00
|
|
|
if ($("#duplicate-layer-info").css("display") != "None"){
|
|
|
|
$("#duplicate-layer-info").fadeOut(function(){
|
|
|
|
$(".fields-apart-from-layer-name").show();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-28 20:12:18 +00:00
|
|
|
/* Don't remove the error class if we're displaying the error for another
|
|
|
|
* reason.
|
|
|
|
*/
|
|
|
|
if (!duplicatedLayerName.is(":visible"))
|
|
|
|
layerNameCtrl.removeClass("error")
|
|
|
|
|
|
|
|
$("#invalid-layer-name-hint").hide();
|
|
|
|
check_form();
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Have a guess at the layer name */
|
|
|
|
vcsURLInput.focusout(function (){
|
|
|
|
/* 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())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ($(this).val().search("/")){
|
|
|
|
var urlPts = $(this).val().split("/");
|
|
|
|
var suggestion = urlPts[urlPts.length-1].replace(".git","");
|
|
|
|
layerNameInput.val(suggestion);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|