strategy-singleitem.js 4.4 KB
/*
 *
 * ADOBE CONFIDENTIAL
 *
 * Copyright 2016 Adobe Systems Incorporated
 * All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 *
 *
 */
(function($, player) {
    'use strict';

    /**
     * A strategy which only shows a single item of a cycle.
     * The strategy will store its position even when stopped
     *
     * Since this strategy only shows a single item,
     * all transitions are ignored
     *
     * @class
     *
     * @param {HTMLElement|jQuery}  el  The sequence element
     */
    var SingleItemStrategy = function(el) {
        this.$el = $(el);
        this.el = this.$el.get(0);

        this.$el.data('screens-strategy', this);

        this.items().hide();

        this.startItem();
    };

    /**
     * The sequence elements for this strategy.
     */
    SingleItemStrategy.prototype.init = function() {};

    SingleItemStrategy.prototype.items = function() {
        return $(this.el.getElementsByClassName('cq-Sequence-item'));
    };

    /**
     * Get the item which is shown when the sequence starts
     *
     * @return {jQuery} The starting item according to the strategy
     */
    SingleItemStrategy.prototype.startItem = function() {
        this.$currentItem = null;
        return $();
    };

    /**
     * Check if a cycle is complete
     *
     * @param {jQuery} [$item] The current sequence element
     *
     * @returns {boolean} `true` if the cycle of the sequence is completed, `false` otherwise
     */
    SingleItemStrategy.prototype.isCycleComplete = function($item) {
        return !this.items().length || !!$item;
    };

    SingleItemStrategy.prototype.getNext = function() {
        var $items = this.items();
        var index = this.$currentItem ? $items.index(this.$currentItem) : -1;
        var $nextItem = $items.eq((index + 1) % $items.length);

        return $nextItem;
    };

    /**
     * Get the next sequence element for this strategy.
     *
     * @param  {jQuery} [$item] The current sequence element
     *
     * @return {jQuery} The next item according to the strategy
     */
    SingleItemStrategy.prototype.next = function($item) {
        // if a previous item was already shown
        // we do not provide another item
        if ($item) {
            return null;
        }
        this.$currentItem = this.getNext();

        return this.$currentItem ? {
            $item: this.$currentItem,
            transitionType: 'normal'
        } : null;
    };

    /**
     * Get the duration for the specified sequence element.
     *
     * @param  {jQuery} [$item] The sequence element to get the duration for
     *
     * @return {Number} The duration for the item in ms
     */
    SingleItemStrategy.prototype.duration = function($item) {
        var item = $item.get(0);
        var duration = null;
        if (item) {
            duration = parseInt(item.dataset.duration, 10);
            if (!duration) {
                var el = item.querySelector('[data-duration]');
                duration = el ? parseInt(el.dataset.duration, 10) : null;
            }
        }

        if (!duration || duration === '0') {
            // 0 or null case, use duration of the sequence
            duration = parseInt(this.el.dataset.duration, 10) || 5000;
        }

        return parseInt(duration, 10);
    };

    /**
     * Schedule the current item.
     *
     * @param  {Function} callback   Function to call when the next item should play
     * @param  {Number} duration     The duration in ms
     */
    SingleItemStrategy.prototype.scheduleCurrent = function(callback, duration) {
        this.timeout = window.setTimeout(callback, duration);
    };

    /**
     * Destroy the strategy.
     */
    SingleItemStrategy.prototype.destroy = function() {
        this.$el.removeData('screens-strategy');
        this.$el = null;
        this.el = null;
    };

    player.strategies.singleitem = SingleItemStrategy;

}(window.jQuery, window.ScreensPlayer));