<commit_before>/**
 * @fileOverview Requirejs module containing the antie.widgets.hbbtvsource class.
 *
 * @preserve Copyright (c) 2013 British Broadcasting Corporation
 * (http://www.bbc.co.uk) and TAL Contributors (1)
 *
 * (1) TAL Contributors are listed in the AUTHORS file and at
 *     https://github.com/fmtvp/TAL/AUTHORS - please extend this file,
 *     not this notice.
 *
 * @license Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * All rights reserved
 * Please contact us for an alternative licence
 */

require.def('antie/devices/broadcastsource/hbbtvsource',
    [
        'antie/devices/browserdevice',
        'antie/devices/broadcastsource/basetvsource',
        'antie/application',
        'antie/devices/broadcastsource/channel',
        'antie/events/tunerunavailableevent',
        'antie/events/tunerpresentingevent',
        'antie/events/tunerstoppedevent'
    ],
    function (Device, BaseTvSource, Application, Channel, TunerUnavailableEvent, TunerPresentingEvent, TunerStoppedEvent ) {
        /**
         * Contains a HBBTV implementation of the antie broadcast TV source.
         */
        var DOM_ELEMENT_ID = 'broadcastVideoObject';
        var ID_DVB_T = 12;
        var HbbTVSource = BaseTvSource.extend(/** @lends antie.devices.broadcastsource.HbbTVSource.prototype */ {
            /**
             * @constructor
             * @ignore
             */
            init: function () {

                var self = this;
                this._broadcastVideoObject = document.getElementById(DOM_ELEMENT_ID);

                if (!this._broadcastVideoObject) {
                    throw new Error('Unable to initialise HbbTV broadcast source');
                }

                // adding as instance rather then class var as module instantiated via method
                this._playStates = {
                    UNREALIZED: 0,
                    CONNECTING: 1,
                    PRESENTING: 2,
                    STOPPED: 3
                };

                this.playState = this._playStates.UNREALIZED;

                this._broadcastVideoObject.addEventListener("PlayStateChange", function() {

                    var oldPlayState = self.playState;
                    var newPlayState = self.getPlayState();

                    if (oldPlayState === self._playStates.PRESENTING && newPlayState === self._playStates.UNREALIZED) {
                        Application.getCurrentApplication().broadcastEvent(new TunerUnavailableEvent());

                    } else if (newPlayState === self._playStates.PRESENTING) {
                        Application.getCurrentApplication().broadcastEvent(new TunerPresentingEvent(self.getCurrentChannel()));

                    } else if (newPlayState === self._playStates.STOPPED) {
                        Application.getCurrentApplication().broadcastEvent(new TunerStoppedEvent());
                    }

                    self.playState = newPlayState;

                });
            },
            showCurrentChannel: function () {
                // Check if exception is thrown by bindToCurrentChannel?
                this._setBroadcastToFullScreen();
                this._broadcastVideoObject.bindToCurrentChannel();
                this._broadcastVideoObject.style.display = "block";
            },
            stopCurrentChannel: function () {
                try {
                    if (this.getPlayState() === this._playStates.UNREALIZED) {
                        this._broadcastVideoObject.bindToCurrentChannel();
                    }
                } catch(e) {
                    throw Error("Unable to bind to current channel");
                }

                this._broadcastVideoObject.stop();
                this._broadcastVideoObject.style.display = "none";
            },
            getCurrentChannelName: function () {
                var channelConfig = this._broadcastVideoObject.currentChannel;
                // maybe check the hbbtv docs to check return type
                if (channelConfig === null) {
                    throw new Error('Current channel name not available');
                }

                if (channelConfig.name === null) {
                    throw new Error('Current channel name is null');
                }

                if (typeof channelConfig.name === "undefined") {
                    throw new Error('Current channel name is not defined');
                }

                return channelConfig.name;
            },
            getChannelList : function (params) {
                try {
                    var result = this._getChannelList();
                    params.onSuccess(result);

                } catch (e) {
                    params.onError(e);
                }
            },
            getCurrentChannel : function () {
                var result = false;

                var currentChannel = this._broadcastVideoObject.currentChannel;

                if (currentChannel) {
                    result = new Channel({
                        name : currentChannel.name,
                        type: currentChannel.channelType,
                        onid: currentChannel.onid,
                        tsid: currentChannel.tsid,
                        sid: currentChannel.sid
                    });
                }

                return result;
            },
            setChannelByName : function(params) {

                var currentChannel = this.getCurrentChannel();

                if (!currentChannel) {
                    params.onError({
                        name : "ChannelError",
                        message: "Unable to determine current channel name"
                    });

                } else if (params.channelName === currentChannel.name) {
                    this.showCurrentChannel();
                    params.onSuccess();

                } else {

                    try {

                        var self = this;

                        var channelList = this._getChannelList();

                        var channel = undefined;

                        for (var i = 0; i < channelList.length; i++) {
                            if (channelList[i].name === params.channelName) {
                                channel = channelList[i];
                                break;
                            }
                        }

                        if (!channel) {
                            throw {
                                name : "ChannelError",
                                message: params.channelName + " not found in channel list"
                            };
                        }

                        this._tuneToChannelByTriplet(channel.type, channel.onid, channel.tsid, channel.sid, params.onSuccess, params.onError);

                    } catch(e) {
                        params.onError(e);
                    }
                }
            },
            setPosition : function(top, left, width, height) {
                this._broadcastVideoObject.style.top = top + "px";
                this._broadcastVideoObject.style.left = left + "px";
                this._broadcastVideoObject.style.width = width + "px";
                this._broadcastVideoObject.style.height = height + "px";
            },
            getPlayState : function() {
                return this._broadcastVideoObject.playState;
            },
            destroy : function() {
                // Not currently required for hbbtv
            },
            setChannel : function(params) {
                var channelType = this._getChannelType();
                this._tuneToChannelByTriplet(channelType, params.onid, params.tsid, params.sid, params.onSuccess, params.onError);
            },
            _tuneToChannelByTriplet: function (channelType, onid, tsid, sid, onSuccess, onError) {
                var newChannel = this._broadcastVideoObject.createChannelObject(channelType, onid, tsid, sid);
                if (newChannel === null) {
                    onError({
                        name : "ChannelError",
                        message : "Channel could not be found"
                    });
                } else {
                    this._tuneToChannelObject(newChannel, onSuccess, onError);
                }
            },
            _tuneToChannelObject: function (newChannel, onSuccess, onError) {
                var self = this;
                var successEventListener = function(channel) {
                    self._broadcastVideoObject.removeEventListener("ChannelChangeSucceeded", successEventListener);
                    self._broadcastVideoObject.removeEventListener("ChannelChangeError", errorEventListener);
                    onSuccess();
                };

                var errorEventListener = function(channel, errorState) {
                    self._broadcastVideoObject.removeEventListener("ChannelChangeSucceeded", successEventListener);
                    self._broadcastVideoObject.removeEventListener("ChannelChangeError", errorEventListener);
                    onError({
                        name : "ChangeChannelError",
                        message : "Error tuning channel"
                    });
                };

                this._broadcastVideoObject.addEventListener("ChannelChangeSucceeded", successEventListener);
                this._broadcastVideoObject.addEventListener("ChannelChangeError", errorEventListener);

                this._broadcastVideoObject.setChannel(newChannel);
            },
            /**
             * @returns The type of the channel, as indicated by one of the ID_* constants in the HBBTV
             *      specification. Defaults to ID_DVB_T in the case of error
             *  @private
             */
            _getChannelType : function() {
                var channelType = undefined;
                try {
                    channelType = this._broadcastVideoObject.currentChannel.idType;
                    if (typeof channelType === "undefined") {
                        channelType = ID_DVB_T;
                    }
                } catch(e) {
                    channelType = ID_DVB_T;
                }

                return channelType;
            },
            /**
             * Sets the size of the broadcast object to be the same as the required screen size identified by antie at
             * application start up.
             */
            _setBroadcastToFullScreen : function() {
                var currentLayout = Application.getCurrentApplication().getLayout().requiredScreenSize;
                this.setPosition(0, 0, currentLayout.width, currentLayout.height);
            },
            _getChannelList : function() {
                var channelConfig = undefined;

                try {
                    channelConfig = this._broadcastVideoObject.getChannelConfig();
                } catch (e) {
                    throw {
                        name : "ChannelListError",
                        message : "Channel list is not available"
                    };
                }

                if (!channelConfig || !channelConfig.channelList || channelConfig.channelList.length === 0) {
                    throw {
                        name : "ChannelListError",
                        message : "Channel list is empty or not available"
                    };
                }

                var result = [];
                for (var i = 0; i < channelConfig.channelList.length; i++) {
                    var channel = channelConfig.channelList[i]
                    result.push(new Channel(
                        {
                            name: channel.name,
                            type: channel.channelType,
                            onid: channel.onid,
                            sid: channel.sid,
                            tsid: channel.tsid
                        }
                    ));
                }
                return result;
            }
        });

        Device.prototype.isBroadcastSourceSupported = function() {
            return this.getHistorian().hasBroadcastOrigin();
        };

        /**
         * Create a broadcastSource object on the Device to be
         * accessed as a singleton to avoid the init being run
         * multiple times
         */
        Device.prototype.createBroadcastSource = function() {
            if (!this._broadcastSource) {
                this._broadcastSource = new HbbTVSource();
            }

            return this._broadcastSource;
        };

        // Return the HbbtvSource object for testing purposes
        return HbbTVSource;
    }
);<commit_msg>Removed extra variable, trimmed whitespace for readability.<commit_after>/**
 * @fileOverview Requirejs module containing the antie.widgets.hbbtvsource class.
 *
 * @preserve Copyright (c) 2013 British Broadcasting Corporation
 * (http://www.bbc.co.uk) and TAL Contributors (1)
 *
 * (1) TAL Contributors are listed in the AUTHORS file and at
 *     https://github.com/fmtvp/TAL/AUTHORS - please extend this file,
 *     not this notice.
 *
 * @license Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * All rights reserved
 * Please contact us for an alternative licence
 */

