%{

def caller = _caller
while (caller._caller) caller = caller._caller
def sm = caller.gsSM

( _arg ) &&  ( _import = _arg);
def a = _import
if (!a) a = _require

if (!(a || _load || _loadMissing || _loadAll)) throw new RuntimeException("either load or import statement needed");

if (a) sm.addJsMissings(a);

if (_load) {
	def missings = play.modules.greenscript.utils.DependencyManager.JS_DEP_MGR.comprehend(_load)
	sm.addJsMissings(missings)
}

if (!_charset) _charset='utf-8'

if (!_loadAll) _loadAll = _loadMissing

}%

#{if (sm.minimize())}
	%{
		def fn = "", l = null, cdn = null;
		if (_load) {
			l = sm.addJsLoaded(_load)
			fn = play.modules.greenscript.utils.Minimizor.minimizeJs(l)
			cdn = sm.getCdnItems(l)
		}
	%}
    #{if (cdn != null && !cdn.isEmpty()) }
        #{list items:cdn, as: '_'}
            <script type="text/javascript" src="${_}" #{if _id} id="${_id}"#{/if} charset="${_charset}"></script>
        #{/list}
    #{/if}
    #{if (!fn.isEmpty())}
        <script type="text/javascript" src="${sm.gsUrl(fn)}" #{if _id} id="${_id}"#{/if} charset="${_charset}"></script>
    #{/if}
    %{sm.clearLoaded()}%

    #{if _loadAll}
        %{
        	l = sm.getJsMissings()
        	fn = play.modules.greenscript.utils.Minimizor.minimizeJs(l);
        	cdn = sm.getCdnItems(l)
        }%
        #{if (cdn != null && !cdn.isEmpty()) }
            #{list items: cdn, as: '_'}
                <script type="text/javascript" src="${_}" #{if _id} id="${_id}"#{/if} charset="${_charset}"></script>
            #{/list}
        #{/if}
        #{if (!fn.isEmpty())}
            <script type="text/javascript" src="${sm.gsUrl(fn)}" #{if _id} id="${_id}"#{/if} charset="${_charset}"></script>
        #{/if}
    #{/if}
#{/if}
#{else}
	#{if _load}
    	#{list items:sm.addJsLoaded(_load), as:'name'}
        	<script type="text/javascript" src="${sm.jsUrl(name)}" #{if _id} id="${_id}"#{/if} charset="${_charset}"></script>
    	#{/list}
    #{/if}
    %{sm.clearLoaded()}%
    #{if _loadAll}
        #{list items:sm.getJsMissings(), as:'name'}
            <script type="text/javascript" src="${sm.jsUrl(name)}" #{if _id} id="${_id}"#{/if} charset="${_charset}"></script>
        #{/list}
    #{/if}
#{/else}

