<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.

var BaseObject = require('../../base/base_object');
var $ = require("jquery");
var _ = require('underscore');

var PipPlugin = BaseObject.extend({
  initialize: function(core) {
    this.core = core;
    this.pipStyle = {width: "24%", height: "24%", "z-index": 20, bottom: "7px", right: "7px",
                     "border-width": "3px", "border-style": "solid", "border-color": "rgba(255,255,255, .3)",
                     "background-clip": "padding-box", "-webkit-background-clip": "padding-box", "cursor": "pointer"};
    this.masterStyle = {width: "100%", height: "100%", bottom: "0px", right: "0px", border: "none", "cursor" : "default"};
    this.masterContainer = core.containers[0];
    if (core.containers.length === 2) {
      this.pipContainer = core.containers[1];
      this.pipContainer.setStyle(this.pipStyle);
      if (!this.pipContainer.isReady) {
        this.pipContainer.on('container:ready', function() {
          this.pipContainer.play();
          this.pipContainer.setVolume(0);
        }.bind(this));
      } else {
        this.pipContainer.play();
        this.pipContainer.setVolume(0);
      }
      this.listenToPipClick();
      this.core.mediaControl.setContainer(this.masterContainer);
      this.core.mediaControl.render();
    }
    this.listenTo(this.core.mediaControl, 'mediacontrol:show', this.onMediaControlShow);
    this.listenTo(this.core.mediaControl, 'mediacontrol:hide', this.onMediaControlHide);
  },
  getExternalInterface: function() {
    return {
      addPip: this.addPip,
      discardPip: this.discardPip,
      addMaster: this.addMaster,
      addMasterContainer: this.addMasterContainer,
      pipToMaster: this.pipToMaster
    };
  },
  addPip: function(source) {
    this.discardPip();
    this.pipContainer = this.core.playbackHandler.createContainer(source, this.addPipCallback.bind(this));
    if (this.pipContainer.hasPlugin('hls_playback')) {
      // html5 players dispatches on:ready instantly, causing this style to be set after addPipCallback.
      // For flash players, we need to put it behind everything until flash dispatch on:ready.
      this.pipContainer.setStyle({'z-index': -1});
    }
    this.core.$el.append(this.pipContainer.render().el);
  },
  addPipCallback: function(container) {
    this.pipContainer = container;
    this.pipContainer.on('container:ready', function() {
      this.pipContainer.setVolume(0);
      this.pipContainer.play();
      this.pipContainer.setStyle(this.pipStyle);
      this.core.containers[1] = this.pipContainer;
      this.stopListening(this.pipContainer);
      this.listenToPipClick();
      this.pipContainer.trigger("container:pip", true);
    }.bind(this));
  },
  discardPip: function() {
    if (this.pipContainer) {
      this.stopListening(this.pipContainer);
      this.discardContainer(this.pipContainer);
      this.pipContainer = undefined;
    }
  },
  discardMaster: function() {
    if (this.masterContainer) {
      this.discardContainer(this.masterContainer);
      this.masterContainer = undefined;
    }
  },
  addMaster: function(source) {
    if (this.masterContainer) {
      this.tmpContainer = this.masterContainer;
      this.tmpContainer.setStyle({'z-index': 20});
      this.core.playbackHandler.createContainer(source, this.addMasterCallback.bind(this));
    }
  },
  addMasterContainer: function(container) {
    if (this.masterContainer) {
      this.tmpContainer = this.masterContainer;
      this.tmpContainer.setStyle({'z-index': 20});
      this.addMasterCallback(container);
    }
  },
  addMasterCallback: function(container) {
    this.masterContainer = container;
    this.masterContainer.on('container:ready', function() {
      this.discardPip();
      this.masterContainer.play();
      this.pipContainer = this.tmpContainer;
      this.tmpContainer = undefined;
      this.pipContainer.setVolume(0);
      this.pipContainer.trigger("container:pip", true);
      if (this.pipContainer.hasPlugin('hls_playback')) { //flash breaks on animate
        this.pipContainer.setStyle(this.pipStyle);
      } else {
        this.pipContainer.animate(this.pipStyle, {complete: function() { this.pipContainer.setStyle(this.pipStyle); }.bind(this)});
      }
      this.core.mediaControl.setContainer(this.masterContainer);
      this.core.mediaControl.render();
      this.listenToPipClick();
    }.bind(this));
    this.core.$el.append(this.masterContainer.render().el);
    this.core.containers.splice(0, 0, this.masterContainer);
    this.listenToPipClick();
  },
  listenToPipClick: function() {
    if (this.pipContainer) {
      this.stopListening(this.pipContainer);
      this.listenTo(this.pipContainer, "container:click", this.pipToMaster.bind(this));
    }
  },
  discardContainer: function(container) {
    container.destroy();
    this.core.containers = _.without(this.core.containers, _.findWhere(this.core.containers, container));
  },
  pipToMaster: function() {
    if (this.pipContainer) {
      this.pipContainer.animate(this.masterStyle, {complete: this.pipToMasterCallback.bind(this)});
    }
  },
  pipToMasterCallback: function() {
    this.discardMaster();
    this.pipContainer.setVolume(100);
    this.pipContainer.trigger("container:pip", false);
    this.masterContainer = this.pipContainer;
    this.masterContainer.setStyle({"z-index": 1});
    this.pipContainer = undefined;
    this.core.mediaControl.setContainer(this.masterContainer);
    this.core.mediaControl.render();
  },
  onMediaControlShow: function () {
    if (!this.pipContainer || this.pipContainer.$el.is(':animated')) return;
    this.pipContainer.animate({ bottom: 47 }, 400);
  },<commit_msg>update pip to make a better use of core's features<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.

var BaseObject = require('../../base/base_object');
var $ = require("jquery");
var _ = require('underscore');

var PipPlugin = BaseObject.extend({
  initialize: function(core) {
    this.core = core;
    this.pipStyle = {width: "24%", height: "24%", "z-index": 20, bottom: "7px", right: "7px",
                     "border-width": "3px", "border-style": "solid", "border-color": "rgba(255,255,255, .3)",
                     "background-clip": "padding-box", "-webkit-background-clip": "padding-box", "cursor": "pointer"};
    this.masterStyle = {width: "100%", height: "100%", bottom: "0px", right: "0px", border: "none", "cursor" : "default"};
    this.masterContainer = core.containers[0];
    if (core.containers.length === 2) {
      this.pipContainer = core.containers[1];
      this.pipContainer.setStyle(this.pipStyle);
      this.pipContainer.play();
      this.pipContainer.setVolume(0);
      this.listenToPipClick();
      this.core.mediaControl.setContainer(this.masterContainer);
      this.core.mediaControl.render();
    }
    this.listenTo(this.core.mediaControl, 'mediacontrol:show', this.onMediaControlShow);
    this.listenTo(this.core.mediaControl, 'mediacontrol:hide', this.onMediaControlHide);
  },
  getExternalInterface: function() {
    return {
      addPip: this.addPip,
      discardPip: this.discardPip,
      addMaster: this.addMaster,
      addMasterContainer: this.addMasterContainer,
      pipToMaster: this.pipToMaster,
      hasPip: this.hasPip
    };
  },
  hasPip: function() {
    return !!this.pipContainer;
  },
  addPip: function(source) {
    this.discardPip();
    this.core.createContainer(source).then(this.addPipCallback.bind(this));
  },
  addPipCallback: function(container) {
    this.pipContainer = _(container).isArray() ? container[0] : container;
    if (this.pipContainer.hasPlugin('hls_playback')) {
      // html5 players dispatches on:ready instantly, causing this style to be set after addPipCallback.
      // For flash players, we need to put it behind everything until flash dispatch on:ready.
      this.pipContainer.setStyle({'z-index': -1});
    }
    this.onContainerReady();
  },
  onContainerReady: function() {
    this.pipContainer.setVolume(0);
    this.pipContainer.play();
    this.pipContainer.setStyle(this.pipStyle);
    this.stopListening(this.pipContainer);
    this.listenToPipClick();
  },
  discardPip: function() {
    if (this.pipContainer) {
      this.stopListening(this.pipContainer);
      this.discardContainer(this.pipContainer);
      this.pipContainer = undefined;
    }
  },
  discardMaster: function() {
    if (this.masterContainer) {
      this.discardContainer(this.masterContainer);
      this.masterContainer = undefined;
    }
  },
  addMaster: function(source) {
    if (this.masterContainer) {
      this.tmpContainer = this.masterContainer;
      this.tmpContainer.setStyle({'z-index': 2000});
      this.core.createContainer(source).then(this.addMasterCallback.bind(this));
    }
  },
  addMasterContainer: function(container) {
    if (this.masterContainer) {
      this.tmpContainer = this.masterContainer;
      this.tmpContainer.setStyle({'z-index': 2000});
      this.addMasterCallback(container);
    }
  },
  addMasterCallback: function(container) {
    this.masterContainer = container;
    if(this.pipContainer) {
      this.discardPip();
    }
    this.masterContainer.play();
    this.pipContainer = this.tmpContainer;
    this.tmpContainer = undefined;
    this.pipContainer.setVolume(0);
    this.pipContainer.trigger("container:pip", true);
    if (this.pipContainer.hasPlugin('hls_playback')) { //flash breaks on animate
      this.pipContainer.setStyle(this.pipStyle);
    } else {
      this.pipContainer.animate(this.pipStyle, {
        complete: function() {
          if(this.pipContainer) {
            this.pipContainer.setStyle(this.pipStyle);
          }
        }.bind(this)
      });
    }
    this.core.mediaControl.setContainer(this.masterContainer);
    this.core.mediaControl.render();
    this.listenToPipClick();
  },
  listenToPipClick: function() {
    if (this.pipContainer) {
      this.stopListening(this.pipContainer);
      this.listenTo(this.pipContainer, "container:click", this.pipToMaster.bind(this));
    }
  },
  discardContainer: function(container) {
    container.destroy();
  },
  pipToMaster: function() {
    if (this.pipContainer) {
      this.pipContainer.animate(this.masterStyle, {complete: this.pipToMasterCallback.bind(this)});
    }
    return this;
  },
  pipToMasterCallback: function() {
    this.discardMaster();
    this.pipContainer.setVolume(100);
    this.pipContainer.trigger("container:pip", false);
    this.pipContainer.trigger('container:play');
    this.masterContainer = this.pipContainer;
    this.masterContainer.setStyle({"z-index": 20});
    this.pipContainer = undefined;
    this.core.mediaControl.setContainer(this.masterContainer);
    this.core.mediaControl.render();
  },
  onMediaControlShow: function () {
    if (!this.pipContainer || this.pipContainer.$el.is(':animated')) return;
    this.pipContainer.animate({ bottom: 47 }, 400);
  },
{'fraction_non_alphanumeric': 0.14428750223493653, 'fraction_numerical': 0.010191310566779903, 'mean_word_length': 3.549410329402196, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 3, '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': '3270492', 'n_tokens_mistral': 3592, 'n_tokens_neox': 3387, 'n_words': 574}