require.def('antie/devices/broadcastsource/hbbtvsource',
    [
        'antie/devices/browserdevice',
        'antie/devices/broadcastsource/basetvsource',
        'antie/application',
        'antie/devices/broadcastsource/channel',
        'antie/events/tunerunavailableevent',
        'antie/events/tunerpresentingevent',
        'antie/events/tunerstoppedevent'
    ],
    function (Device, BaseTvSource, Application, Channel, TunerUnavailableEvent, TunerPresentingEvent, TunerStoppedEvent ) {
        /**
         * Contains a HBBTV implementation of the antie broadcast TV source.
         */
        var DOM_ELEMENT_ID = 'broadcastVideoObject';
        var ID_DVB_T = 12;
        var HbbTVSource = BaseTvSource.extend(/** @lends antie.devices.broadcastsource.HbbTVSource.prototype */ {
            /**
             * @constructor
             * @ignore
             */
            init: function () {

                var self = this;
                this._broadcastVideoObject = document.getElementById(DOM_ELEMENT_ID);

                if (!this._broadcastVideoObject) {
                    throw new Error('Unable to initialise HbbTV broadcast source');
                }

                // adding as instance rather then class var as module instantiated via method
                this._playStates = {
                    UNREALIZED: 0,
                    CONNECTING: 1,
                    PRESENTING: 2,
                    STOPPED: 3
                };

                this.playState = this._playStates.UNREALIZED;

                this._broadcastVideoObject.addEventListener("PlayStateChange", function() {

                    var oldPlayState = self.playState;
                    var newPlayState = self.getPlayState();

                    if (oldPlayState === self._playStates.PRESENTING && newPlayState === self._playStates.UNREALIZED) {
                        Application.getCurrentApplication().broadcastEvent(new TunerUnavailableEvent());

                    } else if (newPlayState === self._playStates.PRESENTING) {
                        Application.getCurrentApplication().broadcastEvent(new TunerPresentingEvent(self.getCurrentChannel()));

                    } else if (newPlayState === self._playStates.STOPPED) {
                        Application.getCurrentApplication().broadcastEvent(new TunerStoppedEvent());
                    }

                    self.playState = newPlayState;

                });
            },
            showCurrentChannel: function () {
                // Check if exception is thrown by bindToCurrentChannel?
                this._setBroadcastToFullScreen();
                this._broadcastVideoObject.bindToCurrentChannel();
                this._broadcastVideoObject.style.display = "block";
            },
            stopCurrentChannel: function () {
                try {
                    if (this.getPlayState() === this._playStates.UNREALIZED) {
                        this._broadcastVideoObject.bindToCurrentChannel();
                    }
                } catch(e) {
                    throw Error("Unable to bind to current channel");
                }

                this._broadcastVideoObject.stop();
                this._broadcastVideoObject.style.display = "none";
            },
            getCurrentChannelName: function () {
                var channelConfig = this._broadcastVideoObject.currentChannel;
                // maybe check the hbbtv docs to check return type
                if (channelConfig === null) {
                    throw new Error('Current channel name not available');
                }

                if (channelConfig.name === null) {
                    throw new Error('Current channel name is null');
                }

                if (typeof channelConfig.name === "undefined") {
                    throw new Error('Current channel name is not defined');
                }

                return channelConfig.name;
            },
            getChannelList : function (params) {
                try {
                    var result = this._getChannelList();
                    params.onSuccess(result);

                } catch (e) {
                    params.onError(e);
                }
            },
            getCurrentChannel : function () {
                var result = false;

                var currentChannel = this._broadcastVideoObject.currentChannel;

                if (currentChannel) {
                    result = new Channel({
                        name : currentChannel.name,
                        type: currentChannel.channelType,
                        onid: currentChannel.onid,
                        tsid: currentChannel.tsid,
                        sid: currentChannel.sid
                    });
                }

                return result;
            },
            setChannelByName : function(params) {

                var currentChannel = this.getCurrentChannel();

                if (!currentChannel) {
                    params.onError({
                        name : "ChannelError",
                        message: "Unable to determine current channel name"
                    });

                } else if (params.channelName === currentChannel.name) {
                    this.showCurrentChannel();
                    params.onSuccess();

                } else {
                    try {
                        var channelList = this._getChannelList();
                        var channel = undefined;

                        for (var i = 0; i < channelList.length; i++) {
                            if (channelList[i].name === params.channelName) {
                                channel = channelList[i];
                                break;
                            }
                        }

                        if (!channel) {
                            throw {
                                name : "ChannelError",
                                message: params.channelName + " not found in channel list"
                            };
                        }

                        this._tuneToChannelByTriplet(channel.type, channel.onid, channel.tsid, channel.sid, params.onSuccess, params.onError);

                    } catch(e) {
                        params.onError(e);
                    }
                }
            },
            setPosition : function(top, left, width, height) {
                this._broadcastVideoObject.style.top = top + "px";
                this._broadcastVideoObject.style.left = left + "px";
                this._broadcastVideoObject.style.width = width + "px";
                this._broadcastVideoObject.style.height = height + "px";
            },
            getPlayState : function() {
                return this._broadcastVideoObject.playState;
            },
            destroy : function() {
                // Not currently required for hbbtv
            },
            setChannel : function(params) {
                var channelType = this._getChannelType();
                this._tuneToChannelByTriplet(channelType, params.onid, params.tsid, params.sid, params.onSuccess, params.onError);
            },
            _tuneToChannelByTriplet: function (channelType, onid, tsid, sid, onSuccess, onError) {
                var newChannel = this._broadcastVideoObject.createChannelObject(channelType, onid, tsid, sid);
                if (newChannel === null) {
                    onError({
                        name : "ChannelError",
                        message : "Channel could not be found"
                    });
                } else {
                    this._tuneToChannelObject(newChannel, onSuccess, onError);
                }
            },
            _tuneToChannelObject: function (newChannel, onSuccess, onError) {
                var self = this;
                var successEventListener = function(channel) {
                    self._broadcastVideoObject.removeEventListener("ChannelChangeSucceeded", successEventListener);
                    self._broadcastVideoObject.removeEventListener("ChannelChangeError", errorEventListener);
                    onSuccess();
                };

                var errorEventListener = function(channel, errorState) {
                    self._broadcastVideoObject.removeEventListener("ChannelChangeSucceeded", successEventListener);
                    self._broadcastVideoObject.removeEventListener("ChannelChangeError", errorEventListener);
                    onError({
                        name : "ChangeChannelError",
                        message : "Error tuning channel"
                    });
                };

                this._broadcastVideoObject.addEventListener("ChannelChangeSucceeded", successEventListener);
                this._broadcastVideoObject.addEventListener("ChannelChangeError", errorEventListener);

                this._broadcastVideoObject.setChannel(newChannel);
            },
            /**
             * @returns The type of the channel, as indicated by one of the ID_* constants in the HBBTV
             *      specification. Defaults to ID_DVB_T in the case of error
             *  @private
             */
            _getChannelType : function() {
                var channelType = undefined;
                try {
                    channelType = this._broadcastVideoObject.currentChannel.idType;
                    if (typeof channelType === "undefined") {
                        channelType = ID_DVB_T;
                    }
                } catch(e) {
                    channelType = ID_DVB_T;
                }

                return channelType;
            },
            /**
             * Sets the size of the broadcast object to be the same as the required screen size identified by antie at
             * application start up.
             */
            _setBroadcastToFullScreen : function() {
                var currentLayout = Application.getCurrentApplication().getLayout().requiredScreenSize;
                this.setPosition(0, 0, currentLayout.width, currentLayout.height);
            },
            _getChannelList : function() {
                var channelConfig = undefined;

                try {
                    channelConfig = this._broadcastVideoObject.getChannelConfig();
                } catch (e) {
                    throw {
                        name : "ChannelListError",
                        message : "Channel list is not available"
                    };
                }

                if (!channelConfig || !channelConfig.channelList || channelConfig.channelList.length === 0) {
                    throw {
                        name : "ChannelListError",
                        message : "Channel list is empty or not available"
                    };
                }

                var result = [];
                for (var i = 0; i < channelConfig.channelList.length; i++) {
                    var channel = channelConfig.channelList[i]
                    result.push(new Channel(
                        {
                            name: channel.name,
                            type: channel.channelType,
                            onid: channel.onid,
                            sid: channel.sid,
                            tsid: channel.tsid
                        }
                    ));
                }
                return result;
            }
        });

        Device.prototype.isBroadcastSourceSupported = function() {
            return this.getHistorian().hasBroadcastOrigin();
        };

        /**
         * Create a broadcastSource object on the Device to be
         * accessed as a singleton to avoid the init being run
         * multiple times
         */
        Device.prototype.createBroadcastSource = function() {
            if (!this._broadcastSource) {
                this._broadcastSource = new HbbTVSource();
            }

            return this._broadcastSource;
        };

        // Return the HbbtvSource object for testing purposes
        return HbbTVSource;
    }
);
{'fraction_non_alphanumeric': 0.08114974782905464, 'fraction_numerical': 0.0015926586022524743, 'mean_word_length': 1.3866063348416289, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, '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': '974878', 'n_tokens_mistral': 6166, 'n_tokens_neox': 5697, 'n_words': 1413}