##
## JS Libraries.
##
$xwiki.jsfx.use('js/amd/require.js', {'defer': false, 'minimize':false})
$xwiki.jsfx.use('js/prototype/prototype.js', {'defer': false, 'minimize':false})
$xwiki.jsfx.use('js/scriptaculous/effects.js', true)
##
## In order to make it easy to debug Javascript in browsers, allow the request parameter "minify=false" to be passed to
## avoid using the minified versions of JS/CSS.
##
#if ("$!request.minify" == 'false')
  $xwiki.jsfx.use('js/xwiki/xwiki.js', {'forceSkinAction': true, 'defer': false})
  $xwiki.jsfx.use('js/xwiki/widgets/modalPopup.js', true)
  $xwiki.ssfx.use('js/xwiki/widgets/modalPopup.css', {'forceSkinAction' : true, 'colorTheme' : $themeDoc})
  $xwiki.jsfx.use('js/xwiki/widgets/jumpToPage.js', true)
  $xwiki.ssfx.use('js/xwiki/widgets/jumpToPage.css', {'forceSkinAction' : true, 'language' : $xcontext.language})
  $xwiki.jsfx.use('uicomponents/model/entityReference.js')
  $xwiki.jsfx.use('uicomponents/widgets/confirmationBox.js', true)
  $xwiki.ssfx.use('uicomponents/widgets/confirmationBox.css', true)
  $xwiki.jsfx.use('uicomponents/widgets/confirmedAjaxRequest.js', true)
  $xwiki.jsfx.use('uicomponents/widgets/notification.js', true)
  $xwiki.ssfx.use('uicomponents/widgets/notification.css', true)
  $xwiki.jsfx.use('uicomponents/widgets/list/xlist.js')
  $xwiki.jsfx.use('uicomponents/suggest/suggest.js', {'forceSkinAction' : true, 'language' : $xcontext.language})
  $xwiki.ssfx.use('uicomponents/suggest/suggest.css', true)
#else
  $xwiki.jsfx.use('js/xwiki/xwiki-min.js', {'forceSkinAction': true, 'defer': false, 'language': $xcontext.language})
  $xwiki.ssfx.use('css/xwiki-min.css', {'forceSkinAction': true, 'colorTheme': $themeDoc, 'language': $xcontext.language})
#end
#if($xwiki.exists('XWiki.SearchSuggestConfig'))
  ## Hook to request the search suggest script if search suggest configuration is present.
  ## We can't just have it as a always-use SSX yet since we compute a hash to be sure to refresh
  ## the file when the configuration changes.
  ## Ideally, this code should be located in a interface extension, in the search application.
  #set($searchSuggestConfigDocument = $xwiki.getDocument('XWiki.SearchSuggestConfig'))
  #set($searchSuggestConfigObject = $searchSuggestConfigDocument.getObject('XWiki.SearchSuggestConfig'))
  #if($searchSuggestConfigObject.getProperty('activated').value == 1)
    #set($searchSuggestConfigHash = '')
    #foreach($source in $searchSuggestConfigDocument.getObjects('XWiki.SearchSuggestSourceClass'))
      #if($source.getValue('activated') == 1)
        #set($sourceId = "${source.getValue('name')}:${source.getValue('url')}:${source.getValue('highlight')}:${source.getValue('icon')}}")
        #set($searchSuggestConfigHash = "${searchSuggestConfigHash}|${sourceId}")
      #end
    #end
    $xwiki.jsfx.use('uicomponents/search/searchSuggest.js', {
      'forceSkinAction' : true,
      'h' : $searchSuggestConfigHash.hashCode()
    })
    $xwiki.ssfx.use('uicomponents/search/searchSuggest.css', true)
  #end
#end
#set($editActions = ['edit', 'inline', 'admin'])
#if($editActions.contains($xcontext.action))
  $xwiki.jsfx.use('uicomponents/lock/lock.js')
  $xwiki.jsfx.use('uicomponents/widgets/validation/livevalidation_prototype.js')
  $xwiki.ssfx.use('uicomponents/widgets/validation/livevalidation.css', true)
#end
##
## Hooks for inserting JavaScript skin extensions
##
#javaScriptExtensionHooks
##
## Compatibility "aspect" file for deprecated code.
## Placed at the very end of the stream so that skin file extensions code can be deprecated easily as well.
##
<script type="text/javascript" src="$xwiki.getSkinFile("js/xwiki/compatibility.js", false)" defer="defer"></script>
## Marker script that signals that all the deferred scripts have indeed been executed, guarding against a premature dom:loaded event
<script type="text/javascript" src="$xwiki.getSkinFile("js/xwiki/markerScript.js", false)" defer="defer"></script>
<!--[if lt IE 9]>
  ## HTML5Shiv enable the compatibility with IE8
  <script src="$services.webjars.url('html5shiv/3.7.2/html5shiv.min.js')"></script>
  ## Respond.js implements the CSS 3 media queries for IE8
  <script type="text/javascript" src="$services.webjars.url('respond/1.4.2/dest/respond.min.js')" defer="defer"></script>
