"use strict";

/**
@module UI snippets
*/

requirejs('controls/control');
requirejs('controls/embeddedWindow');

/**
WebBrowser - control for web browser

@class WebBrowser
@constructor
@extends Control
*/

class WebBrowser extends Control {

    initialize(elem, params) {
        super.initialize(elem, params);

        this.panel = document.createElement('div');
        this.panel.classList.add('fill');
        this.panel.classList.add('flex');
        this.panel.classList.add('column');

        this.edit = document.createElement('input');
        this.edit.classList.add('fullWidth');
        this.panel.appendChild(this.edit);

        this.browser = document.createElement('div');
        this.browser.setAttribute('data-control-class', 'EmbeddedWindow');
        this.browser.setAttribute('data-init-params', '{type: "browser"}');
        this.panel.appendChild(this.browser);

        this.container.appendChild(this.panel);
        
        initializeControls(this.panel);
        
        this.keyUpHandler = function(e) {
            switch(friendlyKeyName(e)) {
                case 'Enter': 
                    this.browser.controlClass.url = this.edit.value;
                    break;
                    
            }
        }.bind(this);
        
        app.listen(this.edit, 'keyup', this.keyUpHandler);
    }

    cleanUp() {
        super.cleanUp();
        app.unlisten(this.edit, 'keyup', this.keyUpHandler);
    }
    
    get url () {
        return this.browser.controlClass.url;
    }
    set url (value) {
        this.browser.controlClass.url = value;
        this.edit.value = value;
    }
    
}
registerClass(WebBrowser);