Best JavaScript code snippet using best
MusicGrid.js
Source:MusicGrid.js  
1/** A grid of toggle buttons used to represent our music. */2class MusicGrid 3{4	/**5 	 * Constructor6	 * @param {number} x - top left x co-ordinate for the music grid7	 * @param {number} y - top left y co-ordinate for the music grid8	 * @param {number} width - width of the music grid9	 * @param {number} height - height of the music grid10 	 * @return {void} Nothing11 	 */12	constructor (x, y, width, height)13	{14		this.x = x;15		this.y = y; 16		this.width = width - 10;17		this.height = height;18		this.gridWidth = 8;19		this.gridHeight = 8;20		this.padding = 0.5;21		this.toggleButtons = [];22		for (let j = 0; j < this.gridWidth; ++j) 23		{24  			for (let i = 0; i < this.gridHeight; ++i) 25  			{26  				this.toggleButtons.push(new ToggleButton (this.x + (this.width / this.gridWidth) * j + (this.padding * 0.5),27  			 										this.y + (this.height / this.gridHeight) * i + (this.padding * 0.5), 28  			 										(this.width / this.gridWidth) - this.padding,29  			 										(this.height / this.gridHeight) - this.padding));30  			}31  		}	32	}33	/**34 	 * Updates the position of the music grid and its buttons.35	 * @param {number} x - top left x co-ordinate for the music grid36	 * @param {number} y - top left y co-ordinate for the music grid37	 * @param {number} w - width of the music grid38	 * @param {number} h - height of the music grid39 	 * @return {void} Nothing40 	 */41	update (x, y, w, h)42	{43		this.x = x + (w * 0.075) + 5.5;44		this.y = y + (h * 0.05) + 0.1; 45		this.width = w * 0.76;46		this.height = h * 0.89;47		let counter = 0; 48		for (let j = 0; j < this.gridWidth; ++j) 49		{50  			for (let i = 0; i < this.gridHeight; ++i) 51  			{52  				this.toggleButtons[counter].update(this.x + (this.width / this.gridWidth) * j + (this.padding * 0.5),53  			 							  		   this.y + (this.height / this.gridHeight) * i + (this.padding * 0.5), 54  			 							  	       (this.width / this.gridWidth) - this.padding,55  			 							           (this.height / this.gridHeight) - this.padding);56  				counter++;57  			}58  		}	59	}60	/**61 	 * Toggle if the buttons should be transparent given if a block is outside the workspace.62	 * @param {bool} isTransparent - if true set the buttons for the grid to be transparent. 63 	 * @return {void} Nothing64 	 */65	toggleTransparency(isTransparent)66	{67		for (let i = 0; i < this.toggleButtons.length; ++i)68  		{69  			this.toggleButtons[i].blocksOutside = isTransparent;70  		}71	}72	/**73 	 * Draws the grid of buttons to the canvas. 74 	 * @return {void} Nothing75 	 */76	draw()77	{78		noStroke();79		fill(darkGrey)80		this.setToggleButtonColours()81		rect(this.x, this.y, this.width, this.height);82  83  		for (let i = 0; i < this.toggleButtons.length; ++i)84  		{85  			this.toggleButtons[i].draw();	86  		}87	}88	/**89 	 * Whether mouse is over the music grid.90 	 * @return {bool} true is mouse is over the grid. 91 	 */92	hasMouseOver()93	{94		if (mouseX >= this.x 95		    && mouseX <= this.x+this.width96		    && mouseY >= this.y97		    && mouseY <= this.y+this.height)98		{99			return true;100		}101		else102		{103			return false;104		}105	}106	/**107 	 * If mouse is pressed check if any of the buttons need to be toggled. 108 	 * @return {void} Nothing109 	 */110	mousePressed() 111	{112		for (let i = 0; i < this.toggleButtons.length; ++i)113		{114	  		if(this.toggleButtons[i].hasMouseOver())115	  		{116	  			this.toggleButtons[i].toogle();117	  		}	118	  	}119	}120	/**121 	 * Takes a boolean array and uses this to toggle the buttons of the musical grid as either on or off.122	 * @param {array} boolArray - an array of 1's and 0's for setting the grid values.123 	 * @return {void} Nothing124 	 */125	setInternalButtonsFromBoolArray(boolArray)126	{127		for (let i = 0; i < boolArray.length; ++i)128		{129			if (boolArray[i] === 1)130			{131				this.toggleButtons[i].setOn();132			}133			else134			{135				this.toggleButtons[i].setOff();136			}137		}138	}139	/**140 	 * Getter for the array of buttons which make up the music grid.141 	 * @return {array} of toggle buttons.142 	 */143	getInternalButtonsArray()144	{145		return this.toggleButtons;146	}147	/**148 	 * Highlights patterns by changing toggle on buttons.149 	 * @return {void} nothing. 150 	 */151	setToggleButtonColours()152	{153		// Make orange to clear the palette154		for (let i = 0; i < (this.toggleButtons.length - 7); ++i){155			this.toggleButtons[i].setOnColour(orange);156		}157		158		// Check Ascending159		for (let i = 0; i < (this.toggleButtons.length - 7); ++i)160  		{161  			if (i % 8 !== 0) // ignore top row162  			{163  				let start = i; 164				let end = start + 7;165				if (this.toggleButtons[start].isOn === true 166					&& this.toggleButtons[end].isOn === true)167				{168					this.toggleButtons[start].setOnColour(googGreen);169					this.toggleButtons[end].setOnColour(googGreen);170				}171  			}172  			173  		}174  		// // Check Descending175  		// for (let i = 0; i < (this.toggleButtons.length - 9); ++i)176  		// {177  		// 	if ((i+1) % 8 !== 0) // ignore bottom row178  		// 	{179  		// 		let start = i; 180				// let end = start + 9;181				// if (this.toggleButtons[start].isOn === true 182				// 	&& this.toggleButtons[end].isOn === true)183				// {184				// 	this.toggleButtons[start].setOnColour(purple);185				// 	this.toggleButtons[end].setOnColour(purple);186				// }187  		// 	}188  			189  		// }190  // 		// Check for thirds191		// for (let i = 0; i < (this.toggleButtons.length - 2); ++i)192  // 		{193  // 			let myList = [6, 7, 14, 15, 22, 23, 30, 31, 38, 39, 46, 47, 54, 55, 62, 63];194  // 			if (!myList.includes(i)) // ignore bottom rows195  // 			{196  // 				let start = i; 197		// 		let end = start + 2;198		// 		if (this.toggleButtons[start].isOn === true 199		// 			&& this.toggleButtons[end].isOn === true)200		// 		{201		// 			this.toggleButtons[start].setOnColour(googGreen);202		// 			this.toggleButtons[end].setOnColour(googGreen);203		// 		}204  // 			}205  // 		}206  		// Check for dissonance207		for (let i = 0; i < (this.toggleButtons.length - 1); ++i)208  		{209  			if ((i+1) % 8 !== 0) // ignore bottom row210  			{211  				let start = i; 212				let end = start + 1;213				if (this.toggleButtons[start].isOn === true 214					&& this.toggleButtons[end].isOn === true)215				{216					this.toggleButtons[start].setOnColour(purple);217					this.toggleButtons[end].setOnColour(purple);218				}219  			}220  		}221	}...ThreeToggle.js
Source:ThreeToggle.js  
1/* @flow */2// React3import React, { Component } from "react";4// semantic-ui-react5import { Label, Segment, Checkbox, Button } from "semantic-ui-react";6export class ThreeToggleMulti extends Component {7  state = { toggleButtons: [] };8  constructor(props: Object): void {9    super(props);10    (this: any).updateChecked = this.updateChecked.bind(this);11  }12  componentDidMount() {13    this.setState({ toggleButtons: this.props.buttons });14  }15  updateChecked(index: number): void {16    const { toggleButtons } = this.state;17    let cloned = toggleButtons.slice(0);18    cloned.forEach(button => {19      button.checked = false;20    });21    cloned[index].checked = !toggleButtons[index].checked;22    this.setState(23      {24        toggleButtons: cloned25      },26      () => {27        const { toggleButtons } = this.state;28        toggleButtons[index].callback(toggleButtons[index].checked);29      }30    );31  }32  render() {33    const { title } = this.props;34    const { toggleButtons } = this.state;35    return (36      <Segment className="three-tool-component-container">37        <Label className="three-tool-component-label" attached="top left">38          {title}39        </Label>40        <div className="three-tool-toggle-container">41          <div className="three-toggle-multi-container">42            {toggleButtons.map((button, index) => {43              const { toggle } = button;44              const toggleVal = (toggle === true || toggle === false) ? toggle : true;45              return(46                <div className="three-toggle-multi">47                  <span className="three-toggle-multi-label">{button.label}</span>48                  <Checkbox49                    className="three-tool-toggle"50                    checked={button.checked}51                    toggle={toggleVal}52                    radio={!toggleVal}53                    onClick={() => this.updateChecked(index)}54                  />55                  {(toggleVal === true) ?56                    <span className="three-toggle-status">57                      {button.checked ? "on" : "off"}58                    </span>59                    : null }60                </div>61              )62            })}63          </div>64        </div>65      </Segment>66    );67  }68}69export default class ThreeToggle extends Component {70  state = { checked: false };71  constructor(props: Object): void {72    super(props);73    (this: any).updateChecked = this.updateChecked.bind(this);74  }75  componentDidMount() {76    this.setState({ checked: this.props.defaultVal });77  }78  updateChecked(): void {79    let newVal = !this.state.checked;80    this.setState(81      {82        checked: newVal83      },84      () => { if (this.props.callback !== undefined) this.props.callback(newVal) }85    );86  }87  render() {88    const { title, toggle, size } = this.props;89    const { checked } = this.state;90    const toggleVal = (toggle !== true || toggle !== false) ? true : toggle;91    const buttonSize = size ? size : "medium"92    return (93      <Segment className="three-tool-component-container">94        <Label className="three-tool-component-label" attached="top left">95          {title}96        </Label>97        <div className="three-tool-toggle-container">98          <Button99            toggle100            active={checked}101            size={buttonSize}102            onClick={this.updateChecked}103          >104            {checked ? "on" : "off"}105          </Button>106        </div>107      </Segment>108    );109  }...form-component.js
Source:form-component.js  
...13        alert("Changed a tag: " + tag);14    }15    //toggle button16    window.prettyPrint && prettyPrint();17    $('.toggle-button-class').toggleButtons({18        style: {19            // Accepted values ["primary", "danger", "info", "success", "warning"] or nothing20            enabled: "success",21            disabled: "danger"22        }23    });24    $('#normal-toggle-button').toggleButtons();25    $('#text-toggle-button').toggleButtons({26        width: 220,27        label: {28            enabled: "Lorem Ipsum",29            disabled: "Dolor Sit"30        }31    });32    $('#not-animated-toggle-button').toggleButtons({33        animated: false34    });35    $('#transition-percent-toggle-button').toggleButtons({36        transitionspeed: "500%"37    });38    $('#transition-value-toggle-button').toggleButtons({39        transitionspeed: 1 // default value: 0.0540    });41    $('#danger-toggle-button').toggleButtons({42        style: {43            // Accepted values ["primary", "danger", "info", "success", "warning"] or nothing44            enabled: "danger",45            disabled: "info"46        }47    });48    $('#info-toggle-button').toggleButtons({49        style: {50            // Accepted values ["primary", "danger", "info", "success", "warning"] or nothing51            enabled: "info",52            disabled: "info"53        }54    });55    $('#success-toggle-button').toggleButtons({56        style: {57            // Accepted values ["primary", "danger", "info", "success", "warning"] or nothing58            enabled: "success",59            disabled: "info"60        }61    });62    $('#warning-toggle-button').toggleButtons({63        style: {64            // Accepted values ["primary", "danger", "info", "success", "warning"] or nothing65            enabled: "warning",66            disabled: "info"67        }68    });69    $('#height-text-style-toggle-button').toggleButtons({70        height: 100,71        font: {72            'line-height': '100px',73            'font-size': '18px',74            'font-style': 'regular'75        }76    });...Using AI Code Generation
1$(document).ready(function() {2  $(".best_in_place").best_in_place();3});4.best_in_place {5  display: inline;6}7.best_in_place:hover {8  background-color: #FFFFCC;9}10.best_in_place:after {11  content: " (edit)";12}13.best_in_place.active {14  background-color: #FFFFCC;15}16.best_in_place.active:after {17  content: " (save)";18}19.best_in_place.active input, .best_in_place.active textarea {20  background-color: #FFFFCC;21}22.best_in_place input, .best_in_place textarea {23  background-color: transparent;24}25.best_in_place input:focus, .best_in_place textarea:focus {26  background-color: #FFFFCC;27}281. Fork it (Using AI Code Generation
1var bestPractice = new BestPractice();2var toggleButtons = bestPractice.toggleButtons();3console.log(toggleButtons);4var bestPractice = new BestPractice();5var toggleButtons = bestPractice.toggleButtons();6console.log(toggleButtons);7var bestPractice = new BestPractice();8var toggleButtons = bestPractice.toggleButtons();9console.log(toggleButtons);10var bestPractice = new BestPractice();11var toggleButtons = bestPractice.toggleButtons();12console.log(toggleButtons);13var bestPractice = new BestPractice();14var toggleButtons = bestPractice.toggleButtons();15console.log(toggleButtons);16var bestPractice = new BestPractice();17var toggleButtons = bestPractice.toggleButtons();18console.log(toggleButtons);19var bestPractice = new BestPractice();20var toggleButtons = bestPractice.toggleButtons();21console.log(toggleButtons);22var bestPractice = new BestPractice();23var toggleButtons = bestPractice.toggleButtons();24console.log(toggleButtons);Using AI Code Generation
1  $(document).ready(function() {2    BestInPlaceEditor.toggleButtons();3  });4  beforeUpdate: (bestInPlaceEditor) ->5  afterUpdate: (bestInPlaceEditor) ->61. Fork it (Using AI Code Generation
1var bestbuy = require('bestbuy')('your api key');2bestbuy.toggleButtons({3}, function(err, data) {4  if (err) {5    console.log("Error: " + err);6    return;7  }8  console.log(JSON.stringify(data));9});10var bestbuy = require('bestbuy')('your api key');11bestbuy.listButtons({12}, function(err, data) {13  if (err) {14    console.log("Error: " + err);15    return;16  }17  console.log(JSON.stringify(data));18});19var bestbuy = require('bestbuy')('your api key');20bestbuy.listButtonDetails({21}, function(err, data) {22  if (err) {23    console.log("Error: " + err);24    return;25  }26  console.log(JSON.stringify(data));27});28var bestbuy = require('bestbuy')('your api key');29bestbuy.listButtonEvents({30}, function(err, data) {31  if (err) {32    console.log("Error: " + err);33    return;34  }35  console.log(JSON.stringify(data));36});37var bestbuy = require('bestbuy')('your api key');38bestbuy.listButtonEventsDetails({39}, function(err, data) {40  if (err) {41    console.log("Error: " + err);Using AI Code Generation
1$(document).ready(function(){2    BestInPlaceEditor.toggleButtons();3});4The problem with this solution is that the buttons are not toggled when the page loads. I think this is because the `toggleButtons()` method is executed before the `BestInPlaceEditor` objects are created. How can I make the buttons appear when the page loads?5self.toggleButtons = function(){6    if (self.activateLinks.length > 0) {7        self.activateLinks.hide();8        self.deactivateLinks.show();9    } else {10        self.activateLinks.show();11        self.deactivateLinks.hide();12    }13}14self.toggleButtons();Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
