Best JavaScript code snippet using devicefarmer-stf
dragdrop.ts
Source:dragdrop.ts
1import {NgModule,Directive,OnDestroy,AfterViewInit,ElementRef,HostListener,Input,Output,EventEmitter,NgZone} from '@angular/core';2import {CommonModule} from '@angular/common';3import {DomHandler} from 'primeng/dom';4@Directive({5 selector: '[pDraggable]'6})7export class Draggable implements AfterViewInit, OnDestroy {8 9 @Input('pDraggable') scope: string;10 11 @Input() dragEffect: string;12 13 @Input() dragHandle: string;14 15 @Output() onDragStart: EventEmitter<any> = new EventEmitter();16 17 @Output() onDragEnd: EventEmitter<any> = new EventEmitter();18 19 @Output() onDrag: EventEmitter<any> = new EventEmitter();20 21 handle: any;22 dragListener: any;23 mouseDownListener: any;24 mouseUpListener: any;25 _pDraggableDisabled: boolean;26 27 constructor(public el: ElementRef, public zone: NgZone) {}28 @Input() get pDraggableDisabled(): boolean {29 return this._pDraggableDisabled;30 }31 set pDraggableDisabled(_pDraggableDisabled:boolean) {32 this._pDraggableDisabled = _pDraggableDisabled;33 34 if (this._pDraggableDisabled) {35 this.unbindMouseListeners();36 }37 else {38 this.el.nativeElement.draggable = true;39 this.bindMouseListeners();40 }41 }42 43 ngAfterViewInit() {44 if (!this.pDraggableDisabled) {45 this.el.nativeElement.draggable = true;46 this.bindMouseListeners();47 }48 }49 bindDragListener() {50 if (!this.dragListener) {51 this.zone.runOutsideAngular(() => {52 this.dragListener = this.drag.bind(this);53 this.el.nativeElement.addEventListener('drag', this.dragListener);54 });55 }56 }57 unbindDragListener() {58 if (this.dragListener) {59 this.zone.runOutsideAngular(() => {60 this.el.nativeElement.removeEventListener('drag', this.dragListener);61 this.dragListener = null;62 });63 }64 }65 bindMouseListeners() {66 if (!this.mouseDownListener && !this.mouseUpListener) {67 this.zone.runOutsideAngular(() => {68 this.mouseDownListener = this.mousedown.bind(this);69 this.mouseUpListener = this.mouseup.bind(this);70 this.el.nativeElement.addEventListener('mousedown', this.mouseDownListener);71 this.el.nativeElement.addEventListener('mouseup', this.mouseUpListener);72 });73 }74 }75 unbindMouseListeners() {76 if (this.mouseDownListener && this.mouseUpListener) {77 this.zone.runOutsideAngular(() => {78 this.el.nativeElement.removeEventListener('mousedown', this.mouseDownListener);79 this.el.nativeElement.removeEventListener('mouseup', this.mouseUpListener);80 this.mouseDownListener = null;81 this.mouseUpListener = null;82 });83 }84 }85 drag(event) {86 this.onDrag.emit(event);87 }88 @HostListener('dragstart', ['$event']) 89 dragStart(event) {90 if (this.allowDrag() && !this.pDraggableDisabled) {91 if (this.dragEffect) {92 event.dataTransfer.effectAllowed = this.dragEffect;93 }94 event.dataTransfer.setData('text', this.scope);95 96 this.onDragStart.emit(event);97 this.bindDragListener();98 }99 else {100 event.preventDefault();101 }102 }103 @HostListener('dragend', ['$event']) 104 dragEnd(event) {105 this.onDragEnd.emit(event);106 this.unbindDragListener();107 }108 109 mousedown(event) {110 this.handle = event.target;111 }112 mouseup(event) {113 this.handle = null;114 }115 116 allowDrag() : boolean {117 if (this.dragHandle && this.handle)118 return DomHandler.matches(this.handle, this.dragHandle);119 else120 return true;121 }122 ngOnDestroy() {123 this.unbindDragListener();124 this.unbindMouseListeners();125 }126 127}128@Directive({129 selector: '[pDroppable]'130})131export class Droppable implements AfterViewInit, OnDestroy {132 133 @Input('pDroppable') scope: string|string[];134 @Input() pDroppableDisabled: boolean;135 136 @Input() dropEffect: string;137 138 @Output() onDragEnter: EventEmitter<any> = new EventEmitter();139 140 @Output() onDragLeave: EventEmitter<any> = new EventEmitter();141 142 @Output() onDrop: EventEmitter<any> = new EventEmitter();143 144 constructor(public el: ElementRef, public zone: NgZone) {}145 dragOverListener: any;146 ngAfterViewInit() {147 if (!this.pDroppableDisabled) {148 this.bindDragOverListener();149 }150 }151 bindDragOverListener() {152 if (!this.dragOverListener) {153 this.zone.runOutsideAngular(() => {154 this.dragOverListener = this.dragOver.bind(this);155 this.el.nativeElement.addEventListener('dragover', this.dragOverListener);156 });157 }158 }159 unbindDragOverListener() {160 if (this.dragOverListener) {161 this.zone.runOutsideAngular(() => {162 this.el.nativeElement.removeEventListener('dragover', this.dragOverListener);163 this.dragOverListener = null;164 });165 }166 }167 dragOver(event) {168 event.preventDefault();169 }170 171 @HostListener('drop', ['$event'])172 drop(event) {173 if (this.allowDrop(event)) {174 DomHandler.removeClass(this.el.nativeElement, 'p-draggable-enter');175 event.preventDefault();176 this.onDrop.emit(event);177 }178 }179 180 @HostListener('dragenter', ['$event']) 181 dragEnter(event) {182 event.preventDefault();183 184 if (this.dropEffect) {185 event.dataTransfer.dropEffect = this.dropEffect;186 }187 188 DomHandler.addClass(this.el.nativeElement, 'p-draggable-enter');189 this.onDragEnter.emit(event);190 }191 192 @HostListener('dragleave', ['$event']) 193 dragLeave(event) {194 event.preventDefault();195 196 DomHandler.removeClass(this.el.nativeElement, 'p-draggable-enter');197 this.onDragLeave.emit(event);198 }199 200 allowDrop(event): boolean {201 let dragScope = event.dataTransfer.getData('text');202 if (typeof (this.scope) == "string" && dragScope == this.scope) {203 return true;204 }205 else if (this.scope instanceof Array) {206 for(let j = 0; j < this.scope.length; j++) {207 if (dragScope == this.scope[j]) {208 return true;209 }210 }211 }212 return false;213 }214 ngOnDestroy() {215 this.unbindDragOverListener();216 }217}218@NgModule({219 imports: [CommonModule],220 exports: [Draggable,Droppable],221 declarations: [Draggable,Droppable]222})...
aDraggable.ts
Source:aDraggable.ts
1import {Directive, HostListener, HostBinding, ElementRef, Output, Input, EventEmitter} from "angular2/core";2import {AGridService} from "./aGridService";3export class ADraggable {4 private mouseMoveListener: (event: MouseEvent) => void = null;5 private mouseUpListener: (event: MouseEvent) => void = null;6 private offsetX: number;7 private offsetY: number;8 private boundingRect: ClientRect;9 @HostBinding("style.position") pos = "absolute";10 @HostBinding("style.left.px") dragLeft = 0;11 @HostBinding("style.top.px") dragTop = 0;12 @HostBinding("style.pointer-events")13 public get stylePointerEvents(): string {14 return this.dragging ? "none" : "auto";15 }16 public dragging: boolean = false;17 @Output()18 public dropped = new EventEmitter(true);19 constructor(protected elementRef: ElementRef, protected grid: AGridService) {20 }21 @HostListener("mousedown", ['$event'])22 startDrag(event: MouseEvent) {23 let e = this.elementRef.nativeElement as HTMLElement;24 this.offsetX = event.offsetX;25 this.offsetY = event.offsetY;26 this.boundingRect = (e.parentNode as HTMLElement).getBoundingClientRect();27 this.dragging = true;28 this.clearListeners(); // Make sure to clean up old listeners29 this.mouseMoveListener = (event: MouseEvent) => this.onDrag(event);30 this.mouseUpListener = (event: MouseEvent) => this.drop(event);31 window.addEventListener("mousemove", this.mouseMoveListener, true);32 window.addEventListener("mouseup", this.mouseUpListener, true);33 event.preventDefault();34 }35 onDrag(event: MouseEvent) {36 this.dragLeft = Math.max(event.pageX - this.boundingRect.left - this.offsetX);37 this.dragTop = Math.max(event.pageY - this.boundingRect.top - this.offsetY);38 // this.ghost.style.top = this.roundTop(this.top) + "px";39 // this.ghost.style.left = this.roundLeft(this.left) + "px";40 }41 drop(event: MouseEvent) {42 this.dragging = false;43 this.clearListeners();44 }45 clearListeners() {46 if (this.mouseMoveListener != null) {47 window.removeEventListener("mousemove", this.mouseMoveListener, true);48 this.mouseMoveListener = null;49 }50 if (this.mouseUpListener != null) {51 window.removeEventListener("mouseup", this.mouseUpListener, true);52 this.mouseUpListener = null;53 }54 }55 // roundLeft(left: number) {56 // let x = Math.round(left / (this.gridSize + this.gridPadding));57 // return this.gridPadding + x * this.gridPadding + x * this.gridSize;58 // }59 //60 // roundTop(top: number) {61 // let y = Math.round(top / (this.gridSize + this.gridPadding));62 // return this.gridPadding + y * this.gridPadding + y * this.gridSize;63 // }...
dragToScroll.js
Source:dragToScroll.js
1function mouseDownDragHandler(element, event) {2 element.style.cursor = 'grabbing';3 element.style.userSelect = 'none';4 element.pos = {5 left: element.scrollLeft,6 top: element.scrollTop,7 x: event.clientX,8 y: event.clientY9 }10 element.mouseMoveListener = evt => mouseMoveDragHandler(element, evt);11 element.mouseUpListener = evt => mouseUpDragHandler(element);12 element.addEventListener("mousemove", element.mouseMoveListener);13 element.addEventListener("mouseup", element.mouseUpListener);14 element.addEventListener("mouseout", element.mouseUpListener);15}16function mouseMoveDragHandler(element, event) {17 if (element.pos) {18 const dx = event.clientX - element.pos.x;19 const dy = event.clientY - element.pos.y;20 element.scrollTop = element.pos.top - dy;21 element.scrollLeft = element.pos.left - dx;22 }23}24function mouseUpDragHandler(element) {25 element.pos = null;26 element.style.cursor = 'grab';27 element.style.removeProperty('user-select');28 if (element.mouseMoveListener) {29 element.removeEventListener("mousemove", element.mouseMoveListener);30 }31 if (element.mouseUpListener) {32 element.removeEventListener("mouseup", element.mouseUpListener);33 element.removeEventListener("mouseout", element.mouseUpListener);34 }...
Using AI Code Generation
1var stf = require('devicefarmer-stf');2stf.mouseUpListener(100, 100);3var stf = require('devicefarmer-stf');4stf.mouseDownListener(100, 100);5var stf = require('devicefarmer-stf');6stf.mouseMoveListener(100, 100);7var stf = require('devicefarmer-stf');8stf.mouseWheelListener(100, 100);9var stf = require('devicefarmer-stf');10stf.keyboardListener(100, 100);11var stf = require('devicefarmer-stf');12stf.clipboardListener(100, 100);13var stf = require('devicefarmer-stf');14stf.videoListener(100, 100);15var stf = require('devicefarmer-stf');16stf.screenListener(100, 100);17var stf = require('devicefarmer-stf');18stf.deviceListener(100, 100);19var stf = require('devicefarmer-stf');20stf.disconnectListener();21var stf = require('devicefarmer-stf');22stf.deviceInfo(100, 100);23var stf = require('devicefarmer-stf');24stf.deviceConnect(100, 100
Using AI Code Generation
1var stf = require('devicefarmer-stf');2stf.mouseUpListener();3var stf = require('devicefarmer-stf');4stf.mouseDownListener();5var stf = require('devicefarmer-stf');6stf.mouseMoveListener();7var stf = require('devicefarmer-stf');8stf.mouseWheelListener();9var stf = require('devicefarmer-stf');10stf.keyboardListener();11var stf = require('devicefarmer-stf');12stf.touchUpListener();13var stf = require('devicefarmer-stf');14stf.touchDownListener();15var stf = require('devicefarmer-stf');16stf.touchMoveListener();17var stf = require('devicefarmer-stf');18stf.touchCancelListener();19var stf = require('devicefarmer-stf');20stf.touchDoubleTapListener();21var stf = require('devicefarmer-stf');22stf.touchLongPressListener();23var stf = require('devicefarmer-stf');24stf.touchScrollListener();25var stf = require('devicefarmer-stf');26stf.touchFlingListener();27var stf = require('devicefarmer-stf');28stf.touchPinchListener();
Using AI Code Generation
1function mouseUpListener() {2 var event = new MouseEvent("mouseup", {3 });4 var cb = document.getElementById("checkbox");5 var canceled = !cb.dispatchEvent(event);6 if(canceled) {7 console.log("canceled");8 } else {9 console.log("not canceled");10 }11}12function mouseDownListener() {13 var event = new MouseEvent("mousedown", {14 });15 var cb = document.getElementById("checkbox");16 var canceled = !cb.dispatchEvent(event);17 if(canceled) {18 console.log("canceled");19 } else {20 console.log("not canceled");21 }22}23function mouseMoveListener() {24 var event = new MouseEvent("mousemove", {25 });26 var cb = document.getElementById("checkbox");27 var canceled = !cb.dispatchEvent(event);28 if(canceled) {29 console.log("canceled");30 } else {31 console.log("not canceled");32 }33}34function mouseWheelListener() {35 var event = new MouseEvent("mousewheel", {36 });37 var cb = document.getElementById("checkbox");38 var canceled = !cb.dispatchEvent(event);39 if(canceled) {40 console.log("canceled");41 } else {42 console.log("not canceled");43 }44}45function keyDownListener() {46 var event = new KeyboardEvent("keydown", {47 });48 var cb = document.getElementById("checkbox");
Using AI Code Generation
1var stf = require('devicefarmer-stf');2stf.mouseUpListener(0, 0, function(err, data) {3 if (err) {4 console.log('Error: ', err);5 return;6 }7 console.log('data: ', data);8});9var stf = require('devicefarmer-stf');10stf.mouseDownListener(0, 0, function(err, data) {11 if (err) {12 console.log('Error: ', err);13 return;14 }15 console.log('data: ', data);16});17var stf = require('devicefarmer-stf');18stf.mouseMoveListener(0, 0, function(err, data) {19 if (err) {20 console.log('Error: ', err);21 return;22 }23 console.log('data: ', data);24});25var stf = require('devicefarmer-stf');26stf.mouseWheelListener(0, 0, function(err, data) {27 if (err) {28 console.log('Error: ', err);29 return;30 }31 console.log('data: ', data);32});33var stf = require('devicefarmer-stf');34stf.mouseWheelListener(0, 0, function(err, data) {35 if (err) {36 console.log('Error: ', err);37 return;38 }39 console.log('data: ', data);40});41var stf = require('devicefarmer-stf');42stf.mouseWheelListener(0, 0, function(err, data) {43 if (err) {44 console.log('Error: ', err);45 return;46 }47 console.log('data: ', data);48});49var stf = require('devicefarmer-stf');50stf.mouseWheelListener(
Using AI Code Generation
1var stf = require('devicefarmer-stf');2var device = stf.getDevice();3device.mouseUpListener(function (x, y) {4 console.log('Mouse up detected at: ' + x + ', ' + y);5});6var stf = require('devicefarmer-stf');7var device = stf.getDevice();8device.mouseDownListener(function (x, y) {9 console.log('Mouse down detected at: ' + x + ', ' + y);10});11var stf = require('devicefarmer-stf');12var device = stf.getDevice();13device.mouseMoveListener(function (x, y) {14 console.log('Mouse move detected at: ' + x + ', ' + y);15});16var stf = require('devicefarmer-stf');17var device = stf.getDevice();18device.touchUpListener(function (x, y) {19 console.log('Touch up detected at: ' + x + ', ' + y);20});21var stf = require('devicefarmer-stf');22var device = stf.getDevice();23device.touchDownListener(function (x, y) {24 console.log('Touch down detected at: ' + x + ', ' + y);25});26var stf = require('devicefarmer-stf');27var device = stf.getDevice();28device.touchMoveListener(function (x, y) {29 console.log('Touch move detected at: ' + x + ', ' + y);30});31var stf = require('devicefarmer-stf');32var device = stf.getDevice();33device.touchCancelListener(function (x, y) {
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!!