// --------------------------------------------------------------------
function init_extras()
{
// initialize the layout
$( "#tabs-extras .left-panel" ).resizable( {
resizeHeight: false,
handles: "e",
create: function( event, ui ) {
$( ".ui-resizable-e" ).css( "cursor", "ew-resize" ) ;
},
} ) ;
// identify the extras templates
var extra_templates = [] ;
for ( var template_id in gTemplatePack.templates ) {
if ( template_id.substr( 0, 7 ) === "extras/" ) {
extra_templates.push(
_parse_extra_template( template_id, gTemplatePack.templates[template_id] )
) ;
}
}
// sort the extras templates by name
extra_templates.sort( function( lhs, rhs ) {
return lhs.name.localeCompare( rhs.name, "en", { sensitivity: "base" } ) ;
} ) ;
// build the side-panel showing the available templates
var $index = $( "
" ) ;
for ( var i=0 ; i < extra_templates.length ; ++i ) {
var buf = [] ;
buf.push( "",
"", extra_templates[i].name, "
"
) ;
if ( extra_templates[i].caption )
buf.push( "", extra_templates[i].caption, "
" ) ;
buf.push( "", "" ) ;
var $entry = $( buf.join("") ) ;
if ( i === 0 )
$entry.addClass( "ui-selecting" ) ;
$entry.data( "template_id", extra_templates[i].template_id ) ;
$index.append( $entry ) ;
}
$index.selectable( {
selected: function( evt, ui ) {
_show_extra_template( $(ui.selected).data( "template_id" ) ) ;
},
selecting: function( evt, ui ) { // nb: prevent multiple selections
if ( $index.find( ".ui-selected, .ui-selecting" ).length > 1 )
$(ui.selecting).removeClass( "ui-selecting" ) ;
},
} ) ;
$index.data( "ui-selectable" )._mouseStop( null ) ; // nb: trigger the selection
$( "#tabs-extras .left-panel .content" ).empty().append( $index ) ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function _show_extra_template( template_id )
{
// parse the template (nb: we do this every time since the user may have changed it in the UI)
var template_info = _parse_extra_template( template_id, gTemplatePack.templates[template_id] ) ;
// generate the form for entering the template parameters
var buf = [ "" ] ;
buf.push( "
", template_info.name, "
" ) ;
if ( template_info.caption )
buf.push( "
", template_info.caption, "
" ) ;
if ( template_info.description )
buf.push( "
", template_info.description, "
" ) ;
if ( template_info.params.length > 0 ) {
buf.push( "
" ) ;
}
buf.push( "
" ) ;
buf.push( "
" ) ;
var $form = $( buf.join("") ) ;
$form.find( "select" ).select2( {
minimumResultsForSearch: -1
} ).on( "select2:open", function() {
restrict_droplist_height( $(this) ) ;
} ) ;
fixup_external_links( $form ) ;
// initialize the "generate" button
init_snippet_button( $form.find( "button.generate" ) ) ;
// install the form
$( "#tabs-extras .right-panel" ).empty().append( $form ) ;
}
// --------------------------------------------------------------------
function _parse_extra_template( template_id, template )
{
// extract the main details
var result = { template_id: template_id, name: template_id } ;
function extract_val( key ) {
var match = template.match( new RegExp( "" ) ) ;
if ( match )
result[key] = match[1] ;
}
extract_val( "name" ) ;
extract_val( "caption" ) ;
extract_val( "description" ) ;
// extract the template parameters
result.params = [] ;
var params_regex = new RegExp( /\{\{(.*?)\}\}/g ) ;
while( (match = params_regex.exec( template )) !== null ) {
// extract the parameter name and default value
var words = match[1].split( "|" ) ;
var param = { name: words[0] } ;
var pos = param.name.indexOf( ":" ) ;
if ( pos === -1 )
continue ;
var val = param.name.substr( pos+1 ) ;
param.name = param.name.substr( 0, pos ) ;
// figure out what type of parameter we have
if ( val.indexOf( "::" ) !== -1 ) {
// we have a