Best JavaScript code snippet using wpt
TemplateVariableRow.js
Source:TemplateVariableRow.js
1import React, {PropTypes, Component} from 'react'2import {connect} from 'react-redux'3import {bindActionCreators} from 'redux'4import uniq from 'lodash/uniq'5import OnClickOutside from 'react-onclickoutside'6import classnames from 'classnames'7import Dropdown from 'shared/components/Dropdown'8import DeleteConfirmButtons from 'shared/components/DeleteConfirmButtons'9import TemplateQueryBuilder from 'src/dashboards/components/TemplateQueryBuilder'10import {runTemplateVariableQuery as runTemplateVariableQueryAJAX} from 'src/dashboards/apis'11import parsers from 'shared/parsing'12import {TEMPLATE_TYPES} from 'src/dashboards/constants'13import generateTemplateVariableQuery from 'src/dashboards/utils/templateVariableQueryGenerator'14import {errorThrown as errorThrownAction} from 'shared/actions/errors'15import {publishAutoDismissingNotification} from 'shared/dispatchers'16const compact = values => uniq(values).filter(value => /\S/.test(value))17const RowValues = ({18 selectedType,19 values = [],20 isEditing,21 onStartEdit,22 autoFocusTarget,23}) => {24 const _values = values.map(v => v.value).join(', ')25 if (selectedType === 'csv') {26 return (27 <TableInput28 name="values"29 defaultValue={_values}30 isEditing={isEditing}31 onStartEdit={onStartEdit}32 autoFocusTarget={autoFocusTarget}33 spellCheck={false}34 autoComplete={false}35 />36 )37 }38 return (39 <div className={values.length ? 'tvm-values' : 'tvm-values-empty'}>40 {values.length ? _values : 'No values to display'}41 </div>42 )43}44const RowButtons = ({45 onStartEdit,46 isEditing,47 onCancelEdit,48 onDelete,49 id,50 selectedType,51}) => {52 if (isEditing) {53 return (54 <div className="tvm-actions">55 <button56 className="btn btn-sm btn-info"57 type="button"58 onClick={onCancelEdit}59 >60 Cancel61 </button>62 <button className="btn btn-sm btn-success" type="submit">63 {selectedType === 'csv' ? 'Save Values' : 'Get Values'}64 </button>65 </div>66 )67 }68 return (69 <div className="tvm-actions">70 <DeleteConfirmButtons onDelete={() => onDelete(id)} />71 <button72 className="btn btn-sm btn-info btn-edit btn-square"73 type="button"74 onClick={e => {75 // prevent subsequent 'onSubmit' that is caused by an unknown source,76 // possible onClickOutside, after 'onClick'. this allows77 // us to enter 'isEditing' mode78 e.preventDefault()79 onStartEdit('tempVar')80 }}81 >82 <span className="icon pencil" />83 </button>84 </div>85 )86}87const TemplateVariableRow = ({88 template: {id, tempVar, values},89 isEditing,90 selectedType,91 selectedDatabase,92 selectedMeasurement,93 onSelectType,94 onSelectDatabase,95 onSelectMeasurement,96 selectedTagKey,97 onSelectTagKey,98 onStartEdit,99 onCancelEdit,100 autoFocusTarget,101 onSubmit,102 onDelete,103 onErrorThrown,104}) =>105 <form106 className={classnames('template-variable-manager--table-row', {107 editing: isEditing,108 })}109 onSubmit={onSubmit({110 selectedType,111 selectedDatabase,112 selectedMeasurement,113 selectedTagKey,114 })}115 >116 <div className="tvm--col-1">117 <TableInput118 name="tempVar"119 defaultValue={tempVar}120 isEditing={isEditing}121 onStartEdit={onStartEdit}122 autoFocusTarget={autoFocusTarget}123 />124 </div>125 <div className="tvm--col-2">126 <Dropdown127 items={TEMPLATE_TYPES}128 onChoose={onSelectType}129 onClick={() => onStartEdit(null)}130 selected={TEMPLATE_TYPES.find(t => t.type === selectedType).text}131 className="dropdown-140"132 />133 </div>134 <div className="tvm--col-3">135 <TemplateQueryBuilder136 onSelectDatabase={onSelectDatabase}137 selectedType={selectedType}138 selectedDatabase={selectedDatabase}139 onSelectMeasurement={onSelectMeasurement}140 selectedMeasurement={selectedMeasurement}141 selectedTagKey={selectedTagKey}142 onSelectTagKey={onSelectTagKey}143 onStartEdit={onStartEdit}144 onErrorThrown={onErrorThrown}145 />146 <RowValues147 selectedType={selectedType}148 values={values}149 isEditing={isEditing}150 onStartEdit={onStartEdit}151 autoFocusTarget={autoFocusTarget}152 />153 </div>154 <div className="tvm--col-4">155 <RowButtons156 onStartEdit={onStartEdit}157 isEditing={isEditing}158 onCancelEdit={onCancelEdit}159 onDelete={onDelete}160 id={id}161 selectedType={selectedType}162 />163 </div>164 </form>165const TableInput = ({166 name,167 defaultValue,168 isEditing,169 onStartEdit,170 autoFocusTarget,171}) => {172 return isEditing173 ? <div name={name} style={{width: '100%'}}>174 <input175 required={true}176 name={name}177 autoFocus={name === autoFocusTarget}178 className="form-control input-sm tvm-input-edit"179 type="text"180 defaultValue={181 name === 'tempVar'182 ? defaultValue.replace(/\u003a/g, '') // remove ':'s183 : defaultValue184 }185 />186 </div>187 : <div style={{width: '100%'}} onClick={() => onStartEdit(name)}>188 <div className="tvm-input">{defaultValue}</div>189 </div>190}191class RowWrapper extends Component {192 constructor(props) {193 super(props)194 const {template: {type, query, isNew}} = this.props195 this.state = {196 isEditing: !!isNew,197 isNew: !!isNew,198 hasBeenSavedToComponentStateOnce: !isNew,199 selectedType: type,200 selectedDatabase: query && query.db,201 selectedMeasurement: query && query.measurement,202 selectedTagKey: query && query.tagKey,203 autoFocusTarget: 'tempVar',204 }205 this.handleSubmit = ::this.handleSubmit206 this.handleSelectType = ::this.handleSelectType207 this.handleSelectDatabase = ::this.handleSelectDatabase208 this.handleSelectMeasurement = ::this.handleSelectMeasurement209 this.handleSelectTagKey = ::this.handleSelectTagKey210 this.handleStartEdit = ::this.handleStartEdit211 this.handleCancelEdit = ::this.handleCancelEdit212 this.runTemplateVariableQuery = ::this.runTemplateVariableQuery213 }214 handleSubmit({215 selectedDatabase: database,216 selectedMeasurement: measurement,217 selectedTagKey: tagKey,218 selectedType: type,219 }) {220 return async e => {221 e.preventDefault()222 const {223 source,224 template,225 template: {id},226 onRunQuerySuccess,227 onRunQueryFailure,228 tempVarAlreadyExists,229 notify,230 } = this.props231 const _tempVar = e.target.tempVar.value.replace(/\u003a/g, '')232 const tempVar = `\u003a${_tempVar}\u003a` // add ':'s233 if (tempVarAlreadyExists(tempVar, id)) {234 return notify(235 'error',236 `Variable '${_tempVar}' already exists. Please enter a new value.`237 )238 }239 this.setState({240 isEditing: false,241 hasBeenSavedToComponentStateOnce: true,242 })243 const {query, tempVars} = generateTemplateVariableQuery({244 type,245 tempVar,246 query: {247 database,248 // rp, TODO249 measurement,250 tagKey,251 },252 })253 const queryConfig = {254 type,255 tempVars,256 query,257 database,258 // rp: TODO259 measurement,260 tagKey,261 }262 try {263 let parsedData264 if (type === 'csv') {265 parsedData = e.target.values.value266 .split(',')267 .map(value => value.trim())268 } else {269 parsedData = await this.runTemplateVariableQuery(source, queryConfig)270 }271 onRunQuerySuccess(template, queryConfig, compact(parsedData), tempVar)272 } catch (error) {273 onRunQueryFailure(error)274 }275 }276 }277 handleClickOutside() {278 this.handleCancelEdit()279 }280 handleStartEdit(name) {281 this.setState({isEditing: true, autoFocusTarget: name})282 }283 handleCancelEdit() {284 const {285 template: {type, query: {db, measurement, tagKey}, id},286 onDelete,287 } = this.props288 const {hasBeenSavedToComponentStateOnce} = this.state289 if (!hasBeenSavedToComponentStateOnce) {290 return onDelete(id)291 }292 this.setState({293 selectedType: type,294 selectedDatabase: db,295 selectedMeasurement: measurement,296 selectedTagKey: tagKey,297 isEditing: false,298 })299 }300 handleSelectType(item) {301 this.setState({302 selectedType: item.type,303 selectedDatabase: null,304 selectedMeasurement: null,305 selectedTagKey: null,306 })307 }308 handleSelectDatabase(item) {309 this.setState({selectedDatabase: item.text})310 }311 handleSelectMeasurement(item) {312 this.setState({selectedMeasurement: item.text})313 }314 handleSelectTagKey(item) {315 this.setState({selectedTagKey: item.text})316 }317 async runTemplateVariableQuery(318 source,319 {query, database, rp, tempVars, type, measurement, tagKey}320 ) {321 try {322 const {data} = await runTemplateVariableQueryAJAX(source, {323 query,324 db: database,325 rp,326 tempVars,327 })328 const parsedData = parsers[type](data, tagKey || measurement) // tagKey covers tagKey and fieldKey329 if (parsedData.errors.length) {330 throw parsedData.errors331 }332 return parsedData[type]333 } catch (error) {334 console.error(error)335 throw error336 }337 }338 render() {339 const {340 isEditing,341 selectedType,342 selectedDatabase,343 selectedMeasurement,344 selectedTagKey,345 autoFocusTarget,346 } = this.state347 return (348 <TemplateVariableRow349 {...this.props}350 isEditing={isEditing}351 selectedType={selectedType}352 selectedDatabase={selectedDatabase}353 selectedMeasurement={selectedMeasurement}354 selectedTagKey={selectedTagKey}355 onSelectType={this.handleSelectType}356 onSelectDatabase={this.handleSelectDatabase}357 onSelectMeasurement={this.handleSelectMeasurement}358 onSelectTagKey={this.handleSelectTagKey}359 onStartEdit={this.handleStartEdit}360 onCancelEdit={this.handleCancelEdit}361 autoFocusTarget={autoFocusTarget}362 onSubmit={this.handleSubmit}363 />364 )365 }366}367const {arrayOf, bool, func, shape, string} = PropTypes368RowWrapper.propTypes = {369 source: shape({370 links: shape({371 proxy: string,372 }),373 }).isRequired,374 template: shape({375 type: string.isRequired,376 tempVar: string.isRequired,377 query: shape({378 db: string,379 influxql: string,380 measurement: string,381 tagKey: string,382 }),383 values: arrayOf(384 shape({385 value: string.isRequired,386 type: string.isRequired,387 selected: bool.isRequired,388 })389 ).isRequired,390 links: shape({391 self: string.isRequired,392 }),393 }),394 onRunQuerySuccess: func.isRequired,395 onRunQueryFailure: func.isRequired,396 onDelete: func.isRequired,397 tempVarAlreadyExists: func.isRequired,398 notify: func.isRequired,399}400TemplateVariableRow.propTypes = {401 ...RowWrapper.propTypes,402 selectedType: string.isRequired,403 selectedDatabase: string,404 selectedTagKey: string,405 onSelectType: func.isRequired,406 onSelectDatabase: func.isRequired,407 onSelectTagKey: func.isRequired,408 onStartEdit: func.isRequired,409 onCancelEdit: func.isRequired,410 onSubmit: func.isRequired,411 onErrorThrown: func.isRequired,412}413TableInput.propTypes = {414 defaultValue: string,415 isEditing: bool.isRequired,416 onStartEdit: func.isRequired,417 name: string.isRequired,418 autoFocusTarget: string,419}420RowValues.propTypes = {421 selectedType: string.isRequired,422 values: arrayOf(shape()),423 isEditing: bool.isRequired,424 onStartEdit: func.isRequired,425 autoFocusTarget: string,426}427RowButtons.propTypes = {428 onStartEdit: func.isRequired,429 isEditing: bool.isRequired,430 onCancelEdit: func.isRequired,431 onDelete: func.isRequired,432 id: string.isRequired,433 selectedType: string.isRequired,434}435const mapDispatchToProps = dispatch => ({436 onErrorThrown: bindActionCreators(errorThrownAction, dispatch),437 notify: bindActionCreators(publishAutoDismissingNotification, dispatch),438})...
tab-group.js
Source:tab-group.js
1export default class TabGroup {2 constructor() {3 this.nodesByElement = new Map();4 this.lastElement = null;5 this.autofocusTarget = null;6 }7 appendElement(element, autofocus) {8 const lastNode = this.nodesByElement.get(this.lastElement) || {next: element, previous: element};9 const next = lastNode.next;10 const previous = this.lastElement || element;11 this.nodesByElement.set(element, {next, previous});12 this.nodesByElement.get(lastNode.next).previous = element;13 lastNode.next = element;14 this.lastElement = element;15 if (autofocus && this.autofocusTarget === null) {16 this.autofocusTarget = element;17 }18 }19 removeElement(element) {20 const node = this.nodesByElement.get(element);21 if (node) {22 const beforeNode = this.nodesByElement.get(node.previous);23 const afterNode = this.nodesByElement.get(node.next);24 beforeNode.next = node.next;25 afterNode.previous = node.previous;26 }27 this.nodesByElement.delete(element);28 }29 after(element) {30 const node = this.nodesByElement.get(element) || {next: undefined};31 return node.next;32 }33 focusAfter(element) {34 const original = this.getCurrentFocus();35 let next = this.after(element);36 while (next && next !== element) {37 next.focus();38 if (this.getCurrentFocus() !== original) {39 return;40 }41 next = this.after(next);42 }43 }44 before(element) {45 const node = this.nodesByElement.get(element) || {previous: undefined};46 return node.previous;47 }48 focusBefore(element) {49 const original = this.getCurrentFocus();50 let previous = this.before(element);51 while (previous && previous !== element) {52 previous.focus();53 if (this.getCurrentFocus() !== original) {54 return;55 }56 previous = this.before(previous);57 }58 }59 autofocus() {60 this.autofocusTarget && this.autofocusTarget.focus();61 }62 /* istanbul ignore next */63 getCurrentFocus() {64 return document.activeElement;65 }...
Using AI Code Generation
1var wptouch = new WPTouch();2wptouch.autofocusTarget('input');3var wptouch = new WPTouch();4wptouch.autofocusTarget('input');5var wptouch = new WPTouch();6wptouch.autofocusTarget('input');7var wptouch = new WPTouch();8wptouch.autofocusTarget('input');9var wptouch = new WPTouch();10wptouch.autofocusTarget('input');11var wptouch = new WPTouch();12wptouch.autofocusTarget('input');13var wptouch = new WPTouch();14wptouch.autofocusTarget('input');15var wptouch = new WPTouch();16wptouch.autofocusTarget('input');17var wptouch = new WPTouch();18wptouch.autofocusTarget('input');19var wptouch = new WPTouch();20wptouch.autofocusTarget('input');21var wptouch = new WPTouch();22wptouch.autofocusTarget('input');23var wptouch = new WPTouch();24wptouch.autofocusTarget('input');25var wptouch = new WPTouch();26wptouch.autofocusTarget('input');
Using AI Code Generation
1const wptDriver = require('wpt-driver');2wptDriver.autofocusTarget('input[name="q"]');3wptDriver.autofocusTarget('input[name="q"]', function(err) {4 if (err) {5 throw err;6 }7});8const wptDriver = require('wpt-driver');9wptDriver.click('input[name="q"]');10wptDriver.click('input[name="q"]', function(err) {11 if (err) {12 throw err;13 }14});15const wptDriver = require('wpt-driver');16wptDriver.close();17wptDriver.close(function(err) {18 if (err) {19 throw err;20 }21});22const wptDriver = require('wpt-driver');23wptDriver.currentUrl();24wptDriver.currentUrl(function(err, url) {25 if (err) {26 throw err;27 }28});29const wptDriver = require('wpt-driver');30wptDriver.deleteAllCookies();31wptDriver.deleteAllCookies(function(err) {32 if (err) {33 throw err;34 }35});36const wptDriver = require('wpt-driver');37wptDriver.deleteCookie('name');38wptDriver.deleteCookie('name', function(err) {39 if (err) {40 throw err;41 }42});43const wptDriver = require('wpt-driver');44wptDriver.execute(function() {45 return document.title;46});47wptDriver.execute(function() { return document.title; }, function(err, title) {48 if (err) {49 throw err;50 }51});52const wptDriver = require('
Using AI Code Generation
1var myWptbElement = document.querySelector( '.wptb-element' );2myWptbElement.autofocusTarget();3var myWptbElement = document.querySelector( '.wptb-element' );4myWptbElement.autofocusTarget();5var myWptbElement = document.querySelector( '.wptb-element' );6myWptbElement.autofocusTarget();7var myWptbElement = document.querySelector( '.wptb-element' );8myWptbElement.autofocusTarget();9var myWptbElement = document.querySelector( '.wptb-element' );10myWptbElement.autofocusTarget();11var myWptbElement = document.querySelector( '.wptb-element' );12myWptbElement.autofocusTarget();13var myWptbElement = document.querySelector( '.wptb-element' );14myWptbElement.autofocusTarget();15var myWptbElement = document.querySelector( '.wptb-element' );16myWptbElement.autofocusTarget();17var myWptbElement = document.querySelector( '.wptb-element'
Using AI Code Generation
1autofocusTarget(target, document);2autofocusTarget(target, document);3autofocusTarget(target, document);4autofocusTarget(target, document);5autofocusTarget(target, document);6autofocusTarget(target, document);7autofocusTarget(target, document);8autofocusTarget(target, document);9autofocusTarget(target, document);10autofocusTarget(target, document);11autofocusTarget(target, document);
Using AI Code Generation
1function autofocusTarget(){2 var elem = document.getElementById("focus");3 if(elem!=null){4 elem.focus();5 }6}7function autofocusTarget(){8 var elem = document.getElementById("focus");9 if(elem!=null){10 elem.focus();11 }12 else{13 }14}15function autofocusTarget(){16 var elem = document.getElementById("focus");17 if(elem!=null){18 elem.focus();19 }20 else{21 }22}23function autofocusTarget(){24 var elem = document.getElementById("focus");25 if(elem!=null){26 elem.focus();27 }28 else{29 }30}31function autofocusTarget(){32 var elem = document.getElementById("focus");33 if(elem!=null){34 elem.focus();35 }36 else{37 }38}39function autofocusTarget(){40 var elem = document.getElementById("focus");41 if(elem!=null){42 elem.focus();43 }44 else{45 }46}47function autofocusTarget(){48 var elem = document.getElementById("focus");49 if(elem!=null){50 elem.focus();51 }52 else{53 }54}
Using AI Code Generation
1function setFocus()2{3 var target = document.getElementById("mytextbox");4 window.opener.top.gWPTB.autofocusTarget(target);5}6function setFocus()7{8 var target = document.getElementById("mytextbox");9 window.opener.top.gWPTB.autofocusTarget(target);10}11function setFocus()12{13 var target = document.getElementById("mytextbox");14 window.opener.top.gWPTB.autofocusTarget(target);15}16function setFocus()17{18 var target = document.getElementById("mytextbox");19 window.opener.top.gWPTB.autofocusTarget(target);20}21function setFocus()22{23 var target = document.getElementById("mytextbox");24 window.opener.top.gWPTB.autofocusTarget(target);25}26function setFocus()27{28 var target = document.getElementById("mytextbox");29 window.opener.top.gWPTB.autofocusTarget(target);30}
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!!