{"size": 1452, "ext": "js", "max_stars_repo_path": "girder_xtk_demo/web_client/main.js", "max_stars_repo_name": "manthey/girder-ctk-demo", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "girder_xtk_demo/web_client/main.js", "max_issues_repo_name": "manthey/girder-ctk-demo", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "girder_xtk_demo/web_client/main.js", "max_forks_repo_name": "manthey/girder-ctk-demo", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.5652173913, "max_line_length": 79, "alphanum_fraction": 0.5516528926}
import events from '@girder/core/events';
import { wrap } from '@girder/core/utilities/PluginUtils';
import ItemView from '@girder/core/views/body/ItemView';
import XtkView from './XtkView';

let _xtkLoaded = false;
/**
 * Ensures that the XTK javascript is loaded into the page context. When done,
 * triggers "g:xtkLoaded" event. This is a lazy loader -- after the javascript
 * has been loaded once, this will be a no-op but will still trigger the event.
 */
const ensureXtkLoaded = function () {
    if (_xtkLoaded) {
        events.trigger('g:xtkLoaded', X);
    } else {
        $.getScript('//get.goXTK.com/xtk_edge.js', function () {
            _xtkLoaded = true;
            events.trigger('g:xtkLoaded', X);
        });
    }
};


wrap(ItemView, 'initialize', function (initialize, settings) {
    initialize.call(this, settings);
    this.on('g:rendered', function () {
        var meta = this.model.get('meta') || {};

        if (_.has(meta, 'XTK')) {
            var el = $('<div>', {
                class: 'g-xtk-demo-container'
            }).prependTo(this.$('.g-item-info'));

            events.once('g:xtkLoaded', function () {
                new XtkView({
                    parentView: this,
                    item: this.model,
                    files: this.fileListWidget.collection,
                    el: el
                }).render();
            }, this);
            ensureXtkLoaded();
        }
    }, this);
});
