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" ) ;
var layerNameInput = $ ( "#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 layerDeps = { } ;
var layerDepsDeps = { } ;
var currentLayerDepSelection ;
var validLayerName = /^(\w|-)+$/ ;
$ ( "#new-project-button" ) . hide ( ) ;
libtoaster . makeTypeahead ( layerDepInput , ctx . xhrDataTypeaheadUrl , { type : "layers" , project _id : ctx . projectId , include _added : "true" } , function ( item ) {
currentLayerDepSelection = item ;
layerDepBtn . removeAttr ( "disabled" ) ;
} ) ;
/ * W e a u t o m a t i c a l l y a d d " o p e n e m b e d d e d - c o r e " l a y e r f o r c o n v e n i e n c e a s a
* dependency as pretty much all layers depend on this one
* /
$ . getJSON ( ctx . xhrDataTypeaheadUrl , { type : "layers" , project _id : ctx . projectId , include _added : "true" , value : "openembedded-core" } , function ( layer ) {
if ( layer . list . length == 1 ) {
currentLayerDepSelection = layer . list [ 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=\"icon-trash\" data-toggle=\"tooltip\" title=\"Delete\"></span></li>" ) ;
newLayerDep . data ( 'layer-id' , currentLayerDepSelection . id ) ;
newLayerDep . children ( "span" ) . tooltip ( ) ;
var link = newLayerDep . children ( "a" ) ;
link . attr ( "href" , ctx . layerDetailsUrl + String ( currentLayerDepSelection . id ) ) ;
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 ( ctx . xhrDataTypeaheadUrl , ctx . projectId , currentLayerDepSelection . id , function ( data ) {
/* These are the dependencies of the layer added as a dependency */
if ( data . list . length > 0 ) {
currentLayerDepSelection . url = ctx . layerDetailsUrl + currentLayerDepSelection . id ;
layerDeps [ currentLayerDepSelection . id ] . deps = data . list
}
/* Clear the current selection */
layerDepInput . val ( "" ) ;
currentLayerDepSelection = undefined ;
layerDepBtn . attr ( "disabled" , "disabled" ) ;
} , null ) ;
} ) ;
importAndAddBtn . click ( function ( ) {
2014-12-05 17:25:51 +00:00
/ * T h i s i s a l i s t o f t h e n a m e s f r o m l a y e r D e p s f o r t h e l a y e r d e p s
* 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
/ * t e m p o r a r y o b j e c t t o u s e t o d o a r e d u c e o n t h e d e p e n d e n c i e s f o r e a c h
* layer dependency added
* /
var depDeps = { } ;
/ * t h e l a y e r s t h a t h a v e d e p e n d e n c i e s h a v e a n e x t r a p r o p e r t y " d e p s "
* 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>" ;
show _layer _deps _modal ( ctx . projectId , layer , depDepsArray , title , body , false , function ( selected ) {
2014-11-28 20:12:18 +00:00
/* Add the accepted dependencies to the allDeps array */
if ( selected . length > 0 ) {
2014-12-05 17:25:51 +00:00
allDeps = allDeps . concat ( selected ) ;
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 ( ) ,
summary : $ ( "#layer-summary" ) . val ( ) ,
dir _path : $ ( "#layer-subdir" ) . val ( ) ,
project _id : ctx . projectId ,
layer _deps : layerDepsCsv ,
} ;
$ . ajax ( {
type : "POST" ,
url : ctx . xhrImportLayerUrl ,
data : layerData ,
headers : { 'X-CSRFToken' : $ . cookie ( 'csrftoken' ) } ,
success : function ( data ) {
if ( data . error != "ok" ) {
show _error _message ( data , layerData ) ;
console . log ( data . error ) ;
} else {
2014-12-05 17:25:51 +00:00
layerData . layersAdded = data . layers _added ;
2014-11-28 20:12:18 +00:00
/* Success layer import now go to the project page */
2014-12-05 17:25:51 +00:00
$ . cookie ( 'layer-imported-alert' , JSON . stringify ( layerData ) , { path : '/' } ) ;
window . location . replace ( ctx . projectPageUrl + '#/layerimported' ) ;
2014-11-28 20:12:18 +00:00
}
} ,
error : function ( data ) {
console . log ( "Call failed" ) ;
console . log ( data ) ;
}
} ) ;
}
} ) ;
function show _error _message ( error , layerData ) {
var errorMsg = $ ( "#import-error" ) . fadeIn ( ) ;
var errorType = error . error ;
var body = errorMsg . children ( "span" ) ;
var title = errorMsg . children ( "h3" ) ;
var optionsList = errorMsg . children ( "ul" ) ;
var invalidLayerRevision = $ ( "#invalid-layer-revision-hint" ) ;
var layerRevisionCtrl = $ ( "#layer-revision-ctrl" ) ;
/* remove any existing items */
optionsList . children ( ) . each ( function ( ) { $ ( this ) . remove ( ) ; } ) ;
body . text ( "" ) ;
title . text ( "" ) ;
invalidLayerRevision . hide ( ) ;
layerNameCtrl . removeClass ( "error" ) ;
layerRevisionCtrl . removeClass ( "error" ) ;
switch ( errorType ) {
case 'hint-layer-version-exists' :
title . text ( "This layer already exists" ) ;
body . html ( "A layer <strong>" + layerData . name + "</strong> already exists with this Git repository URL and this revision. You can:" ) ;
optionsList . append ( "<li>Import <strong>" + layerData . name + "</strong> with a different revision </li>" ) ;
optionsList . append ( "<li>or <a href=\"" + ctx . layerDetailsUrl + error . existing _layer _version + "/\" >change the revision of the existing layer</a></li>" ) ;
layerRevisionCtrl . addClass ( "error" ) ;
invalidLayerRevision . html ( "A layer <strong>" + layerData . name + "</strong> already exists with this revision.<br />You can import <strong>" + layerData . name + "</strong> with a different revision" ) ;
invalidLayerRevision . show ( ) ;
break ;
case 'hint-layer-exists-with-different-url' :
title . text ( "This layer already exists" ) ;
body . html ( "A layer <strong>" + layerData . name + "</strong> already exists with a different Git repository URL:<br /><br />" + error . current _url + "<br /><br />You Can:" ) ;
optionsList . append ( "<li>Import the layer under a different name</li>" ) ;
optionsList . append ( "<li>or <a href=\"" + ctx . layerDetailsUrl + error . current _id + "/\" >change the Git repository URL of the existing layer</a></li>" ) ;
duplicatedLayerName . html ( "A layer <strong>" + layerData . name + "</strong> already exists with a different Git repository URL.<br />To import this layer give it a different name." ) ;
duplicatedLayerName . show ( ) ;
layerNameCtrl . addClass ( "error" ) ;
break ;
case 'hint-layer-exists' :
title . text ( "This layer already exists" ) ;
body . html ( "A layer <strong>" + layerData . name + "</strong> already exists: You Can:" ) ;
optionsList . append ( "<li>Import the layer under a different name</li>" ) ;
break ;
default :
title . text ( "Error" )
body . text ( data . error ) ;
}
}
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" ) ;
for ( var i = 0 ; i < inputs . length ; i ++ ) {
if ( ! ( valid = inputs [ i ] . value ) ) {
enable _import _btn ( false ) ;
break ;
}
}
if ( valid )
enable _import _btn ( true ) ;
}
vcsURLInput . keyup ( function ( ) {
check _form ( ) ;
} ) ;
gitRefInput . keyup ( function ( ) {
check _form ( ) ;
} ) ;
layerNameInput . keyup ( function ( ) {
if ( $ ( this ) . val ( ) && ! validLayerName . test ( $ ( this ) . val ( ) ) ) {
layerNameCtrl . addClass ( "error" )
$ ( "#invalid-layer-name-hint" ) . show ( ) ;
enable _import _btn ( false ) ;
return ;
}
/ * D o n ' t r e m o v e t h e e r r o r c l a s s i f w e ' r e d i s p l a y i n g t h e e r r o r f o r a n o t h e r
* 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 ( ) {
/ * I f w e a l a y e r n a m e s p e c i f i e d d o n ' t o v e r w r i t e i t o r i f t h e r e i s n ' 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 ) ;
}
} ) ;
}