/**
 * @license
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

CLASS({
  package: 'foam.ui',
  name: 'WebView',
  extendsModel: 'View',
  imports: [
    'window',
  ],
  help: 'A view that renders the HTML given to it.',
  properties: [
    {
      name: 'data',
    },
    {
      name: 'intervalId',
    },
  ],
  methods: {
    toHTML: function() {
      var id = this.id;
      return '<iframe id="' + id + '"></iframe>';
    },
    initHTML: function() {
      this.SUPER();

      var iframe = document.getElementById(this.id);
      iframe.contentDocument.write(this.data);

      // This is a lazy way of handling the change in size when things like
      // images load asynchronously.
      this.intervalId = this.window.setInterval(function() {
        iframe.style.height = iframe.contentDocument.body.scrollHeight;
      }, 500);

      this.window.addEventListener('resize', this.onResize);
    },
    destroy: function() {
      this.window.clearInterval(this.intervalId);
      this.window.removeEventListener('resize', this.onResize);
    },
  },
  listeners: [
    {
      name: 'onResize',
      isMerged: 500,
      code: function() {
        var iframe = document.getElementById(this.id);
        // If the height of the content gets smaller on resize (e.g. because the
        // window got wider) then that won't be reflected in the scrollHeight so
        // set the size to 0 first and then use the scrollHeight.
        iframe.style.height = 0;
        iframe.style.height = iframe.contentDocument.body.scrollHeight;
      }
    }
  ]
});

/**
 * @license
 * Copyright 2015 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

CLASS({
  package: 'foam.ui',
  name: 'WebView',
  extendsModel: 'View',
  imports: [
    'window',
  ],
  help: 'A view that renders the HTML given to it.',
  properties: [
    {
      name: 'data',
    },
    {
      name: 'intervalId',
    },
  ],
  methods: {
    toHTML: function() {
      var id = this.id;
      return '<iframe id="' + id + '"></iframe>';
    },
    initHTML: function() {
      this.SUPER();

      var iframe = this.$;
      iframe.contentDocument.write(this.data);

      // This is a lazy way of handling the change in size when things like
      // images load asynchronously.
      this.intervalId = this.window.setInterval(function() {
        iframe.style.height = iframe.contentDocument.body.scrollHeight;
      }, 500);

      this.window.addEventListener('resize', this.onResize);
    },
    destroy: function() {
      this.window.clearInterval(this.intervalId);
      this.window.removeEventListener('resize', this.onResize);
    },
  },
  listeners: [
    {
      name: 'onResize',
      isMerged: 500,
      code: function() {
        // If the height of the content gets smaller on resize (e.g. because the
        // window got wider) then that won't be reflected in the scrollHeight so
        // set the size to 0 first and then use the scrollHeight.
        this.$.style.height = 0;
        this.$.style.height = this.$.contentDocument.body.scrollHeight;
      }
    }
  ]
});

Use this.$ instead of document.getElementById