<commit_before>// Copyright 2014 Globo.com Player authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/**
 * The Core is responsible to manage Containers, the mediator, MediaControl
 * and the player state.
 */

var _ = require('underscore')
var $ = require('jquery')

var UIObject = require('../../base/ui_object')
var ContainerFactory = require('../container_factory')
var Fullscreen = require('../../base/utils').Fullscreen
var Styler = require('../../base/styler')
var MediaControl = require('../media_control')

class Core extends UIObject {
  get events() {
    return {
      'webkitfullscreenchange': 'exit',
      'mousemove': 'showMediaControl',
      'mouseleave': 'hideMediaControl'
    }
  }

  get attributes() {
    return {
      'data-player': ''
    }
  }

  constructor(options) {
    super(options)
    this.defer = $.Deferred()
    this.defer.promise(this)
    this.plugins = []
    this.containers = []
    this.options = options 
    this.options.displayType || (this.options.displayType = 'pip')
    this.parentElement = options.parentElement
    this.loader = this.options.loader
    this.containerFactory = new ContainerFactory(options, this.loader)
    this.containerFactory
      .createContainers()
      .then((containers) => this.setupContainers(containers))
      .then((containers) => this.resolveOnContainersReady(containers))
    this.updateSize()
    //FIXME fullscreen api sucks
    document.addEventListener('mozfullscreenchange', () => this.exit())
    $(window).resize(() => this.updateSize())
  }

  updateSize() {
   if (Fullscreen.isFullscreen()) {
      this.$el.addClass('fullscreen')
      this.$el.removeAttr('style')
    } else {
      var width = 0
      var height = 0
      if (this.options.stretchWidth && this.options.stretchHeight && this.options.stretchWidth <= window.innerWidth && this.options.stretchHeight <= (window.innerHeight * 0.73)) {
        width = this.options.stretchWidth
        height = this.options.stretchHeight
      } else {
        width = this.options.width || width
        height = this.options.height || height
      }
      if (width > 0) {
        this.$el.css({ width: width })
      }
      if (height > 0) {
        this.$el.css({ height: height })
      }
      this.$el.removeClass('fullscreen')
    }
  }

  resolveOnContainersReady(containers) {
    $.when.apply($, containers).done(() =>this.defer.resolve(this))
  }

  addPlugin(plugin) {
    this.plugins.push(plugin)
  }

  hasPlugin(name) {
    return !!this.getPlugin(name)
  }

  getPlugin(name) {
    return _(this.plugins).find((plugin) => plugin.name === name)
  }

  load(sources) {
    sources = _.isString(sources) ? [sources]: sources;
    _(this.containers).each((container) => container.destroy())
    this.containerFactory.options = _(this.options).extend({sources})
    this.containerFactory.createContainers().then((containers) => this.setupContainers(containers))
  }

  destroy() {
    _(this.containers).each((container) => container.destroy())
    this.$el.remove()
  }

  exit() {
    this.updateSize()
    this.mediaControl.show()
  }

  setMediaControlContainer(container) {
    this.mediaControl.setContainer(container)
    this.mediaControl.render()
  }

  disableMediaControl() {
    this.mediaControl.disable()
    this.$el.removeClass('nocursor')
  }

  enableMediaControl() {
    this.mediaControl.enable()
  }

  removeContainer(container) {
    console.log('container being removed')
    this.stopListening(container)
    this.containers = _.without(this.containers, container)
  }

  appendContainer(container) {
    this.listenTo(container, 'container:destroyed', this.removeContainer)
    this.el.appendChild(container.render().el)
    this.containers.push(container)
  }

  prependContainer(container) {
    this.listenTo(container, 'container:destroyed', this.removeContainer)
    this.$el.append(container.render().el)
    this.containers.unshift(container)
  }

  setupContainers(containers) {
    _.map(containers, this.appendContainer, this)
    this.setupMediaControl(this.getCurrentContainer())
    this.render()
    this.$el.appendTo(this.parentElement)
    return containers
  }

  createContainer(source) {
    var container = this.containerFactory.createContainer(source)
    this.appendContainer(container)
    return container
  }

  setupMediaControl(container) {
    if (this.mediaControl) {
      this.mediaControl.setContainer(container)
    } else {
      this.mediaControl = new MediaControl(_.extend({container: container}, this.options))
      this.listenTo(this.mediaControl, 'mediacontrol:fullscreen', this.toggleFullscreen)
      this.listenTo(this.mediaControl, 'mediacontrol:show', this.onMediaControlShow.bind(this, true))
      this.listenTo(this.mediaControl, 'mediacontrol:hide', this.onMediaControlShow.bind(this, false))
    }
  }

  getCurrentContainer() {
    return this.containers[0]
  }

  toggleFullscreen() {
    if (!Fullscreen.isFullscreen()) {
      Fullscreen.requestFullscreen(this.el)
      this.$el.addClass('fullscreen')
    } else {
      Fullscreen.cancelFullscreen()
      this.$el.removeClass('fullscreen nocursor')
    }
    this.mediaControl.show()
  }
<commit_msg>core: add support for an external media control, closes #119<commit_after>// Copyright 2014 Globo.com Player authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/**
 * The Core is responsible to manage Containers, the mediator, MediaControl
 * and the player state.
 */

var _ = require('underscore')
var $ = require('jquery')

var UIObject = require('../../base/ui_object')
var ContainerFactory = require('../container_factory')
var Fullscreen = require('../../base/utils').Fullscreen
var Styler = require('../../base/styler')
var MediaControl = require('../media_control')

class Core extends UIObject {
  get events() {
    return {
      'webkitfullscreenchange': 'exit',
      'mousemove': 'showMediaControl',
      'mouseleave': 'hideMediaControl'
    }
  }

