/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 David Heidrich, BowlingX <me@bowlingx.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
'use strict';

require('styles/main.scss');

import {Client, EVENT_MESSAGE} from 'Client';
import Marklib from 'marklib';
import Selector from 'Selector';

const ATTR_COMMENTP = 'data-commentp';

document.addEventListener('DOMContentLoaded', () => {

    const node = document.querySelector(`[${ATTR_COMMENTP}]`);
    if (node) {
        const channel = node.getAttribute(ATTR_COMMENTP);
        Client.connect(channel).then((client) => {

            const selectorApp = new Selector(node, global.document);


            client.on(EVENT_MESSAGE, (msg) => {
                // FIXME: Create a queue :)
                const renderer = new Marklib.Rendering(document, 'marking-remote', node);
                renderer.renderWithResult(msg);
            });
        });
    }


});

export default Client;

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 David Heidrich, BowlingX <me@bowlingx.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
'use strict';

require('styles/main.scss');

import {Client, EVENT_MESSAGE} from 'Client';
import Marklib from 'marklib';
import Selector from 'Selector';

const ATTR_COMMENTP = 'data-commentp';
const DOM_COMPLETE = 'complete';

function init() {
    const node = document.querySelector(`[${ATTR_COMMENTP}]`);
    if (node) {
        const channel = node.getAttribute(ATTR_COMMENTP);
        Client.connect(channel).then((client) => {
            const selectorApp = new Selector(node, global.document);
            client.on(EVENT_MESSAGE, (msg) => {
                // FIXME: Create a queue :)
                const renderer = new Marklib.Rendering(document, 'marking-remote', node);
                renderer.renderWithResult(msg);
            });
        });
    }
}

if (global.document.readyState === DOM_COMPLETE) {
    init();
} else {
    document.addEventListener('DOMContentLoaded', () => {
        init();
    });
}

export default Client;

Load Application as soon as DOM is completly loaded