<commit_before>// Copyright (c) Microsoft Open Technologies, Inc.  All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
define([
    '../Core/_Global',
    '../Core/_Base',
    '../Core/_BaseUtils',
    '../Core/_Events',
    '../Core/_Resources',
    '../Utilities/_Control',
    '../Utilities/_ElementUtilities',
    'require-style!less/desktop/controls',
    'require-style!less/phone/controls'
    ], 
    function toggleNewInit(_Global, _Base, _BaseUtils, _Events, _Resources, _Control, _ElementUtilities) {
        "use strict";

        _Base.Namespace.define("WinJS.UI", {
            ToggleSwitchNew: _Base.Namespace._lazy(function() {

                // Store some class names
                var classContainer = 'win-toggleswitch-new';
                var classHeader = 'win-toggleswitch-header-new';
                var classTrack = 'win-toggleswitch-track-new';
                var classFill = 'win-toggleswitch-fill-new';
                var classFillLower = 'win-toggleswitch-fill-lower-new';
                var classFillUpper = 'win-toggleswitch-fill-upper-new';
                var classThumb = 'win-toggleswitch-thumb-new';
                var classValue = 'win-toggleswitch-value-new';
                var classDescription = 'win-toggleswitch-description-new';
                var classOn = 'win-toggleswitch-on-new';
                var classOff = 'win-toggleswitch-off-new';
                var classDragging = 'win-toggleswitch-dragging-new';
                var classPressed = 'win-toggleswitch-pressed-new';

                // Define the ToggleSwitch class
                var Toggle = _Base.Class.define(function ToggleSwitchNew_ctor(element, options) {
                    // Constructor

                    // Set up DOM elements

                    // Main container
                    element = element || _Global.document.createElement('div');
                    this._domElement = element;
                    _ElementUtilities.addClass(this._domElement, classContainer);

                    // Header/Title text
                    this._headerElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._headerElement, classHeader);
                    this._domElement.appendChild(this._headerElement);

                    // Clickable region
                    this._clickElement = _Global.document.createElement('div');
                    this._domElement.appendChild(this._clickElement);

                    // Slider track
                    this._trackElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._trackElement, classTrack);
                    this._clickElement.appendChild(this._trackElement);

                    // Lower portion of slider
                    this._fillLowerElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._fillLowerElement, classFill);
                    _ElementUtilities.addClass(this._fillLowerElement, classFillLower);
                    this._trackElement.appendChild(this._fillLowerElement);

                    // Thumb element
                    this._thumbElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._thumbElement, classThumb);
                    this._trackElement.appendChild(this._thumbElement);

                    // Upper portion of slider
                    this._fillUpperElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._fillUpperElement, classFill);
                    _ElementUtilities.addClass(this._fillUpperElement, classFillUpper);
                    this._trackElement.appendChild(this._fillUpperElement);

                    // Current value label
                    this._valueElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._valueElement, classValue);
                    this._clickElement.appendChild(this._valueElement);

                    // Description text
                    this._descriptionElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._descriptionElement, classDescription);
                    this._domElement.appendChild(this._descriptionElement);

                    // Some initialization of main element
                    element.winControl = this;
                    _ElementUtilities.addClass(element, 'win-disposable');

                    // Current x coord while being dragged
                    var dragX = 0;

                    // Event handlers
                    var pointerDownHandler = function(e) {
                        e.preventDefault();
                        this._mousedown = true;
                        _ElementUtilities.addClass(this._domElement, classPressed);
                    }.bind(this);

                    var pointerUpHandler = function(e) {
                        // Since up is a global event we should only take action
                        // if a mousedown was registered on us initially
                        if (!this._mousedown) {
                            return;
                        }

                        e.preventDefault();

                        // If the thumb is being dragged, pick a new value based on what the thumb
                        // was closest to
                        if (this._dragging) {
                            var maxX = this._trackElement.offsetWidth - this._thumbElement.offsetWidth;
                            this.checked = dragX >= maxX / 2;
                            this._dragging = false;
                            _ElementUtilities.removeClass(this._domElement, classDragging);
                        } else {
                            // Otherwise, just toggle the value as the up constitutes a 
                            // click event
                            this.checked = !this.checked;
                        }

                        // Reset tracking variables and intermediate styles
                        this._mousedown = false;
                        this._thumbElement.style.left = '';
                        this._fillLowerElement.style.width = '';
                        this._fillUpperElement.style.width = '';
                        _ElementUtilities.removeClass(this._domElement, classPressed);
                    }.bind(this);

                    var pointerMoveHandler = function(e) {
                        // Not dragging if mouse isn't down
                        if (!this._mousedown) {
                            return;
                        }

                        e.preventDefault();

                        // Always seem to get one move event even on a simple click
                        // so we will eat the first move event
                        if (!this._ateFirstDragEvent) {
                            this._ateFirstDragEvent = true;
                            return;
                        }

                        // On the first drag event, set dragging state
                        if (!this._dragging) {
                            _ElementUtilities.addClass(this._domElement, classDragging);
                            this._dragging = true;
                        }

                        // Get pointer x coord relative to control
                        var pageX = 'touches' in e ? e.touches[0].pageX : e.pageX;
                        var localMouseX = pageX - this._trackElement.offsetLeft - this._thumbElement.offsetWidth / 2;

                        // Calculate a new width for the fill elements and position for
                        // the thumb
                        var maxX = this._trackElement.offsetWidth - this._thumbElement.offsetWidth;
                        var trackOffset = this._fillLowerElement.offsetLeft + this._trackElement.offsetLeft + this._trackElement.clientLeft;
                        dragX = Math.min(maxX, localMouseX);
                        dragX = Math.max(0, dragX);

                        this._thumbElement.style.left = dragX + 'px';
                        this._fillLowerElement.style.width = (dragX - trackOffset) + 'px';
                        this._fillUpperElement.style.width = (maxX - dragX - trackOffset) + 'px';
                    }.bind(this);

                    // Add listeners
                    this._clickElement.addEventListener('mousedown', pointerDownHandler);
                    this._clickElement.addEventListener('touchstart', pointerDownHandler);
                    window.addEventListener('mousemove', pointerMoveHandler);
                    window.addEventListener('touchmove', pointerMoveHandler);
                    window.addEventListener('mouseup', pointerUpHandler);
                    window.addEventListener('touchend', pointerUpHandler);

                    // Default state is unchecked
                    this._setChecked(false);
                }, {
                    // Properties
                    element: {
                        get: function() {return this._domElement;}
                    },
                    checked: {
                        get: function () {return this._checked;},
                        set: function (value) {this._setChecked(value);}
                    },

                    // Methods
                    _setChecked: function(value) {<commit_msg>Fix toggles that aren't at origin<commit_after>// Copyright (c) Microsoft Open Technologies, Inc.  All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
define([
    '../Core/_Global',
    '../Core/_Base',
    '../Core/_BaseUtils',
    '../Core/_Events',
    '../Core/_Resources',
    '../Utilities/_Control',
    '../Utilities/_ElementUtilities',
    'require-style!less/desktop/controls',
    'require-style!less/phone/controls'
    ], 
    function toggleNewInit(_Global, _Base, _BaseUtils, _Events, _Resources, _Control, _ElementUtilities) {
        "use strict";

        _Base.Namespace.define("WinJS.UI", {
            ToggleSwitchNew: _Base.Namespace._lazy(function() {

                // Store some class names
                var classContainer = 'win-toggleswitch-new';
                var classHeader = 'win-toggleswitch-header-new';
                var classTrack = 'win-toggleswitch-track-new';
                var classFill = 'win-toggleswitch-fill-new';
                var classFillLower = 'win-toggleswitch-fill-lower-new';
                var classFillUpper = 'win-toggleswitch-fill-upper-new';
                var classThumb = 'win-toggleswitch-thumb-new';
                var classValue = 'win-toggleswitch-value-new';
                var classDescription = 'win-toggleswitch-description-new';
                var classOn = 'win-toggleswitch-on-new';
                var classOff = 'win-toggleswitch-off-new';
                var classDragging = 'win-toggleswitch-dragging-new';
                var classPressed = 'win-toggleswitch-pressed-new';

                // Define the ToggleSwitch class
                var Toggle = _Base.Class.define(function ToggleSwitchNew_ctor(element, options) {
                    // Constructor

                    // Set up DOM elements

                    // Main container
                    element = element || _Global.document.createElement('div');
                    this._domElement = element;
                    _ElementUtilities.addClass(this._domElement, classContainer);

                    // Header/Title text
                    this._headerElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._headerElement, classHeader);
                    this._domElement.appendChild(this._headerElement);

                    // Clickable region
                    this._clickElement = _Global.document.createElement('div');
                    this._domElement.appendChild(this._clickElement);

                    // Slider track
                    this._trackElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._trackElement, classTrack);
                    this._clickElement.appendChild(this._trackElement);

                    // Lower portion of slider
                    this._fillLowerElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._fillLowerElement, classFill);
                    _ElementUtilities.addClass(this._fillLowerElement, classFillLower);
                    this._trackElement.appendChild(this._fillLowerElement);

                    // Thumb element
                    this._thumbElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._thumbElement, classThumb);
                    this._trackElement.appendChild(this._thumbElement);

                    // Upper portion of slider
                    this._fillUpperElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._fillUpperElement, classFill);
                    _ElementUtilities.addClass(this._fillUpperElement, classFillUpper);
                    this._trackElement.appendChild(this._fillUpperElement);

                    // Current value label
                    this._valueElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._valueElement, classValue);
                    this._clickElement.appendChild(this._valueElement);

                    // Description text
                    this._descriptionElement = _Global.document.createElement('div');
                    _ElementUtilities.addClass(this._descriptionElement, classDescription);
                    this._domElement.appendChild(this._descriptionElement);

                    // Some initialization of main element
                    element.winControl = this;
                    _ElementUtilities.addClass(element, 'win-disposable');

                    // Current x coord while being dragged
                    var dragX = 0;

                    // Event handlers
                    var pointerDownHandler = function(e) {
                        e.preventDefault();
                        this._mousedown = true;
                        _ElementUtilities.addClass(this._domElement, classPressed);
                    }.bind(this);

                    var pointerUpHandler = function(e) {
                        // Since up is a global event we should only take action
                        // if a mousedown was registered on us initially
                        if (!this._mousedown) {
                            return;
                        }

                        e.preventDefault();

                        // If the thumb is being dragged, pick a new value based on what the thumb
                        // was closest to
                        if (this._dragging) {
                            var maxX = this._trackElement.offsetWidth - this._thumbElement.offsetWidth;
                            this.checked = dragX >= maxX / 2;
                            this._dragging = false;
                            _ElementUtilities.removeClass(this._domElement, classDragging);
                        } else {
                            // Otherwise, just toggle the value as the up constitutes a 
                            // click event
                            this.checked = !this.checked;
                        }

                        // Reset tracking variables and intermediate styles
                        this._mousedown = false;
                        this._thumbElement.style.left = '';
                        this._fillLowerElement.style.width = '';
                        this._fillUpperElement.style.width = '';
                        _ElementUtilities.removeClass(this._domElement, classPressed);
                    }.bind(this);

                    var pointerMoveHandler = function(e) {
                        // Not dragging if mouse isn't down
                        if (!this._mousedown) {
                            return;
                        }

                        e.preventDefault();

                        // Always seem to get one move event even on a simple click
                        // so we will eat the first move event
                        if (!this._ateFirstDragEvent) {
                            this._ateFirstDragEvent = true;
                            return;
                        }

                        // On the first drag event, set dragging state
                        if (!this._dragging) {
                            _ElementUtilities.addClass(this._domElement, classDragging);
                            this._dragging = true;
                        }

                        // Get pointer x coord relative to control
                        var pageX = 'touches' in e ? e.touches[0].pageX : e.pageX;
                        var localMouseX = pageX - this._trackElement.offsetLeft - this._thumbElement.offsetWidth / 2;

                        // Calculate a new width for the fill elements and position for
                        // the thumb
                        var maxX = this._trackElement.offsetWidth - this._thumbElement.offsetWidth;
                        var trackOffset = this._fillLowerElement.offsetLeft + this._trackElement.clientLeft;
                        dragX = Math.min(maxX, localMouseX);
                        dragX = Math.max(0, dragX);

                        this._thumbElement.style.left = dragX + 'px';
                        this._fillLowerElement.style.width = (dragX - trackOffset) + 'px';
                        this._fillUpperElement.style.width = (maxX - dragX - trackOffset) + 'px';
                    }.bind(this);

                    // Add listeners
                    this._clickElement.addEventListener('mousedown', pointerDownHandler);
                    this._clickElement.addEventListener('touchstart', pointerDownHandler);
                    window.addEventListener('mousemove', pointerMoveHandler);
                    window.addEventListener('touchmove', pointerMoveHandler);
                    window.addEventListener('mouseup', pointerUpHandler);
                    window.addEventListener('touchend', pointerUpHandler);

                    // Default state is unchecked
                    this._setChecked(false);
                }, {
                    // Properties
                    element: {
                        get: function() {return this._domElement;}
                    },
                    checked: {
                        get: function () {return this._checked;},
                        set: function (value) {this._setChecked(value);}
                    },

                    // Methods
                    _setChecked: function(value) {
{'fraction_non_alphanumeric': 0.08405645582854156, 'fraction_numerical': 0.0007318348144276007, 'mean_word_length': 1.3571956628881223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6426283', 'n_tokens_mistral': 4341, 'n_tokens_neox': 4028, 'n_words': 892}