{"size": 2639, "ext": "js", "max_stars_repo_path": "src/StartingBlocks.js", "max_stars_repo_name": "rezozero/starting-blocks", "max_stars_repo_stars_event_min_datetime": "2017-01-10T14:34:35.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-27T08:52:43.000Z", "max_issues_repo_path": "src/StartingBlocks.js", "max_issues_repo_name": "roadiz/pageblock-framework", "max_issues_repo_issues_event_min_datetime": "2017-05-21T11:57:47.000Z", "max_issues_repo_issues_event_max_datetime": "2021-02-23T10:05:43.000Z", "max_forks_repo_path": "src/StartingBlocks.js", "max_forks_repo_name": "roadiz/pageblock-framework", "max_forks_repo_forks_event_min_datetime": "2018-03-01T16:14:03.000Z", "max_forks_repo_forks_event_max_datetime": "2020-12-05T12:33:58.000Z"}
{"max_stars_count": 71, "max_issues_count": 16, "max_forks_count": 8, "avg_line_length": 28.376344086, "max_line_length": 95, "alphanum_fraction": 0.6051534672}
/*
 * Copyright © 2017, Rezo Zero
 *
 * @file StartingBlocks.js
 * @author Adrien Scholaert <adrien@rezo-zero.com>
 */
import Bottle from 'bottlejs'
import PageBuilder from './services/PageBuilder'
import BlockBuilder from './services/BlockBuilder'
import Dom from './services/Dom'
import DefaultPage from './pages/DefaultPage'

/**
 * @namespace
 * @type {Object} defaults                      - Default config
 * @property {String} defaults.wrapperId        - Id of the main wrapper
 * @property {String} defaults.pageBlockClass
 * @property {String} defaults.pageClass        - Class name used to identify the containers
 * @property {String} defaults.objectTypeAttr   - The data attribute name to find the node type
 * @property {String} defaults.noAjaxLinkClass
 * @property {String} defaults.noPrefetchClass  - Class name used to ignore prefetch on links.
 * @property {boolean} defaults.manualDomAppend
 * @const
 * @default
 */
const CONFIG = {
    defaults: {
        wrapperId: 'sb-wrapper',
        pageBlockClass: 'page-block',
        pageClass: 'page-content',
        objectTypeAttr: 'data-node-type',
        noAjaxLinkClass: 'no-ajax-link',
        noPrefetchClass: 'no-prefetch',
        manualDomAppend: false,
        debug: 0
    }
}

export default class StartingBlocks {
    constructor (config = {}) {
        this.bottle = new Bottle()
        this.bootables = []

        this.bottle.value('Config', {
            ...CONFIG.defaults,
            ...config
        })

        window.startingBlocksDebugLevel = this.bottle.container.Config.debug

        this.provider('Dom', Dom)
        this.provider('BlockBuilder', BlockBuilder)
        this.instanceFactory('DefaultPage', c => {
            return new DefaultPage(c)
        })
    }

    provider (id, ClassName, ...args) {
        if (!id || !ClassName) {
            throw new Error('A parameter is missing')
        }

        this.bottle.provider(id, function () {
            this.$get = container => {
                return new ClassName(container, ...args)
            }
        })
    }

    factory (id, f) {
        this.bottle.factory(id, f)
    }

    instanceFactory (id, f) {
        this.bottle.instanceFactory(id, f)
    }

    bootableProvider (id, ClassName, ...args) {
        this.provider(id, ClassName, ...args)
        this.bootables.push(id)
    }

    boot () {
        this.bootableProvider('PageBuilder', PageBuilder)

        for (const serviceName of this.bootables) {
            if (this.bottle.container.hasOwnProperty(serviceName)) {
                this.bottle.container[serviceName].boot()
            }
        }
    }
}
