<commit_before>/**
 *  Video player
 *
 */
cdb.admin.VideoPlayer = cdb.core.View.extend({

  className: 'VideoPlayer',

  defaults: {
    minimized: true,
    size: {
      minimized: {
        width: 420,
        height: 236
      },
      maximized: {
        width : 700,
        height: 393
      }
    },
  },

  events: {
    "dblclick": "_toggle",
    "click .js-toggle": "_toggle",
    "click .js-close": "close",
    "mouseenter": "_mouseEnter",
    "mouseleave": "_mouseLeave"
  },

  initialize: function(id) {

    _.bindAll(this, "_onInitDraggable", "_onStopDragging");

    this.template = cdb.templates.getTemplate('new_dashboard/views/video_player');

    this._initLocalStorage();
    this._initModel();
  },

  render: function() {

    var self = this;

    this.clearSubViews();

    if (this.videoData && this.videoData.id) {

      this._onChangeMinimized();

      this.$el.html(
        this.template({
        id: this.videoData.id
      }));

      this.$el.draggable({
        stop: self._onStopDragging,
        create: self._onInitDraggable
      });

      this.video = this.$el.find("iframe");
      this._loadScript();
    }

    return this;

  },

  _initBinds: function() {
    this.model.bind('change:minimized', this._onChangeMinimized, this);
  },

  _initLocalStorage: function() {

    this.localStorage = new cdb.admin.localStorage("VideoPlayer");

    this.videoData = this.localStorage.get("currentVideo") || {};

    if (this.options.id) {
      this._storeVideoID(this.options.id);
      this.videoData = this.localStorage.get("currentVideo");
    }

  },

  _initModel: function() {

    var minimized = this.defaults.minimized;

    if (this.videoData && this.videoData.minimized !== undefined) {
      minimized = this.videoData.minimized;
    } 

    this.model = new cdb.core.Model({
      minimized: minimized,
      width:  this.defaults.size[minimized ? "minimized" : "maximized"].width,
      height: this.defaults.size[minimized ? "minimized" : "maximized"].height,
      left: this.videoData ? this.videoData.left : 20,
      bottom: this.videoData ? this.videoData.bottom : 20
    });

    this._initBinds();
  },

  _loadScript: function() {
    var self = this;
    $.getScript('//f.vimeocdn.com/js/froogaloop2.min.js', function() {
      self._initVideoBinds();
    });
  },

  _initVideoBinds: function() {
    var self = this;

    this.player = $f(this.video[0]);

    this.player.addEvent('ready', function() {

      self._seekToStoredPosition();

      self.player.addEvent('pause', function(m) {
        self._storePlayStatus("stop");
      });

      self.player.addEvent('finish', function(m) {
        self._storePlayStatus("stop");
        self.close(false, { dontHide: true });
      });

      self.player.addEvent('play', function(m) {
        self._storePlayStatus("play");
      });

      self.player.addEvent('playProgress', function(m) {
        self._storeVideoInfo(m.seconds);
      });

    });
  },

  _removeVideoBinds: function() {

    if (!this.player) return;

    this.player.removeEvent('ready');
    //this.player.removeEvent('play');
    //this.player.removeEvent('finish');
    //this.player.removeEvent('pause');
    //this.player.removeEvent('playProgress');
  },


  _onInitDraggable: function(e, ui) {

    var left         = this.model.get("left");
    var elWidth      = this.model.get("width");
    var windowWidth  = $(window).width();

    if ((left + elWidth) > windowWidth) {
      left = windowWidth - elWidth - 20;
    }

    this.$el.css({
      position: "fixed",
      left: left,
      bottom: this.model.get("bottom")
    });

  },

  _onStopDragging: function(e, ui) {

    var windowHeight = $(window).height();
    var top          = ui.position.top;
    var left         = ui.position.left;
    var bottom       = windowHeight - (top + this.$el.outerHeight(true));

    this._storeVideoPosition(left, bottom);
  },

  _mouseEnter: function() {
    this.$el.find(".VideoControls").fadeIn(150);
  },

  _mouseLeave: function() {
    this.$el.find(".VideoControls").fadeOut(150);
  },

  hasVideoData: function() {
    if (this.videoData && this.videoData.id) {
      return true;
    } else {
      return false;
    }
  },

  _saveVideoData: function() {
    this.localStorage.set({ currentVideo: this.videoData });
  },

  _storeVideoID: function(id) {
    this.videoData.id = id;
    this._saveVideoData();
  },

  _storeVideoInfo: function(seconds) {
    this.videoData.seconds = seconds;
    this._saveVideoData();
  },

  _clearStoredData: function(status) {
    this.localStorage.set({ currentVideo: null });
  },

  _storeMinimizedState: function(minimized) {
    this.videoData.minimized = minimized;
    this._saveVideoData();
  },

  _storeVideoPosition: function(left, bottom) {
    this.videoData.left   = left;
    this.videoData.bottom = bottom;
    this._saveVideoData();
  },

  _storePlayStatus: function(status) {
    this.videoData.status = status;
    this._saveVideoData();
  },

  _seekToStoredPosition: function() {

    var self = this;

    if (this.videoData.seconds) {
      this.player.api('seekTo', this.videoData.seconds);
    }

    if (this.videoData.status === "stop") {

      setTimeout(function() {
        self.player.api('pause');
      }, 100);

    }

  },

  close: function(e, opts) {

    e && e.preventDefault();
    e && e.stopPropagation();

    var self = this;

    if (opts && opts.dontHide) {
      self._clearStoredData();
    } else {
      this.$el.animate({ width: 0, height: 0, opacity: 0 }, { easing: "easeInQuad", duration: 200, complete: function() {
        self._clearStoredData();
        self._removeVideoBinds();
        self.remove();
      }});
    }

  },

  _toggle: function(e) {

    e.preventDefault();
    e.stopPropagation();

    this.model.set("minimized", !!!this.model.get("minimized"));
  },

  _onChangeMinimized: function() {

    var self = this;

    var windowWidth    = $(window).width();
    var windowHeight   = $(window).height();
    var documentHeight = $(document).height();

    var elTop  = this.$el.offset().top;
    var elLeft = this.$el.position().left;

    function calcHorizontalPos(w) {
      if ((elLeft + w) > windowWidth) {
        var right = windowWidth - elLeft - self.$el.outerWidth(true);<commit_msg>cleanup code<commit_after>/**
 *  Video player
 *
 */
cdb.admin.VideoPlayer = cdb.core.View.extend({

  className: 'VideoPlayer',

  defaults: {
    minimized: true,
    size: {
      minimized: {
        width: 420,
        height: 236
      },
      maximized: {
        width : 700,
        height: 393
      }
    },
  },

  events: {
    "dblclick": "_toggle",
    "click .js-toggle": "_toggle",
    "click .js-close": "close",
    "mouseenter": "_mouseEnter",
    "mouseleave": "_mouseLeave"
  },

  initialize: function(id) {

    _.bindAll(this, "_onInitDraggable", "_onStopDragging", "_onCloseAnimationFinished");

    this.template = cdb.templates.getTemplate('new_dashboard/views/video_player');

    this._initLocalStorage();
    this._initModel();
  },

  render: function() {

    var self = this;

    this.clearSubViews();

    if (this.videoData && this.videoData.id) {

      this._onChangeMinimized();

      this.$el.html(
        this.template({
        id: this.videoData.id
      }));

      this.$el.draggable({
        stop: self._onStopDragging,
        create: self._onInitDraggable
      });

      this.video = this.$el.find("iframe");
      this._loadScript();
    }

    return this;

  },

  _initBinds: function() {
    this.model.bind('change:minimized', this._onChangeMinimized, this);
  },

  _initLocalStorage: function() {

    this.localStorage = new cdb.admin.localStorage("VideoPlayer");

    this.videoData = this.localStorage.get("currentVideo") || {};

    if (this.options.id) {
      this._storeVideoID(this.options.id);
      this.videoData = this.localStorage.get("currentVideo");
    }

  },

  _initModel: function() {

    var minimized = this.defaults.minimized;

    if (this.videoData && this.videoData.minimized !== undefined) {
      minimized = this.videoData.minimized;
    }

    this.model = new cdb.core.Model({
      minimized: minimized,
      width:  this.defaults.size[minimized ? "minimized" : "maximized"].width,
      height: this.defaults.size[minimized ? "minimized" : "maximized"].height,
      left: this.videoData ? this.videoData.left : 20,
      bottom: this.videoData ? this.videoData.bottom : 20
    });

    this._initBinds();
  },

  _loadScript: function() {
    var self = this;
    $.getScript('//f.vimeocdn.com/js/froogaloop2.min.js', function() {
      self._initVideoBinds();
    });
  },

  _initVideoBinds: function() {
    var self = this;

    this.player = $f(this.video[0]);

    this.player.addEvent('ready', function() {

      self._seekToStoredPosition();

      self.player.addEvent('pause', function(m) {
        self._storePlayStatus("stop");
      });

      self.player.addEvent('finish', function(m) {
        self._storePlayStatus("stop");
        self.close(false, { dontHide: true });
      });

      self.player.addEvent('play', function(m) {
        self._storePlayStatus("play");
      });

      self.player.addEvent('playProgress', function(m) {
        self._storeVideoInfo(m.seconds);
      });

    });
  },

  _removeVideoBinds: function() {
    if (this.player) {
      this.player.removeEvent('ready');
    }
  },

  _onInitDraggable: function(e, ui) {

    var left         = this.model.get("left");
    var elWidth      = this.model.get("width");
    var windowWidth  = $(window).width();

    if ((left + elWidth) > windowWidth) {
      left = windowWidth - elWidth - 20;
    }

    this.$el.css({
      position: "fixed",
      left: left,
      bottom: this.model.get("bottom")
    });

  },

  _onStopDragging: function(e, ui) {

    var windowHeight = $(window).height();
    var top          = ui.position.top;
    var left         = ui.position.left;
    var bottom       = windowHeight - (top + this.$el.outerHeight(true));

    this._storeVideoPosition(left, bottom);
  },

  _mouseEnter: function() {
    this.$el.find(".VideoControls").fadeIn(150);
  },

  _mouseLeave: function() {
    this.$el.find(".VideoControls").fadeOut(150);
  },

  hasVideoData: function() {
    if (this.videoData && this.videoData.id) {
      return true;
    } else {
      return false;
    }
  },

  _saveVideoData: function() {
    this.localStorage.set({ currentVideo: this.videoData });
  },

  _storeVideoID: function(id) {
    this.videoData.id = id;
    this._saveVideoData();
  },

  _storeVideoInfo: function(seconds) {
    this.videoData.seconds = seconds;
    this._saveVideoData();
  },

  _clearStoredData: function(status) {
    this.videoData = null;
    this.localStorage.set({ currentVideo: null });
  },

  _storeMinimizedState: function(minimized) {
    this.videoData.minimized = minimized;
    this._saveVideoData();
  },

  _storeVideoPosition: function(left, bottom) {
    this.videoData.left   = left;
    this.videoData.bottom = bottom;
    this._saveVideoData();
  },

  _storePlayStatus: function(status) {
    this.videoData.status = status;
    this._saveVideoData();
  },

  _seekToStoredPosition: function() {

    var self = this;

    if (this.videoData.seconds) {
      this.player.api('seekTo', this.videoData.seconds);
    }

    if (this.videoData.status === "stop") {

      setTimeout(function() {
        self.player.api('pause');
      }, 100);

    }

  },

  close: function(e, opts) {

    e && e.preventDefault();
    e && e.stopPropagation();

    if (opts && opts.dontHide) {
      this._clearStoredData();
    } else {
      this.$el.animate({ width: 0, height: 0, opacity: 0 }, { easing: "easeInQuad", duration: 200, complete: this._onCloseAnimationFinished });
    }

  },

  _onCloseAnimationFinished: function() {
    this._clearStoredData();
    this._removeVideoBinds();
    this.remove();
  },

  _toggle: function(e) {

    e.preventDefault();
    e.stopPropagation();

    this.model.set("minimized", !!!this.model.get("minimized"));
  },

  _onChangeMinimized: function() {

    var self = this;

    var windowWidth    = $(window).width();
    var windowHeight   = $(window).height();
    var documentHeight = $(document).height();

    var elTop  = this.$el.offset().top;
    var elLeft = this.$el.position().left;

    function calcHorizontalPos(w) {
      if ((elLeft + w) > windowWidth) {
        var right = windowWidth - elLeft - self.$el.outerWidth(true);
{'fraction_non_alphanumeric': 0.15848605577689243, 'fraction_numerical': 0.005577689243027889, 'mean_word_length': 2.6698830409356726, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 7, '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': '6642438', 'n_tokens_mistral': 4252, 'n_tokens_neox': 4039, 'n_words': 683}