constructor()

in public/src/js/models/config/front.js [19:141]


    constructor(opts = {}) {
        super();

        this.id = ko.observable(opts.id);
        this.opts = opts;
        this.dom = null;

        this.props = asObservableProps([
            'navSection',
            'webTitle',
            'title',
            'description',
            'onPageDescription',
            'imageUrl',
            'imageWidth',
            'imageHeight',
            'isImageDisplayed',
            'isHidden',
            'priority',
            'group',
            'canonical']);

        populateObservables(this.props, opts);

        this.capiProps = asObservableProps([
            'section',
            'webTitle',
            'description']);

        this.state = asObservableProps([
            'collectionsCreated',
            'isOpen',
            'isOpenProps',
            'isValidMetadata']);
        this.state.isValidMetadata(true);

        this.applyConstraints();

        this.collections = new Group({
            parent: this,
            parentType: 'Front',
            items: ko.observableArray()
        });


        this.subscribeOn(this.props.priority, this.onChangePriority);
        this.subscribeOn(this.id, () => {
            this.validate(true);
            if (this.id()) {
                this.setOpen(true);
            }
        });
        this.subscribeOn(this.state.isOpen, () => {
            if (!this.state.collectionsCreated()) {
                this.state.collectionsCreated(true);
                this.collections.items(generateCollections(this.opts.collections));
            }
        });

        this.provisionalImageUrl = ko.observable();

        this.subscribeOn(this.props.imageUrl, src => this.provisionalImageUrl(src));
        this.provisionalImageUrl(this.props.imageUrl());

        this.subscribeOn(this.provisionalImageUrl, src => {
            if (!src) {
                this.props.imageUrl(undefined);
                this.props.imageWidth(undefined);
                this.props.imageHeight(undefined);
                this.props.isImageDisplayed(undefined);
            } else if (src !== this.props.imageUrl()) {
                validateImageSrc(src, {minWidth: 120})
                .then(img => {
                    this.props.imageUrl(img.src);
                    this.props.imageWidth(img.width);
                    this.props.imageHeight(img.height);
                    this.saveProps();
                }, err => {
                    this.provisionalImageUrl(undefined);
                    alert(err.message);
                });
            }
        });

        this.depopulateCollection = this._depopulateCollection.bind(this);

        this.placeholders = {};

        this.placeholders.navSection = ko.pureComputed(() => {
            var path = asPath(this.id()),
                isEditionalised = [].concat(vars.model.state().defaults.editions).some(edition => edition === path[0]);

            return this.capiProps.section() || (isEditionalised ? path.length === 1 ? undefined : path[1] : path[0]);
        });

        this.placeholders.webTitle = ko.pureComputed(() => {
            var path = asPath(this.id());

            return this.props.webTitle() || this.capiProps.webTitle() || (toTitleCase(path.slice(path.length > 1 ? 1 : 0).join(' ').replace(/-/g, ' ')) || this.id());
        });

        this.placeholders.title = ko.pureComputed(() => {
            var section = this.props.navSection() || this.placeholders.navSection();

            return this.props.title() || (this.placeholders.webTitle() + (section ? ' | ' + toTitleCase(section) : ''));
        });

        this.placeholders.description = ko.pureComputed(() => {
            return this.props.description() || this.capiProps.description() || ('Latest ' + this.placeholders.webTitle() + ' news, comment and analysis from the Guardian, the world\'s leading liberal voice');
        });

        this.placeholders.onPageDescription = ko.pureComputed(() => {
            return this.props.onPageDescription() || this.capiProps.description() || ('Latest ' + this.placeholders.webTitle() + ' news, comment and analysis from the Guardian, the world\'s leading liberal voice');
        });

        this.ophanPerformances = ko.pureComputed(() => {
            return vars.CONST.ophanFrontBase + encodeURIComponent('/' + this.id());
        });

        this.groups = ko.observableArray(vars.CONST.frontGroups);

        this.isNew = ko.observable(false);
    }