Skip to content

Resources: Moved builder script & css from jquery-wp-content to resources/builder #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 31, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pages/download-builder.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"pageTemplate": "page-contentfull.php"
}
</script>
<link rel="stylesheet" href="/jquery-wp-content/themes/jquerymobile.com/builder.css" />
<script src="/jquery-wp-content/themes/jquerymobile.com/builder.js"></script>
<link rel="stylesheet" href="../resources/builder/builder.css" />
<script src="../resources/builder/builder.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script>

<p class="intro">Customize your jQuery Mobile download by selecting the specific modules you need in the form below. For standard configurations, <a href="/download">download or link to CDN versions</a> of pre-built packages. Please note that the jQuery Mobile Download Builder is still in alpha, and as such should <em>not</em> be used on production websites.<br/><br/>
Expand Down
59 changes: 59 additions & 0 deletions resources/builder/builder.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
.loading {
background: url(/jquery-wp-content/themes/jquerymobile.com/i/ajax.gif) center center no-repeat;
margin: 20px 0 0 0;
}
.sub ul li {
line-height: 1.5em;
margin: 0 0 4px -10px;
}
.builder {
padding: 0 2em;
min-height: 8em;
list-style: none;
}
.builder li {
display: block;
}
.builder label {
display: inline-block;
padding: 0 0 0 6px;
}
.group {
clear: both;
}
.hed-cat {
font-size: 2em;
margin: 0 20px;
padding: 15px 0 5px 0;
border-bottom: 3px solid #ddd;
text-transform: capitalize;
}
.group>ul#events {
border: none;
}
.desc {
display: block;
color: #777;
font-size: .9em;
}
.select-all {
display: block;
float: right;
font-size: 1.2em;
margin: 15px 20px 0 0;
}
.branches {
float: left;
font-size: 1.4em;
font-weight: bold;
margin: 10px 40px;
}
.branches select {
margin: 10px 0 0 0;
clear: both;
float: left;
}

