hip
<reponame>hoox/videojs-adapter-js<filename>src/ads/ima.js
/* global google:true */

var youbora = require('youboralib')
var manifest = require('../../manifest.json')

var ImaAdsAdapter = youbora.Adapter.extend({
  getVersion: function () {
    return manifest.version + '-videojs-ima-ads'
  },

  getDuration: function () {
    if (this.player.ima.getAdsManager().getCurrentAd()!== null) {
      this.duration = this.player.ima.getAdsManager().getCurrentAd().getDuration()
    }
    return this.duration
  },

  // NOT AVAILABLE AT adStart
  // getResource: function () {
  //   return this.player.ima.getAdsManager().getCurrentAd().getMediaUrl()
  // },

  getTitle: function () {
    return this.player.ima.getAdsManager().getCurrentAd().getTitle()
  },

  getPlayhead: function () {
    return this.getDuration() - this.player.ima.getAdsManager().getRemainingTime()
  },

  getPlayerVersion: function () {
    return 'IMA' + google.ima.VERSION + '; contrib-ads'+ this.player.ads.VERSION
  },

  getPosition: function () {
    if (this.plugin._adapter.flags.isEnded) {
      return 'post'
    } else if (this.plugin._adapter.flags.isJoined) {
      return'mid'
    }
    return 'pre'
  },

  registerListeners: function () {
    // Enable playhead monitor
    this.monitorPlayhead(true, false)

    // Shortcut events
    var event = google.ima.AdEvent.Type

    // Console all events if logLevel=DEBUG
    youbora.Util.logAllEvents(this.player.ima.addEventListener, [
      null,
      event.ALL_ADS_COMPLETED,
      event.LINEAR_CHANGED,
      event.USER_CLOSE,
      event.COMPLETE,
      event.IMPRESSION,
      event.CONTENT_PAUSE_REQUESTED,
      event.CONTENT_RESUME_REQUESTED,
      event.SKIPPED,
      event.SKIPPABLE_STATE_CHANGED,
      event.LOADED,
      event.PAUSED,
      event.RESUMED,
      event.STARTED,
      event.AD_CAN_PLAY,
      event.AD_METADATA,
      event.EXPANDED_CHANGED,
      event.AD_BREAK_READY,
      event.LOG,
      event.CLICK,
      google.ima.AdErrorEvent.Type.AD_ERROR
    ])

    // Register listeners

    this.references = []
    this.references[event.LOADED] = this.loadedListener.bind(this)
    this.references[event.CONTENT_PAUSE_REQUESTED] =
      this.contentPauseRequestedListener.bind(this)
    this.references[event.STARTED] = this.startedListener.bind(this)
    this.references[event.PAUSED] = this.pausedListener.bind(this)
    this.references[event.RESUMED] = this.resumedListener.bind(this)
    this.references[event.COMPLETE] = this.completeListener.bind(this)
    this.references[event.SKIPPED] = this.skippedListener.bind(this)
    this.references[event.CLICK] = this.clickListener.bind(this)
    this.references[google.ima.AdErrorEvent.Type.AD_ERROR] =
      this.errorListener.bind(this)
    this.references[event.ALL_ADS_COMPLETED] = this.adsEndedListener.bind(this)

    for (var key in this.references) {
      this.player.ima.addEventListener(key, this.references[key])
    }
    this.player.on('adend', this.adEndedListener.bind(this))
  },

  loadedListener: function (e) {
    this.fireStart()
  },

  contentPauseRequestedListener: function (e) {
    this.fireStart()
  },

  startedListener: function (e) {
    if (this.plugin && this.plugin.getAdapter()) this.plugin.getAdapter().fireStart()
    this.fireStart()
    this.fireJoin()
  },

  pausedListener: function (e) {
    this.firePause()
  },

  resumedListener: function (e) {
    this.fireResume()
  },

  completeListener: function (e) {
    this.fireStop()
  },

  skippedListener: function (e) {
    this.fireStop({ skipped: true })
  },

  adEndedListener: function (e) {
    this.fireStop()
  },

  errorListener: function (e) {
    var error = e.getError()
    this.fireError(error.getCode(), error.getMessage())
  },

  clickListener: function (e) {
    this.fireClick()
  },

  adsEndedListener: function (e) {
    this.plugin.getAdapter().adsEnded = true
  },

  unregisterListeners: function () {
    // Disable playhead monitoring
    this.monitor.stop()

    // unregister listeners
    if (this.player && this.references) {
      for (var key in this.references) {
        this.player.ima.removeEventListener(key, this.references[key])
      }
      this.references = []
    }
  }
},
  // Static Members
  {
    isUsed: function (plugin) {
      return plugin.player.ima
    }
  }
)

module.exports = ImaAdsAdapter
Welcome to the Charity Hopping Around The World Giveaway Hop!
This hop is hosted by: I Am A Reader, Not A Writer, Reading Away the Days, and Reading a Little Bit of Everything.
Each blog in this hop is going to be introducing you to one of thier favorite charities and having a giveaway for you to enter too.
There are just over a hundred blogs participating so there are lots of charities to read about as well as some nice prizes to win.
I would like to introduce you to Dirks Fund Golden Retriever Rescue. I became familiar with them a few months ago when a dog was dumped out here by my house.
At least I’m assuming he was dumped…he might have just excaped from somewhere but since dogs get dumped in this area regularly and no one seemed to know where he came from…that seemed to be the best bet.
We could not keep him. We couldn’t afford all the heart worm meds and shots. And besides that he kept getting out in the road. Which meant he would eventually get hit by a vehicle and badly hurt if not killed.
We called the local shelter which had no room for him either but did put his infomation up on their website. The folks at Dirks Fund saw his picture and offered to take him in and find a home for him.
We did have to drive him part of the way up to the shelter but a volunteer met us halfway. They had him fixed and got his shots and heartworm meds and already found him a home!
These are good people! They are always in need of donations if you would like to do that. They are also in need of all sorts of volunteers if you happen to live close enough(St. Louis, MO area). Please spread the word!
For this hop I’m giving away $10 Paypal cash so everyone can enter. Just enter on the Rafflecopter form below. Then hop on over to the next blog to read about more great charities and enter more giveaways!
Wow, they are wonderful! Thanks for sharing about this charity!
-Heather M.
Great giveaway! These are all perfect! Anyway, I know a lot of people who wants this Charity Hopping Around The World…Looks really interesting!
great giveaway!
Rightly said – Charity begins at home. Good giveaways. A great list for charity giveaways.
We absolutely love your blog and find the majority of your post’s to be exactly I’m looking for. Does one offer guest writers to write content available for you? I wouldn’t mind creating a post or elaborating on many of the subjects you write with regards to here. Again, awesome website!
great giveaway!
Ah, that’s a neat charity. Our friends always get Golden Retriever rescue dogs when the get a new