  get attributes() {
    return {
      'data-player': ''
    }
  }

  constructor(options) {
    super(options)
    this.defer = $.Deferred()
    this.defer.promise(this)
    this.plugins = []
    this.containers = []
    this.options = options 
    this.options.displayType || (this.options.displayType = 'pip')
    this.parentElement = options.parentElement
    this.loader = this.options.loader
    this.containerFactory = new ContainerFactory(options, this.loader)
    this.containerFactory
      .createContainers()
      .then((containers) => this.setupContainers(containers))
      .then((containers) => this.resolveOnContainersReady(containers))
    this.updateSize()
    //FIXME fullscreen api sucks
    document.addEventListener('mozfullscreenchange', () => this.exit())
    $(window).resize(() => this.updateSize())
  }

  updateSize() {
   if (Fullscreen.isFullscreen()) {
      this.$el.addClass('fullscreen')
      this.$el.removeAttr('style')
    } else {
      var width = 0
      var height = 0
      if (this.options.stretchWidth && this.options.stretchHeight && this.options.stretchWidth <= window.innerWidth && this.options.stretchHeight <= (window.innerHeight * 0.73)) {
        width = this.options.stretchWidth
        height = this.options.stretchHeight
      } else {
        width = this.options.width || width
        height = this.options.height || height
      }
      if (width > 0) {
        this.$el.css({ width: width })
      }
      if (height > 0) {
        this.$el.css({ height: height })
      }
      this.$el.removeClass('fullscreen')
    }
  }

  resolveOnContainersReady(containers) {
    $.when.apply($, containers).done(() =>this.defer.resolve(this))
  }

  addPlugin(plugin) {
    this.plugins.push(plugin)
  }

  hasPlugin(name) {
    return !!this.getPlugin(name)
  }

  getPlugin(name) {
    return _(this.plugins).find((plugin) => plugin.name === name)
  }

  load(sources) {
    sources = _.isString(sources) ? [sources]: sources;
    _(this.containers).each((container) => container.destroy())
    this.containerFactory.options = _(this.options).extend({sources})
    this.containerFactory.createContainers().then((containers) => this.setupContainers(containers))
  }

  destroy() {
    _(this.containers).each((container) => container.destroy())
    this.$el.remove()
  }

  exit() {
    this.updateSize()
    this.mediaControl.show()
  }

  setMediaControlContainer(container) {
    this.mediaControl.setContainer(container)
    this.mediaControl.render()
  }

  disableMediaControl() {
    this.mediaControl.disable()
    this.$el.removeClass('nocursor')
  }

  enableMediaControl() {
    this.mediaControl.enable()
  }

  removeContainer(container) {
    console.log('container being removed')
    this.stopListening(container)
    this.containers = _.without(this.containers, container)
  }

  appendContainer(container) {
    this.listenTo(container, 'container:destroyed', this.removeContainer)
    this.el.appendChild(container.render().el)
    this.containers.push(container)
  }

  prependContainer(container) {
    this.listenTo(container, 'container:destroyed', this.removeContainer)
    this.$el.append(container.render().el)
    this.containers.unshift(container)
  }

  setupContainers(containers) {
    _.map(containers, this.appendContainer, this)
    this.setupMediaControl(this.getCurrentContainer())
    this.render()
    this.$el.appendTo(this.parentElement)
    return containers
  }

  createContainer(source) {
    var container = this.containerFactory.createContainer(source)
    this.appendContainer(container)
    return container
  }

  setupMediaControl(container) {
    if (this.mediaControl) {
      this.mediaControl.setContainer(container)
    } else {
      this.mediaControl = this.createMediaControl(_.extend({container: container}, this.options))
      this.listenTo(this.mediaControl, 'mediacontrol:fullscreen', this.toggleFullscreen)
      this.listenTo(this.mediaControl, 'mediacontrol:show', this.onMediaControlShow.bind(this, true))
      this.listenTo(this.mediaControl, 'mediacontrol:hide', this.onMediaControlShow.bind(this, false))
    }
  }

  createMediaControl(options) {
    if(this.options.mediacontrol && this.options.mediacontrol.external) {
      return new this.options.mediacontrol.external(options);
    } else {
      return new MediaControl(options);
    }
  }

  getCurrentContainer() {
    return this.containers[0]
  }

  toggleFullscreen() {
    if (!Fullscreen.isFullscreen()) {
      Fullscreen.requestFullscreen(this.el)
      this.$el.addClass('fullscreen')
    } else {
      Fullscreen.cancelFullscreen()
      this.$el.removeClass('fullscreen nocursor')
    }
    this.mediaControl.show()
  }

{'fraction_non_alphanumeric': 0.13761298653384985, 'fraction_numerical': 0.0024903154399557276, 'mean_word_length': 3.43657937806874, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7560215', 'n_tokens_mistral': 3262, 'n_tokens_neox': 3180, 'n_words': 552}