BVB Source Codes

dat.gui Show NumberControllerBox.js Source code

Return Download dat.gui: download NumberControllerBox.js Source code - Download dat.gui Source code - Type:.js
  1. /**
  2.  * dat-gui JavaScript Controller Library
  3.  * http://code.google.com/p/dat-gui
  4.  *
  5.  * Copyright 2011 Data Arts Team, Google Creative Lab
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  * http://www.apache.org/licenses/LICENSE-2.0
  12.  */
  13.  
  14. import NumberController from './NumberController';
  15. import dom from '../dom/dom';
  16. import common from '../utils/common';
  17.  
  18. function roundToDecimal(value, decimals) {
  19.   const tenTo = Math.pow(10, decimals);
  20.   return Math.round(value * tenTo) / tenTo;
  21. }
  22.  
  23. /**
  24.  * @class Represents a given property of an object that is a number and
  25.  * provides an input element with which to manipulate it.
  26.  *
  27.  * @extends dat.controllers.Controller
  28.  * @extends dat.controllers.NumberController
  29.  *
  30.  * @param {Object} object The object to be manipulated
  31.  * @param {string} property The name of the property to be manipulated
  32.  * @param {Object} [params] Optional parameters
  33.  * @param {Number} [params.min] Minimum allowed value
  34.  * @param {Number} [params.max] Maximum allowed value
  35.  * @param {Number} [params.step] Increment by which to change value
  36.  *
  37.  * @member dat.controllers
  38.  */
  39. class NumberControllerBox extends NumberController {
  40.   constructor(object, property, params) {
  41.     super(object, property, params);
  42.  
  43.     this.__truncationSuspended = false;
  44.  
  45.     const _this = this;
  46.  
  47.     /**
  48.      * {Number} Previous mouse y position
  49.      * @ignore
  50.      */
  51.     let prevY;
  52.  
  53.     function onChange() {
  54.       const attempted = parseFloat(_this.__input.value);
  55.       if (!common.isNaN(attempted)) {
  56.         _this.setValue(attempted);
  57.       }
  58.     }
  59.  
  60.     function onFinish() {
  61.       if (_this.__onFinishChange) {
  62.         _this.__onFinishChange.call(_this, _this.getValue());
  63.       }
  64.     }
  65.  
  66.     function onBlur() {
  67.       onFinish();
  68.     }
  69.  
  70.     function onMouseDrag(e) {
  71.       const diff = prevY - e.clientY;
  72.       _this.setValue(_this.getValue() + diff * _this.__impliedStep);
  73.  
  74.       prevY = e.clientY;
  75.     }
  76.  
  77.     function onMouseUp() {
  78.       dom.unbind(window, 'mousemove', onMouseDrag);
  79.       dom.unbind(window, 'mouseup', onMouseUp);
  80.       onFinish();
  81.     }
  82.  
  83.     function onMouseDown(e) {
  84.       dom.bind(window, 'mousemove', onMouseDrag);
  85.       dom.bind(window, 'mouseup', onMouseUp);
  86.       prevY = e.clientY;
  87.     }
  88.  
  89.     this.__input = document.createElement('input');
  90.     this.__input.setAttribute('type', 'text');
  91.  
  92.     // Makes it so manually specified values are not truncated.
  93.  
  94.     dom.bind(this.__input, 'change', onChange);
  95.     dom.bind(this.__input, 'blur', onBlur);
  96.     dom.bind(this.__input, 'mousedown', onMouseDown);
  97.     dom.bind(this.__input, 'keydown', function(e) {
  98.       // When pressing enter, you can be as precise as you want.
  99.       if (e.keyCode === 13) {
  100.         _this.__truncationSuspended = true;
  101.         this.blur();
  102.         _this.__truncationSuspended = false;
  103.         onFinish();
  104.       }
  105.     });
  106.  
  107.     this.updateDisplay();
  108.  
  109.     this.domElement.appendChild(this.__input);
  110.   }
  111.  
  112.   updateDisplay() {
  113.     this.__input.value = this.__truncationSuspended ? this.getValue() : roundToDecimal(this.getValue(), this.__precision);
  114.     return super.updateDisplay();
  115.   }
  116. }
  117.  
  118. export default NumberControllerBox;
  119.  
downloadNumberControllerBox.js Source code - Download dat.gui Source code
Related Source Codes/Software:
tmate - Instant Terminal Sharing ht... 2017-04-16
WNXHuntForCity - City find By Objective - C 2017-04-16
st2 - StackStorm (aka IFTTT for Ops) is event-driven aut... 2017-04-16
css-in-js - React: CSS in JS techniques comparison. 2017-04-16
android-viewflow - A horizontal view scroller library for Android 2017-04-16
capybara-webkit - A Capybara driver for headless WebKit to test Java... 2017-04-16
fullstackpython.com - Full Stack Python source with Pelican, Bootstrap a... 2017-04-16
naxsi - NAXSI is an open-source, high performance, low rul... 2017-04-16
ToGL - Direct3D to OpenGL abstraction layer 2017-04-16
LiveScript - LiveScript is a language which compiles to JavaScr... 2017-04-16
MaterialSearchView - Express it in the library to implement SearchView ... 2017-04-23
ptpython - A better Python REPL 2017-04-23
Laravel-4-Bootstrap-Starter-Site - Laravel 4 Starter Site is a basic blog application... 2017-04-23
official-images - Primary source of truth for the Docker "Official I... 2017-04-23
awesome-chrome-devtools - Awesome tooling and resources in the Chrome DevToo... 2017-04-23
EventStore - The open-source, functional database with Complex ... 2017-04-23
screencat - 2017-04-23
viz.js - A hack to put Graphviz on the web. ... 2017-04-23
JKeyboardPanelSwitch - For resolve the layout conflict when keybord & amp... 2017-04-23
mama2 - Mother plan - all firewood high flame 2017-04-23

 Back to top