<commit_before>/*
 * common part for `nw` && `connectjs` front ends
 */

var app// FIXME: check if this can be just `App`, one global var is needed

(function gc_wrapper(doc, w, con){
    app = {
        config: null,
        btn: null,// quick launch buttons to access when error
        extjs_helper: extjs_load_gc_wrapped
    }
    return

/* init stuff must be garbage collected */

function extjs_load_gc_wrapped(){
var path, extjs, t

    extjs = app.config.extjs
    path = extjs.path
    css_load(path + 'resources/css/ext-all.css')

    if(extjs.launch && extjs.launch.css){
        for(t = 0; t < extjs.launch.css.length; ++t){
            css_load(extjs.launch.css[t], app.config.backend.url)
        }
    }

    extjs = doc.createElement('script')
    extjs.setAttribute('type', 'application/javascript')
    extjs.setAttribute('charset', 'utf-8')
    extjs.setAttribute('src', path + 'ext-all-nw.js')//`loadScriptFile()` fixed
    doc.head.appendChild(extjs)

    extjs = setInterval(function waiting_extjs(){
        if(!w.Ext) return// waiting if xhr HEAD check is done

        clearInterval(extjs)
        app.extjs_helper = css_load
        extjs = path + 'locale/ext-lang-' + l10n.lang + '.js'
        Ext.Loader.loadScript({
            url: extjs,
            onError: function fail_load_locale(){
                throw new Error('Error loading locale file:\n' + extjs)
            }
        })
        con.log(
            'ExtJS version: ' + Ext.getVersion('extjs') + '\n ' +
            'ExtJS locale: ' + l10n.lang + '\n ' +
            'ExtJS is at <' + path + '>'
        )
        Ext.Loader.setPath('Ext.ux', path + 'examples/ux')

        if(app.config.backend.url){// `nw` context`
           /* patch ExtJS Loader to work from "file://" in `node-webkit`
            * also `debugSourceURL` removed in `ext-all-debug.js#loadScriptFile()`
            * it crushes `eval` there it's critical (plus there are more patches)
            *
            * `app.config.backend.url` has external IP (for remote HTTP)
            **/
            Ext.Loader._getPath = Ext.Loader.getPath
            Ext.Loader.getPath = function getPath(className){
            // load from `App.backendURL = '127.0.0.1'`
                return '/' == className[0] ?
                    App.backendURL + className + '.js' :
                    Ext.Loader._getPath(className)
            }
        }

        Ext.application({
            name: 'App',
            appFolder: app.config.extjs.appFolder || '.',
            controllers:[ 'Main' ],// loads App.controller.Main
            launch: extjs_launch
        })

        return
    }, 1024)
    con.log('extjs_load: done, waiting for ExtJS')

    return
}

function css_load(url, backend){
var el = doc.createElement('link')

    el.setAttribute('rel', 'stylesheet')
    el.setAttribute('href', (backend || '') + url)
    doc.head.appendChild(el)
    el = null
}

function extjs_launch(){
var t

    // global `App` object is available now
    if(app.backend_check){
        App.doCheckBackend = app.backend_check
        App.doRestartBackend = app.backend_restart
        App.doTerminateBackend = app.backend_terminate
        App.doShutdownBackend = app.backend_shutdown

        delete app.backend_check
        delete app.backend_restart
        delete app.backend_terminate
        delete app.backend_shutdown
    }
    App.cfg = app.config
    App.backendURL = App.cfg.backend.url ?
                    'http://127.0.0.1:' + App.cfg.backend.job_port : ''
    App.create = sub_app_create
    App.reload = sub_app_reload_devel_view
    App.getHelpAbstract = get_help_abstract

   /*
    * l10n: Access existing data via e.g.:
    * > l10n.docOpenAll
    * When developing to have some placeholders and viewable content without
    * need to fill of the `l10n` files use call:
    * > l10n('create_season')
    * The namespace of a module can be selected by:
    * > l10n._ns = 'so'; 'code with l10n("stuff")'; l10n._ns = ''
    * > l10n.so.stuff
    **/
    t = l10n
    l10n = l10n_provider
    l10n._ns = ''
    Ext.apply(l10n, t)

    // NOTE: Ext JSON decoding is useful for JS-as-JSON with `l10n` in values
    con.log('ExtJS Ext.encode: always native `JSON.stringify()`')
    Ext.encode = JSON.stringify
    Ext.Error.handle = Ext_Error_handle// by Ext.Error.raise()

    Ext.state.Manager.setProvider(new Ext.state.LocalStorageProvider)
    // Start loading The Application
    Ext.Loader.setPath('Ext.uxo', App.app.appFolder + '/uxo')

    t = Ext.Array.push([
        'App.model.Base',          // loading Models manually, then [M]VC
        'App.backend.Connection',  // `req`<->`res` with backend
        'App.store.CRUD',          // our CRUD for `Ext.data.*`
        'App.view.Window',         // provide core View Class(es)
        'App.view.desktop.Status'  // provide status
    ], App.cfg.extjs.launch.js || [ ])// more stuff from backend, if exists

    var j, i = 0, l = Ext.fly('startup').dom.lastChild

    do {
        l.innerHTML += '<br>' + (j = t[i])// show progress
        Ext.syncRequire(j)
    } while(++i < t.length)

    if(false !== App.cfg.createViewport){// if no auth app_module
        app.extjs_helper = null// mark for GC
        Ext.globalEvents.fireEvent('createViewport')
    }// else userman's: `App.um.controller.Login->createViewportAuth()`

    con.log('ExtJS + App launch: OK')
}

function l10n_provider(msg){
var me, m, idx, tail

    me = (me = l10n._ns) ? l10n[me] : l10n

    idx = msg.indexOf(':')

    if(idx > 0){
        tail = msg.slice(idx + 1)
        msg = msg.slice(0, idx)

        return me && (m = me[msg]) ?<commit_msg>extjs_load no auth module case: provide dummy `App.User.can`<commit_after>/*
 * common part for `nw` && `connectjs` front ends
 */

var app// FIXME: check if this can be just `App`, one global var is needed

(function gc_wrapper(doc, w, con){
    app = {
        config: null,
        btn: null,// quick launch buttons to access when error
        extjs_helper: extjs_load_gc_wrapped
    }
    return

/* init stuff must be garbage collected */

function extjs_load_gc_wrapped(){
var path, extjs, t

    extjs = app.config.extjs
    path = extjs.path
    css_load(path + 'resources/css/ext-all.css')

    if(extjs.launch && extjs.launch.css){
        for(t = 0; t < extjs.launch.css.length; ++t){
            css_load(extjs.launch.css[t], app.config.backend.url)
        }
    }

    extjs = doc.createElement('script')
    extjs.setAttribute('type', 'application/javascript')
    extjs.setAttribute('charset', 'utf-8')
    extjs.setAttribute('src', path + 'ext-all-nw.js')//`loadScriptFile()` fixed
    doc.head.appendChild(extjs)

    extjs = setInterval(function waiting_extjs(){
        if(!w.Ext) return// waiting if xhr HEAD check is done

        clearInterval(extjs)
        app.extjs_helper = css_load
        extjs = path + 'locale/ext-lang-' + l10n.lang + '.js'
        Ext.Loader.loadScript({
            url: extjs,
            onError: function fail_load_locale(){
                throw new Error('Error loading locale file:\n' + extjs)
            }
        })
        con.log(
            'ExtJS version: ' + Ext.getVersion('extjs') + '\n ' +
            'ExtJS locale: ' + l10n.lang + '\n ' +
            'ExtJS is at <' + path + '>'
        )
        Ext.Loader.setPath('Ext.ux', path + 'examples/ux')

        if(app.config.backend.url){// `nw` context`
           /* patch ExtJS Loader to work from "file://" in `node-webkit`
            * also `debugSourceURL` removed in `ext-all-debug.js#loadScriptFile()`
            * it crushes `eval` there it's critical (plus there are more patches)
            *
            * `app.config.backend.url` has external IP (for remote HTTP)
            **/
            Ext.Loader._getPath = Ext.Loader.getPath
            Ext.Loader.getPath = function getPath(className){
            // load from `App.backendURL = '127.0.0.1'`
                return '/' == className[0] ?
                    App.backendURL + className + '.js' :
                    Ext.Loader._getPath(className)
            }
        }

        Ext.application({
            name: 'App',
            appFolder: app.config.extjs.appFolder || '.',
            controllers:[ 'Main' ],// loads App.controller.Main
            launch: extjs_launch
        })

        return
    }, 1024)
    con.log('extjs_load: done, waiting for ExtJS')

    return
}

function css_load(url, backend){
var el = doc.createElement('link')

    el.setAttribute('rel', 'stylesheet')
    el.setAttribute('href', (backend || '') + url)
    doc.head.appendChild(el)
    el = null
}

function extjs_launch(){
var t

    // global `App` object is available now
    if(app.backend_check){
        App.doCheckBackend = app.backend_check
        App.doRestartBackend = app.backend_restart
        App.doTerminateBackend = app.backend_terminate
        App.doShutdownBackend = app.backend_shutdown

        delete app.backend_check
        delete app.backend_restart
        delete app.backend_terminate
        delete app.backend_shutdown
    }
    App.cfg = app.config
    App.backendURL = App.cfg.backend.url ?
                    'http://127.0.0.1:' + App.cfg.backend.job_port : ''
    App.create = sub_app_create
    App.reload = sub_app_reload_devel_view
    App.getHelpAbstract = get_help_abstract

   /*
    * l10n: Access existing data via e.g.:
    * > l10n.docOpenAll
    * When developing to have some placeholders and viewable content without
    * need to fill of the `l10n` files use call:
    * > l10n('create_season')
    * The namespace of a module can be selected by:
    * > l10n._ns = 'so'; 'code with l10n("stuff")'; l10n._ns = ''
    * > l10n.so.stuff
    **/
    t = l10n
    l10n = l10n_provider
    l10n._ns = ''
    Ext.apply(l10n, t)

    // NOTE: Ext JSON decoding is useful for JS-as-JSON with `l10n` in values
    con.log('ExtJS Ext.encode: always native `JSON.stringify()`')
    Ext.encode = JSON.stringify
    Ext.Error.handle = Ext_Error_handle// by Ext.Error.raise()

    Ext.state.Manager.setProvider(new Ext.state.LocalStorageProvider)
    // Start loading The Application
    Ext.Loader.setPath('Ext.uxo', App.app.appFolder + '/uxo')

    t = Ext.Array.push([
        'App.model.Base',          // loading Models manually, then [M]VC
        'App.backend.Connection',  // `req`<->`res` with backend
        'App.store.CRUD',          // our CRUD for `Ext.data.*`
        'App.view.Window',         // provide core View Class(es)
        'App.view.desktop.Status'  // provide status
    ], App.cfg.extjs.launch.js || [ ])// more stuff from backend, if exists

    var j, i = 0, l = Ext.fly('startup').dom.lastChild

    do {
        l.innerHTML += '<br>' + (j = t[i])// show progress
        Ext.syncRequire(j)
    } while(++i < t.length)

    if(false !== App.cfg.createViewport){// if no auth app_module
        app.extjs_helper = null// mark for GC
        Ext.globalEvents.fireEvent('createViewport')
        App.User = { can: { }}// dummy auth object
    }// else userman's: `App.um.controller.Login->createViewportAuth()`

    con.log('ExtJS + App launch: OK')
}

function l10n_provider(msg){
var me, m, idx, tail

    me = (me = l10n._ns) ? l10n[me] : l10n

    idx = msg.indexOf(':')

    if(idx > 0){
        tail = msg.slice(idx + 1)
        msg = msg.slice(0, idx)

        return me && (m = me[msg]) ?
{'fraction_non_alphanumeric': 0.13610818405338954, 'fraction_numerical': 0.011064278187565859, 'mean_word_length': 2.1866256295467266, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3587461', 'n_tokens_mistral': 3779, 'n_tokens_neox': 3620, 'n_words': 864}