<![endif]-->
<script type="text/javascript">
// <![CDATA[
## Requirejs will automatically add a ".js" suffix if the generated URL doesn't contain a "?". It happens that we
## don't generate a URL with "?" when we export in HTML for example. In this case we remove the ".js" suffix since
## requirejs will add one...
## Note that we cannot do this generically in the webjars module when exporting in HTML because the webjars module
## provide generic URLs and they don't all go through requirejs...
## Note that this comment and code below are not correctly indentend voluntarily in order to generate nicely formatted
## (and valid) code at runtime!
#macro(removeJsSuffix $expr)
## Note that velocity takes argument references by name (see: https://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html#velocimacros).
## So we set the value of the $expr in the $url variable to not execute $expr multiple times.
#set ($url = $expr)
#if (!$url.contains('?'))$stringtool.removeEnd($url, '.js'))#else$url#{end}
#end
##
## Start the requirejs config.
##
require.config({
## see: http://requirejs.org/docs/jquery.html#noconflictmap to see why this works.
  paths: {
## We handle the ability to disable the minification, by choosing the file extension to add to the script paths.
#if($request.minify == 'false')
  #set($jsExtension = '.js')
#else
  #set($jsExtension = '.min.js')
#end
    'jquery': '#removeJsSuffix($services.webjars.url("jquery/1.11.1/jquery${jsExtension}"))',
    'bootstrap': '#removeJsSuffix($services.webjars.url("bootstrap/3.2.0/js/bootstrap${jsExtension}"))'
  },
  shim: {
    'bootstrap' : ['jquery']
  },
  map: {
    '*': {
      'jquery': 'jQueryNoConflict'
    },
    'jQueryNoConflict': {
      'jquery': 'jquery'
    },
  }
});
define('jQueryNoConflict', ['jquery'], function ($) {
    return $.noConflict();
});
if (Prototype.BrowserFeatures.ElementExtensions) {
  require(['jquery', 'bootstrap'], function ($) {
    // Fix incompatibilities between BootStrap and Prototype
    var disablePrototypeJS = function (method, pluginsToDisable) {
      var handler = function (event) {
        event.target[method] = undefined;
        setTimeout(function () {
            delete event.target[method];
        }, 0);
      };
      pluginsToDisable.each(function (plugin) { 
          $(window).on(method + '.bs.' + plugin, handler); 
      });
    },
    pluginsToDisable = ['collapse', 'dropdown', 'modal', 'tooltip'];
    disablePrototypeJS('show', pluginsToDisable);
    disablePrototypeJS('hide', pluginsToDisable);
  });
}
XWiki.webapppath = "${xwiki.webAppPath}";
XWiki.servletpath = "${xwiki.servletPath}";
XWiki.contextPath = "${request.contextPath}";
XWiki.mainWiki = "$!{escapetool.javascript(${xcontext.mainWikiName})}";
XWiki.currentWiki = "$!{escapetool.javascript(${xcontext.database})}";
XWiki.currentSpace = "$!{escapetool.javascript($doc.space)}";
XWiki.currentPage = "$!{escapetool.javascript($doc.name)}";
XWiki.editor = "$!{escapetool.javascript($editor)}";
XWiki.viewer = "$!{escapetool.javascript($viewer)}";
XWiki.contextaction = "${xcontext.getAction()}";
XWiki.docisnew = $!doc.isNew();
XWiki.docsyntax = "$!doc.getSyntax().toIdString()";
XWiki.docvariant = "$!{escapetool.javascript($docvariant.replace('&amp;', '&'))}";
XWiki.blacklistedSpaces = [ #foreach($space in $blacklistedSpaces)#if($velocityCount > 1),#end"$space"#end ];
XWiki.hasEdit = $hasEdit;
XWiki.hasProgramming = $hasProgramming;
XWiki.hasBackupPackImportRights =#if ($xwiki.package) $xwiki.package.hasBackupPackImportRights()#else false#end;
XWiki.hasRenderer = $services.rendering.availableRendererSyntaxes.contains($!doc.getSyntax());
window.docviewurl = "${doc.getURL('view')}";
window.docediturl = "${doc.getURL('edit')}";
window.docsaveurl = "${doc.getURL('save')}";
window.docgeturl = "${doc.getURL('get')}";
// ]]>
</script>
