{"size": 2678, "ext": "js", "max_stars_repo_path": "addon/routes/preload.js", "max_stars_repo_name": "PXLbros/ember-cli-asset-loader", "max_stars_repo_stars_event_min_datetime": "2016-05-21T03:04:50.000Z", "max_stars_repo_stars_event_max_datetime": "2016-06-01T18:45:19.000Z", "max_issues_repo_path": "addon/routes/preload.js", "max_issues_repo_name": "PXLbros/ember-cli-asset-loader", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "addon/routes/preload.js", "max_forks_repo_name": "PXLbros/ember-cli-asset-loader", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.1395348837, "max_line_length": 117, "alphanum_fraction": 0.5787901419}
import Ember from 'ember';

export default Ember.Route.extend({
    // Inject the asset loader service
    assetLoader: Ember.inject.service(),

    loadingRoute: Ember.computed(function() {
        return 'loading';
    }),

    assetCount: Ember.computed('assets', function() {
        let assetCount  = 0;
        let assets      = this.get('assets');

        // Create image promises
        if(assets.images) {
            assetCount += assets.images.length;
        }
        // Create video promises
        if(assets.videos) {
            assetCount += assets.videos.length;
        }
        // Create audio promises
        if(assets.audio)  {
            assetCount += assets.audio.length;
        }

        return assetCount;
    }),

    _attachLoadEvent: function() {
        let assetLoader         = this.get('assetLoader');

        assetLoader.on('assetLoaded', (/*assetName, asset*/) => {
            let loadingController   = this.controllerFor(this.get('loadingRoute'));
            loadingController.incrementProperty('loadedAssets');
            console.log(loadingController.get('loadedAssets')/this.get('assetCount'));
            loadingController.set('percentLoaded', loadingController.get('loadedAssets')/this.get('assetCount')*100);
        });
    }.on('init'),

    // Load defined assets
    beforeModel() {
        let assets = this.get('assets');
        if(assets) { return this._loadAssets(assets); }
    },

    afterModel() {
        let assets = this.get('afterModelAssets');
        if(assets) { return this._loadAssets(assets); }
    },

    _loadAssets(assets) {
        let assetLoader = this.get('assetLoader');
        // Array for holding promises
        let promises    = [];
        let totalAssets = 0;

        // Create image promises
        if(assets.images) {
            totalAssets += assets.images.length;
            assets.images.forEach(image => promises.push(assetLoader.loadImage(image)));
        }
        // Create video promises
        if(assets.videos) {
            totalAssets += assets.videos.length;
            assets.videos.forEach(video => promises.push(assetLoader.loadVideo(video)));
        }
        // Create audio promises
        if(assets.audio)  {
            totalAssets += assets.audio.length;
            assets.audio.forEach(audio  => promises.push(assetLoader.loadAudio(audio)));
        }
        // Create font promises
        if(assets.fonts)  {
            promises.push(assetLoader.loadFonts(assets.fonts));
        }

        promises.push(new Ember.RSVP.Promise(res => {
            window.setTimeout(res, 4000);
        }));

        return Ember.RSVP.all(promises);
    }
});