#download {
display: none;
}
252 changes: 252 additions & 0 deletions resources/builder/builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
$( function( $ ) {
var host = "http://amd-builder.jquerymobile.com",
dependencyMap,
builderhtml = [],
sortable = [],
groupBy = function( data, iterator ) {
var res = {};

_.each( _.uniq( _.map( data, iterator ) ), function( val ) {
res[val] = {};
});

_.each( data, function( value, key, list ) {
if ( value.group ) {
res[ value.group ][ key ] = value;
} else {
res.Other[ key ] = value;
}
});
return res;
},
module2domId = function( module ) {
return module.replace( /\./g, '-' )
.replace( /^(.)/, function( c ) { return c.toLowerCase(); } )
.replace( /\//g, '-slash-' );
},
domId2module = function( domId ) {
return domId.replace( /-slash-/g, '/' )
.replace( /\-/g, '.' );
},
group2domId = function( group ) {
return group.replace( / /g, '-' ).replace( /^(.)/, function( c ) { return c.toLowerCase(); } );
},
strip = function( file ) {
return file.replace( /^\.\//g, '' ).replace( /\./g, '-' );
},
buildForm = function( data ) {
var $form = $( "#builder" ).empty(),
groupedComponents = groupBy( data, function( o, key ) {
return ( o.group || "Other" );
}),
groups = _.keys( groupedComponents ).sort();

_.forEach( groups, function( group ) {
if ( group !== "exclude" ) {
var $group = $( "<ul>" ).attr( "id", group2domId( group ) ),
catlength = 0,
cat,
components = _.keys( groupedComponents[ group ] ).sort();

_.forEach( components, function( name ) {
var id = module2domId( name ),
label = data[ name ].label,
desc = data[ name ].description,
req = data[ name ].required,
labelm = "<label for='" + id + "'>" + label + "</label>",
inputm = "<input type='checkbox' class='inc' id='" + id + "' name='" + id + "'" + ( req ? " checked='checked' disabled='true'" : "") + "/>",
descm = "<p class='desc'>" + desc + "</p>",
item = inputm;

if ( label ) {
item = item + labelm;
if ( desc ) { item = item + descm; }

$group.append( "<li>" + item + "</li>" );
catlength++;
}
});

if( catlength ) {
cat = $("<div class='group'></div>")
.append( "<label class='select-all'> Select all <input type='checkbox' class='sel-all' name='select-all-" + group + "' /></label> <h3 class='hed-cat'>" + group + "</h3>" )
.append( $group );

$form.append( cat );
}
}
});

// trace dependencies for required modules and disable their dependencies
$form.find( "input:checkbox:disabled:checked" ).each(
function() {
_.each( buildCheckListFor( domId2module( $( this ).attr( "id" ) ) ),
function( module ) {
$( "#"+module2domId(module) )
.prop( "checked", true )
.trigger( "change" )
.attr( "disabled", true );
}
);
}
);

$form.append( '<input type="submit" value="Build My Download" class="buildBtn">' ).removeClass( "loading" );
},
buildCheckListFor = function( id, hash ) {
var module = dependencyMap[ id ];
hash = hash || {};
if ( module && module.deps ) {
_.each( module.deps, function( name, index ) {
if ( !( name in hash) ) {
hash[ name ] = true;
buildCheckListFor( name, hash );
}
});
}
return _.keys( hash );
},
buildUncheckListFor = function( id, hash ) {
hash = hash || {};
_.each( dependencyMap, function( module, name ) {
if ( !( name in hash ) ) {
if ( _.indexOf( module.deps, id ) > -1 ) {
hash[ name ] = true;
buildUncheckListFor( name, hash );
}
}
});
return _.keys( hash );
},
resolveDependencies = function( e ) {
var $el = $( e.target ),
key, i,
id = domId2module( $el.attr( 'id' ) ),
dep = dependencyMap[ id ],
checked = $el.is( ':checked' ),
list;

if ( checked ) {
list = buildCheckListFor( id );
_.each( list, function( name ) {
$( '#' + module2domId( name ) ).attr( 'checked', 'checked' );
});
} else {
list = buildUncheckListFor( id );
_.each( list, function( name ) {
$( '#' + module2domId( name ) ).removeAttr( 'checked' );
});
}
},
selectAll = function( e ) {
var $el = $( e.target ),
elval = $el.prop( "checked" );

$el.closest( ".group" ).find( "ul input:checkbox" ).not( ":disabled" ).prop( "checked", elval ).trigger( "change" );
},
refreshForm = function() {
var branch = $( "#branch option:selected" ).val() || "master";
$.getJSON( host + '/v1/dependencies/jquery/jquery-mobile/' + branch + '/?baseUrl=js' ).done(
function( data ) {
dependencyMap = data;
// Clean up deps attr from relative paths and plugins
_.each( dependencyMap, function( value, key, map ) {
if ( value.group && value.group === "exclude" ) {
delete map[ key ];
} else if ( value.deps ) {
_.each( value.deps, function( v, k, m ) {
m[ k ] = m[ k ].replace( /^.*!/, "" ); // remove the plugin part
m[ k ] = m[ k ].replace( /\[.*$/, "" ); // remove the plugin arguments at the end of the path
m[ k ] = m[ k ].replace( /^\.\//, "" ); // remove the relative path "./"
});
}
});
buildForm( dependencyMap );
}
);
},
refreshImageBundleLink = function() {
var branch = $( "#branch option:selected" ).val(),
url = "http://code.jquery.com/mobile/" + branch + "/jquery.mobile.images-" + branch + ".zip";

if ( branch === "master" ) {
url = "https://github.com/jquery/jquery-mobile/tree/master/css/themes/default/images";
}

$( "a#imageBundleLink" ).attr( "href", url );
},
isGreaterThan = function( ref, numVer ) {
return ( parseInt( ref.replace( /\./g, "" ), 10 ) > numVer );
},
isLessThan = function( ref, numVer ) {
return ( parseInt( ref.replace( /\./g, "" ), 10 ) < numVer );
};

refreshForm();
refreshImageBundleLink();

$( document )
.delegate( '.inc', 'change', resolveDependencies )
.delegate( '.inc', 'click', function( e ) {
$( e.target ).closest( ".group" ).find( ".sel-all" ).prop( "checked", false );
})
.delegate( '.sel-all', 'change', selectAll );

$( '#branch' ).change( refreshForm );
$( '#branch' ).change( refreshImageBundleLink );

$( "#builder" ).bind( 'submit',
function( e ) {
var $el = $( this ),
formData = $el.find( ':checkbox[id]:checked' ),
ref = $( "#branch option:selected" ).val() || "master",
$button = $( e.target ).find( "input[type=submit]" ),
exclude = [ "jquery", "json", "json!../package.json" ],
config;

$button.attr( "disabled", true );
e.preventDefault();
e.stopImmediatePropagation();

if ( isLessThan( ref, 140 ) && isGreaterThan( ref, 130 ) ) {
// in 1.3.1 we added the path to requirejs plugins to the path config
exclude = [ "jquery", "json", "depend", "json!../package.json" ];
} else if ( isLessThan( ref, 130 ) && isGreaterThan( ref, 120 ) ) {
exclude = [ "jquery", "text", "depend", "text!../version.txt" ];
} else if ( ref.indexOf( "1.1" ) === 0 ) {
exclude = [ "jquery","../external/requirejs/order", "../external/requirejs/depend", "../external/requirejs/text", "../external/requirejs/text!../version.txt" ];
}

config = {
baseUrl: "js",
include: formData.map( function() { return domId2module( $( this ).attr( 'id' ) ); } ).toArray().join( "," ),
// The excludes need to be kept in sync with the ones in jQM's Makefile
exclude: exclude.join( "," ),
wrap: JSON.stringify({
startFile: "build/wrap.start",
endFile: "build/wrap.end"
}),
pragmasOnSave: '{ "jqmBuildExclude": true }',
preserveLicenseComments: false,
skipModuleInsertion: true,
filter: "../build/filter"
};

if ( isGreaterThan( ref, 130 ) || ref === "master") {
// Starting at 1.3.1 we use requirejs.config.js
$.extend( config, {
mainConfigFile: "js/requirejs.config.js"
});
}

$( "#download" ).html(
$( "<iframe>" )
.attr( "src", host + '/v1/bundle/jquery/jquery-mobile/' + ref + '/jquery.mobile.custom.zip?' + $.param( config ) )
);

// I could not leverage iframe.onload to re-enable the button :-/
setTimeout( function() {
$button.attr( "disabled", false );
}, 1000 );
});
});