Best JavaScript code snippet using ng-mocks
TreeToolbar.js
Source:TreeToolbar.js
1/**2* TreeToolbar3* use Prototype4*/5var TreeToolbar = function(_oControl) {6 /** (Private properties) */7 var __self = this;8 var Control = _oControl;9 var HandleItem = null;10 var cDepth = parseInt(Control.container.style.zIndex) || 0;11 var IconsPath = Control.iconsPath;12 var DataSet = Control.dataSet;13 /** (Public properties) */14 this.highlight = null;15 this.element = null;16 this.buttons = new Array();17 /** (Private methods) */18 var __drawButtons = function() {19 var btns = document.createElement('div');20 btns.style.position = 'absolute';21 btns.style.display = 'none';22 btns.className = 'tree_toolbar';23 // del24 __appendButton(btns, {25 name: 'ico_del',26 hint: getLabel('js-del-str'),27 init : function(button) {28 var e = button.element;29 if (HandleItem !== null && HandleItem.permissions & 8) {30 var icon = IconsPath + 'ico_del.png';31 var hint = getLabel('js-del-str');32 if (HandleItem.lockedBy) {33 var locker = HandleItem.lockedBy;34 var lname = typeof(locker['lname']) == 'string' ? locker['lname'] : '';35 var fname = typeof(locker['lname']) == 'string' ? locker['fname'] : '';36 icon = IconsPath + 'ico_locked.png';37 hint = getLabel('js-page-is-locked');38 }39 e.setAttribute('title', hint);40 e.style.background = "url('" + icon + "') no-repeat";41 e.setAttribute('href', '#');42 e.style.visibility = 'visible';43 } else {44 e.style.visibility = 'hidden';45 }46 },47 release : function(button) {48 if (HandleItem !== null) {49 if (HandleItem.lockedBy) {50 alert(getLabel('js-page-is-locked'));51 } else {52 DataSet.execute('tree_delete_element', {53 'childs' : 1,54 'element' : HandleItem.id,55 'handle_item' : HandleItem56 });57 }58 }59 return false;60 }61 });62 // create63 __appendButton(btns, {64 name: 'ico_add',65 hint: getLabel('js-add-subpage'),66 init : function(button) {67 if (HandleItem !== null && HandleItem.createLink !== false && (HandleItem.permissions && HandleItem.permissions & 4 || !HandleItem.permissions)) {68 button.element.setAttribute('href', HandleItem.createLink);69 button.element.style.visibility = 'visible';70 } else {71 button.element.style.visibility = 'hidden';72 }73 }74 });75 // edit76 __appendButton(btns, {77 name: 'ico_edit',78 hint: getLabel('js-edit-page'),79 init : function(button) {80 var e = button.element;81 if (HandleItem !== null && HandleItem.editLink !== false && HandleItem.permissions & 2) {82 var icon = IconsPath + 'ico_edit.png';83 var hint = getLabel('js-edit-page');84 var href = HandleItem.editLink;85 if (HandleItem.lockedBy) {86 var locker = HandleItem.lockedBy;87 var lname = typeof(locker['lname']) == 'string' ? locker['lname'] : '';88 var fname = typeof(locker['lname']) == 'string' ? locker['fname'] : '';89 icon = IconsPath + 'ico_locked.png';90 hint = getLabel('js-page-is-locked');91 href = '#';92 }93 e.setAttribute('title', hint);94 e.style.background = "url('" + icon + "') no-repeat";95 e.setAttribute('href', href);96 e.style.visibility = 'visible';97 } else {98 e.style.visibility = 'hidden';99 }100 },101 release : function(button) {102 if (HandleItem !== null) {103 if (HandleItem.lockedBy) {104 alert(getLabel('js-page-is-locked'));105 return false;106 }107 }108 return true;109 }110 });111 // blocking112 __appendButton(btns, {113 name: 'blocking',114 hint: getLabel('js-disable-page'),115 icon: IconsPath + 'ico_block.png',116 init : function(button) {117 if (HandleItem !== null && HandleItem.allowActivity) {118 var icon = HandleItem.isActive ? IconsPath + 'ico_unblock.png' : IconsPath + 'ico_block.png';119 var hint = HandleItem.isActive ? getLabel('js-disable-page') : getLabel('js-enable-page');120 button.element.style.background = "url('" + icon + "') no-repeat";121 button.element.setAttribute('href', '#');122 button.element.setAttribute('title', hint);123 button.element.style.visibility = 'visible';124 } else {125 button.element.style.visibility = 'hidden';126 }127 },128 release : function(button) {129 if (HandleItem !== null) {130 var icon = HandleItem.isActive ? IconsPath + 'ico_block.png' : IconsPath + 'ico_unblock.png';131 var hint = HandleItem.isActive ? getLabel('js-enable-page') : getLabel('js-disable-page');132 button.element.setAttribute('title', hint);133 button.element.style.background = "url('" + icon + "') no-repeat";134 DataSet.execute('tree_set_activity', {135 'element' : HandleItem.id,136 'active' : HandleItem.isActive ? 0 : 1,137 'handle_item' : HandleItem138 });139 }140 return false;141 }142 });143 // virtual copy144 __appendButton(btns, {145 name: 'copy',146 hint: getLabel('js-vcopy-str'),147 init : function(button) {148 if (HandleItem !== null && HandleItem.allowCopy) {149 button.element.style.visibility = 'visible';150 } else {151 button.element.style.visibility = 'hidden';152 }153 },154 release : function(button) {155 if (HandleItem !== null) {156 HandleItem.className = 'ti virtual';157 if (HandleItem.isDefault) HandleItem.labelControl.className += ' main-page';158 HandleItem.isVirtualCopy = true;159 DataSet.execute('tree_copy_element', {160 'element' : HandleItem.id,161 'childs' : 1,162 'links' : 1,163 'virtuals' : 1,164 'permissions' : 1,165 'handle_item' : HandleItem,166 'copy_all' : 0167 });168 }169 return false;170 }171 });172 // real copy173 __appendButton(btns, {174 name: 'clone',175 hint: getLabel('js-copy-str'),176 init : function(button) {177 if (HandleItem !== null && HandleItem.allowCopy) {178 button.element.style.visibility = 'visible';179 } else {180 button.element.style.visibility = 'hidden';181 }182 },183 release : function(button) {184 if (HandleItem !== null) {185 DataSet.execute('tree_copy_element', {186 'element' : HandleItem.id,187 'childs' : 1,188 'links' : 1,189 'permissions' : 1,190 'handle_item' : HandleItem,191 'copy_all' : 0,192 'clone_mode' : 1193 });194 }195 return false;196 }197 });198 // view199 __appendButton(btns, {200 name: 'view',201 hint: getLabel('js-view-page'),202 init : function(button) {203 if (HandleItem !== null && HandleItem.viewLink !== false) {204 button.element.setAttribute('href', HandleItem.viewLink);205 button.element.style.visibility = 'visible';206 } else {207 button.element.style.visibility = 'hidden';208 }209 }210 });211 __self.element = document.body.appendChild(btns);212 };213 var __initButtons = function() {214 for (var i = 0; i < __self.buttons.length; i++) {215 __self.buttons[i].init(__self.buttons[i]);216 }217 };218 var __appendButton = function(container, options) {219 var b = document.createElement('a');220 var name = options.name || 'toolbtn';221 var href = options.href || '#';222 var icon = options.icon || IconsPath + name + ".png";223 var init = options.init || function() {};224 var title = options.hint || '';225 var el = container.appendChild(b);226 var button = {227 'name' : name,228 'href' : href,229 'icon': icon,230 'init' : init,231 'element' : el232 };233 __self.buttons[__self.buttons.length] = button;234 el.setAttribute('href', href);235 el.setAttribute('title', title);236 el.className = options.className || 'tree_toolbtn';237 if (typeof(options.release) === 'function') {238 el.onclick = function() {239 if (!DataSet.isAvailable()) return false;240 return options.release(button);241 };242 } else {243 el.onclick = function() {244 if (!DataSet.isAvailable()) return false;245 if(HandleItem.focus) HandleItem.focus();246 return true;247 };248 }249 250 el.name = name;251 el.style.background = "url('" + icon + "') no-repeat";252 };253 var __draw = function() {254 var el = document.createElement('div');255 el.className = 'tree-highlight';256 el.style.display = 'none';257 el.style.position = 'absolute';258 el.style.zIndex = cDepth - 1;259 __self.highlight = Control.container.appendChild(el);260 __drawButtons();261 };262 263 /** (Public methods) */264 this.show = function(_HandleItem, bForce) {265 bForce = bForce || false;266 // unselect old HandleItem267 if (typeof(_HandleItem) === 'undefined' || (HandleItem === _HandleItem && !bForce)) return false;268 269 if (HandleItem) {270 HandleItem.labelControl.className = 'ti';271 if (HandleItem.isDefault) HandleItem.labelControl.className += ' main-page';272 }273 HandleItem = _HandleItem;274 HandleItem.labelControl.className = HandleItem.getSelected() ? 'ti selected-highlight' : 'ti hightlight';275 if (HandleItem.isDefault) HandleItem.labelControl.className += ' main-page';276 //HandleItem.focus();277 __initButtons();278 //var iroot = HandleItem.control.getRootNodeId();279 var container = HandleItem.control.initContainer;280 var cpos = jQuery(container).position();281 282 this.element.style.top = HandleItem.position.top + cpos.top + 'px';283 //this.element.style.left = cpos.left + container.offsetWidth - 185 + 'px';284 this.element.style.left = cpos.left + 530 + 'px';285 this.element.style.display = '';286 };287 this.hide = function() {288 if (HandleItem) {289 HandleItem.labelControl.className = HandleItem.getSelected() ? 'ti selected' : (HandleItem.isVirtualCopy ? 'ti virtual' : 'ti');290 if (HandleItem.isDefault) HandleItem.labelControl.className += ' main-page';291 this.element.style.display = 'none';292 HandleItem = null;293 }294 };295 // {main} draw toolbar296 if (typeof(Control) === 'object') {297 __draw();298 } else {299 alert('Can\'t create toolbar without control object');300 }...
TableToolbar.js
Source:TableToolbar.js
1/**2* TableToolbar3* use Prototype4*/5var TableToolbar = function(_oControl) {6 /** (Private properties) */7 var __self = this;8 var Control = _oControl;9 var HandleItem = null;10 var cDepth = parseInt(Control.container.style.zIndex) || 0;11 var IconsPath = Control.iconsPath;12 var DataSet = Control.dataSet;13 /** (Public properties) */14 this.highlight = null;15 this.element = null;16 this.buttons = new Array();17 /** (Private methods) */18 var __drawButtons = function() {19 var btns = document.createElement('div');20 btns.style.position = 'absolute';21 btns.style.display = 'none';22 btns.className = 'tree_toolbar';23 // fix for table24 btns.style.width = '140px';25 // del26 __appendButton(btns, {27 name: 'ico_del',28 hint: getLabel('js-delete'),29 init : function(button) {30 var e = button.element;31 var hint = getLabel('js-delete');32 if (HandleItem !== null && (HandleItem.permissions && HandleItem.permissions & 8 || !HandleItem.permissions)) {33 var icon = IconsPath + 'ico_del.png';34 e.setAttribute('title', hint);35 e.style.background = "url('" + icon + "') no-repeat";36 e.setAttribute('href', '#');37 e.style.visibility = 'visible';38 } else {39 e.style.visibility = 'hidden';40 }41 },42 release : function(button) {43 if (HandleItem !== null) {44 DataSet.execute('tree_delete_element', {45 'childs' : 1,46 'element' : HandleItem.id,47 'handle_item' : HandleItem48 });49 }50 return false;51 }52 });53 // edit54 __appendButton(btns, {55 name: 'ico_edit',56 hint: getLabel('js-edit-item'),57 init : function(button) {58 var e = button.element;59 if (HandleItem !== null && HandleItem.editLink !== false && (HandleItem.permissions && HandleItem.permissions & 2 || !HandleItem.permissions)) {60 var icon = IconsPath + 'ico_edit.png';61 var hint = getLabel('js-edit-item');62 var href = HandleItem.editLink;63 e.setAttribute('title', hint);64 e.style.background = "url('" + icon + "') no-repeat";65 e.setAttribute('href', href);66 e.style.visibility = 'visible';67 } else {68 e.style.visibility = 'hidden';69 }70 },71 release : function(button) {72 return true;73 }74 });75 if(!TableToolbar.disableAddButton) {76 // create77 __appendButton(btns, {78 name: 'ico_add',79 hint: getLabel('js-add-page'),80 init : function(button) {81 if (HandleItem !== null && HandleItem.createLink !== false) {82 button.element.setAttribute('href', HandleItem.createLink);83 button.element.style.visibility = 'visible';84 } else {85 button.element.style.visibility = 'hidden';86 }87 }88 });89 }90 // blocking91 __appendButton(btns, {92 name: 'blocking',93 hint: getLabel('js-disable-page'),94 icon: IconsPath + 'ico_block.png',95 init : function(button) {96 var _allow_activity = HandleItem.allowActivity && (_oControl.contentType=='pages' || (_oControl.contentType=='objects' && _oControl.enableObjectsActivity));97 if (HandleItem !== null && _allow_activity) {98 var icon = HandleItem.isActive ? IconsPath + 'ico_unblock.png' : IconsPath + 'ico_block.png';99 var hint = HandleItem.isActive ? getLabel('js-disable-page') : getLabel('js-enable-page');100 button.element.style.background = "url('" + icon + "') no-repeat";101 button.element.setAttribute('href', '#');102 button.element.setAttribute('title', hint);103 button.element.style.visibility = 'visible';104 } else {105 button.element.style.visibility = 'hidden';106 }107 },108 release : function(button) {109 if (HandleItem !== null) {110 var icon = HandleItem.isActive ? IconsPath + 'ico_block.png' : IconsPath + 'ico_unblock.png';111 var hint = HandleItem.isActive ? getLabel('js-enable-page') : getLabel('js-disable-page');112 button.element.setAttribute('title', hint);113 button.element.style.background = "url('" + icon + "') no-repeat";114 DataSet.execute('tree_set_activity', {115 'element' : HandleItem.id,116 'active' : HandleItem.isActive ? 0 : 1,117 'handle_item' : HandleItem,118 'selected_items' : HandleItem.control.selectedList,119 'object[]' : HandleItem.id,120 'viewMode': 'full'121 });122 }123 return false;124 }125 });126/*127 // virtual copy128 __appendButton(btns, {129 name: 'copy',130 hint: getLabel('js-vcopy-str'),131 init : function(button) {132 if (HandleItem !== null && HandleItem.allowCopy) {133 button.element.style.visibility = 'visible';134 } else {135 button.element.style.visibility = 'hidden';136 }137 },138 release : function(button) {139 if (HandleItem !== null) {140 HandleItem.className = 'ti virtual';141 if (HandleItem.isDefault) HandleItem.labelControl.className += ' main-page';142 HandleItem.isVirtualCopy = true;143 DataSet.execute('tree_copy_element', {144 'element' : HandleItem.id,145 'childs' : 1,146 'links' : 1,147 'virtuals' : 1,148 'permissions' : 1,149 'handle_item' : HandleItem,150 'copy_all' : 0151 });152 }153 return false;154 }155 });156 // real copy157 __appendButton(btns, {158 name: 'clone',159 hint: getLabel('js-copy-str'),160 init : function(button) {161 if (HandleItem !== null && HandleItem.allowCopy) {162 button.element.style.visibility = 'visible';163 } else {164 button.element.style.visibility = 'hidden';165 }166 },167 release : function(button) {168 if (HandleItem !== null) {169 DataSet.execute('tree_copy_element', {170 'element' : HandleItem.id,171 'childs' : 1,172 'links' : 1,173 'permissions' : 1,174 'handle_item' : HandleItem,175 'copy_all' : 0,176 'clone_mode' : 1177 });178 }179 return false;180 }181 });*/182 // view183 __appendButton(btns, {184 name: 'view',185 hint: getLabel('js-view-page'),186 init : function(button) {187 if (HandleItem !== null && HandleItem.getData().guide == "guide") {188 button.element.setAttribute('href', window.pre_lang + "/admin/data/guide_items/" + HandleItem.id + "/");189 button.element.style.visibility = 'visible';190 } else if (HandleItem !== null && HandleItem.viewLink !== false) {191 button.element.setAttribute('href', HandleItem.viewLink);192 button.element.style.visibility = 'visible';193 } else {194 button.element.style.visibility = 'hidden';195 }196 }197 });198 __self.element = document.body.appendChild(btns);199 };200 var __initButtons = function() {201 for (var i = 0; i < __self.buttons.length; i++) {202 __self.buttons[i].init(__self.buttons[i]);203 }204 };205 var __appendButton = function(container, options) {206 var b = document.createElement('a');207 var name = options.name || 'toolbtn';208 var href = options.href || '#';209 var icon = options.icon || IconsPath + name + ".png";210 var init = options.init || function() {};211 var title = options.hint || '';212 var el = container.appendChild(b);213 var button = {214 'name' : name,215 'href' : href,216 'icon': icon,217 'init' : init,218 'element' : el219 };220 __self.buttons[__self.buttons.length] = button;221 el.setAttribute('href', href);222 el.setAttribute('title', title);223 el.className = options.className || 'tree_toolbtn';224 if (typeof(options.release) === 'function') {225 el.onclick = function() {226 if (!DataSet.isAvailable()) return false;227 return options.release(button);228 };229 } else {230 el.onclick = function() {231 if (!DataSet.isAvailable()) return false;232 if(HandleItem.focus) HandleItem.focus();233 return true;234 };235 }236 237 el.name = name;238 el.style.background = "url('" + icon + "') no-repeat";239 };240 var __draw = function() {241 var el = document.createElement('div');242 el.className = 'tree-highlight';243 el.style.display = 'none';244 el.style.position = 'absolute';245 el.style.zIndex = cDepth - 1;246 __self.highlight = Control.container.appendChild(el);247 __drawButtons();248 };249 250 /** (Public methods) */251 this.show = function(_HandleItem, bForce) {252 bForce = bForce || false;253 // unselect old HandleItem254 if (typeof(_HandleItem) === 'undefined' || (HandleItem === _HandleItem && !bForce)) return false;255 256 if (HandleItem) HandleItem.labelControl.className = 'ti';257 HandleItem = _HandleItem;258 HandleItem.labelControl.className = HandleItem.getSelected() ? 'ti selected-highlight' : 'ti hightlight';259 //HandleItem.focus();260 __initButtons();261 //var iroot = HandleItem.control.getRootNodeId();262 var container = HandleItem.control.initContainer.parentNode;263 var cpos = jQuery(container).position();264 // some tables has extra column (toggle button)265 var index = 0;266 if(HandleItem.element.childNodes[index].className=='ti-toggle') {267 index = 1;268 }269 270 var itemName = jQuery(HandleItem.element.childNodes[index]);271 var posItemName = itemName.position();272 var posLeft = posItemName.left + itemName.width() + cpos.left - 7 - container.scrollLeft;273 this.element.style.top = HandleItem.position.top + cpos.top + 'px';274 //this.element.style.left = container.offsetWidth - 130 + 'px';275 this.element.style.left = posLeft + 'px';276 this.element.style.display = '';277 };278 this.hide = function() {279 if (HandleItem) {280 HandleItem.labelControl.className = HandleItem.getSelected() ? 'ti selected' : (HandleItem.isVirtualCopy ? 'ti virtual' : 'ti');281 this.element.style.display = 'none';282 HandleItem = null;283 }284 };285 // {main} draw toolbar286 if (typeof(Control) === 'object') {287 __draw();288 } else {289 alert('Can\'t create toolbar without control object');290 }291};...
Using AI Code Generation
1import { handleItem } from 'ng-mocks';2import { ItemComponent } from './item.component';3describe('ItemComponent', () => {4 it('should render the title', () => {5 const fixture = handleItem(ItemComponent, {6 });7 expect(fixture.nativeElement.querySelector('h1').textContent).toContain(8 );9 });10});11import { Component, Input } from '@angular/core';12@Component({13 template: `<h1>{{title}}</h1>`,14})15export class ItemComponent {16 @Input() title: string;17}18import { handleItem } from 'ng-mocks';19import { ItemComponent } from './item.component';20describe('ItemComponent', () => {21 it('should render the title', () => {22 const fixture = handleItem(ItemComponent, {23 });24 expect(fixture.nativeElement.querySelector('h1').textContent).toContain(25 );26 });27});28import { Component, Input } from '@angular/core';29@Component({30 template: `<h1>{{title}}</h1>`,31})32export class ItemComponent {33 @Input() title: string;34}
Using AI Code Generation
1import { handleItem } from 'ng-mocks';2const fixture = TestBed.createComponent(MyComponent);3const instance = fixture.componentInstance;4const handle = handleItem(instance, 'myMethod');5handle('test');6expect(handle.calls.mostRecent().args[0]).toBe('test');7import { handleItem } from 'ng-mocks';8export class MyComponent {9 public myMethod = (value: string) => {10 expect(value).toBe('test');11 }12}13const instance = new MyComponent();14const handle = handleItem(instance, 'myMethod');15handle('test');16expect(handle.calls.mostRecent().args[0]).toBe('test');
Using AI Code Generation
1import { handleItem } from 'ng-mocks';2describe('test', () => {3 it('test', () => {4 const item = handleItem('test.html', {5 });6 expect(item).toBeDefined();7 });8});9import { Component, OnInit, Input } from '@angular/core';10@Component({11})12export class TestComponent implements OnInit {13 @Input() test: string;14 constructor() { }15 ngOnInit(): void {16 }17}18import { TestBed } from '@angular/core/testing';19import { TestComponent } from './test.component';20import { MockComponent } from 'ng-mocks';21describe('TestComponent', () => {22 let component: TestComponent;23 beforeEach(() => {24 TestBed.configureTestingModule({25 declarations: [TestComponent, MockComponent(TestComponent)],26 }).compileComponents();27 component = TestBed.createComponent(TestComponent).componentInstance;28 });29 it('should create', () => {30 expect(component).toBeTruthy();31 });32 it('should have test', () => {33 expect(component.test).toBeDefined();34 });35});36import { Component, OnInit, Input } from '@angular/core';37@Component({38})39export class TestComponent implements OnInit {40 @Input() test: string;41 constructor() { }42 ngOnInit(): void {43 }44}45import { TestBed } from '@angular/core/testing';46import { TestComponent } from './test.component';47import { MockComponent } from 'ng-mocks';48describe('TestComponent',
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!!