Best JavaScript code snippet using wpt
reference.view.js
Source:reference.view.js
1/* Copyright (c) 2016-2017 OpenText Corp. All Rights Reserved. */2define(['csui/lib/underscore', 'csui/lib/jquery', 'csui/lib/backbone', 'csui/lib/marionette',3 'csui/controls/form/impl/fields/csformfield.view',4 'csui/utils/url',5 'csui/controls/form/impl/fields/csformfield.editable.behavior',6 'csui/dialogs/modal.alert/modal.alert',7 'csui/controls/form/pub.sub',8 'conws/controls/form/fields/referencefield.states.behavior',9 'hbs!conws/controls/form/impl/fields/reference/reference',10 'i18n!conws/controls/form/impl/nls/lang',11 'css!conws/controls/form/impl/fields/reference/reference'12], function (_, $, Backbone, Marionette, FormFieldView, Url,13 FormFieldEditable2Behavior, ModalAlert, PubSub, ReferenceFieldStatesBehavior, template, lang) {14 "use strict";15 var ReferenceFieldView = FormFieldView.extend({16 constructor: function ReferenceFieldView(options) {17 FormFieldView.apply(this, arguments);18 this.create = this.options.mode === "create" ? true : false;19 this.catId = this.options.dataId.split("_")[0];20 this.formView = this.options.alpacaField.connector.config.formView;21 this.node = !!this.formView.node ? this.formView.node:this.formView.model.node;22 this.connector = this.node.connector;23 this.generatereference = false;24 this.regenerateReference = false;25 this.parentsAttrDataChanged = false;26 this.parentattrIds = [];27 this.parentsattrData = [];28 this.canRegenerateReference = this.alpacaField.schema.canRegenerateReference;29 this.rmClassificationUsed = this.alpacaField.schema.rmClassificationUsed;30 this.rmClassificationsAttached = true;31 if (!!this.alpacaField.schema.oldReferenceAttributeID) {32 this.oldReferenceAttributeID = this.alpacaField.schema.oldReferenceAttributeID;33 }34 if( this.create ){35 this.listenTo(this.formView, 'invalid:field', this.invalidFieldChange );36 }37 var attr;38 this.hasValue = this.alpacaField.data === null || this.alpacaField.data === undefined ||39 this.alpacaField.data === "" ? false : true;40 if(!!this.alpacaField.schema.depattrlist) {41 _.each(this.options.alpaca.schema.depattrlist, function (parent) {42 this.parentattrIds.push(parent);43 attr = {id: parent, data: this.getAttrData(parent)};44 this.parentsattrData.push(attr);45 if( this.create ) {46 this.listenTo(PubSub, parent + 'dependentattrchanged', this.parentChange);47 }48 49 }, this);50 }51 if (this.parentattrIds.length > 0) {52 this.checkReadyForGenerateReference();53 } else {54 if (!this.hasValue) {55 this.generatereference = true;56 }57 }58 if (this.create && this.rmClassificationUsed && this.formView.model.collection) {59 this.listenTo(this.formView.model.collection, 'add',60 this.rmClassificationChange);61 this.listenTo(this.formView.model.collection, 'remove',62 this.rmClassificationChange);63 if (!this.formView.model.collection.get("recordsDetails")) {64 this.rmClassificationsAttached = false;65 this.generatereference = false;66 this.regenerateReference = false;67 }68 }69 if( this.create === false ) {70 this.regenerateReference = true;71 }72 this.idBtnLabel = this.options.labelId ? this.options.labelId : "";73 this.refDataId = _.uniqueId('referenceData');74 this.depFieldsText = this.getDependencyInlineText();75 this.btnDesc = this.alpacaField.options.label + ", " + (this.hasValue ? lang.reGenRefNumber : lang.genRefNumber) + ", " + (this.canRegenerateReference ? this.depFieldsText : "");76 },77 className: 'cs-formfield cs-referencefield',78 template: template,79 templateHelpers: function () {80 return {81 create: this.create,82 generateReference: this.generatereference,83 data: this.curVal,84 hasValue: this.hasValue,85 readOnly: this.alpacaField.schema.readonly,86 depInlineText: this.depFieldsText,87 id: this.model && this.model.get('id') ? this.model.get('id') : _.uniqueId("button"),88 genRefNumber: lang.genRefNumber,89 reGenRefNumber: lang.reGenRefNumber,90 canRegenerateReference : this.canRegenerateReference,91 idBtnLabel: this.idBtnLabel,92 refDataId: this.refDataId,93 btnDesc: this.btnDesc94 };95 },96 behaviors: {97 FormFieldEditable: {98 behaviorClass: FormFieldEditable2Behavior99 },100 FormFieldStates: {101 behaviorClass: ReferenceFieldStatesBehavior102 }103 },104 events: {105 'click button.reference-generate-number': 'generateReferenceNumber'106 },107 getDependencyInlineText: function () {108 var dependencyInlineText = '';109 var rmClassName = '';110 var rmClassData;111 if ( this.rmClassificationUsed && this.rmClassificationsAttached && this.formView.model.collection ) {112 if ( this.formView.model.collection.get("recordsDetails").options.className ) {113 rmClassName = this.formView.model.collection.get("recordsDetails").options.className;114 } else if( this.formView.model.collection.get("recordsDetails").get("data")) {115 rmClassData = this.formView.model.collection.get("recordsDetails").get("data");116 if ( rmClassData.name ) {117 rmClassName = this.formView.model.collection.get("recordsDetails").get("data").name;118 } else if ( rmClassData.inherited_rmclassification_name ) {119 rmClassName = rmClassData.inherited_rmclassification_name[0];120 }121 }122 }123 if (this.parentattrIds.length > 0) {124 dependencyInlineText = lang.depFieldsText + " " + this.alpacaField.schema.depattrtext;125 if (this.rmClassificationUsed) {126 dependencyInlineText = dependencyInlineText + ", " + lang.rmClassificationText;127 if (this.rmClassificationsAttached) {128 dependencyInlineText = dependencyInlineText + ". " + lang.rmClassificationNameText + " " +129 rmClassName;130 }131 }132 } else if (this.rmClassificationUsed) {133 dependencyInlineText = lang.depFieldsText + " " + lang.rmClassificationText;134 if (this.rmClassificationsAttached) {135 dependencyInlineText = dependencyInlineText + ". " + lang.rmClassificationNameText + " " +136 rmClassName;137 }138 } else {139 dependencyInlineText = lang.independentFieldsText;140 }141 return dependencyInlineText;142 },143 generateReferenceNumber: function (event) {144 if (this.$el.find('button.reference-generate-number').attr('aria-disabled') === 'true') {145 return event.preventDefault();146 }147 var fullUrl;148 var connector = this.node.connector;149 var url = this.getV2Url(connector.connection.url);150 var self = this;151 var formData = this.getFormData();152 fullUrl = Url.combine(153 url + '/refnumbers');154 var ajaxOptions = {155 type: 'POST',156 url: fullUrl,157 data: formData,158 success: function (response, status, jXHR) {159 if (response.results.errMsg) {160 ModalAlert.showError(response.results.errMsg);161 }162 else {163 if (self.regenerateReference && !!self.oldReferenceAttributeID) {164 var oldReferenceElement = self.getAttrElement(self.oldReferenceAttributeID);165 if ( typeof oldReferenceElement !== 'undefined' ) {166 oldReferenceElement.setValue(self.curVal);167 oldReferenceElement.refresh();168 }169 }170 self.options.alpacaField.setValue(response.results.referenceNumber);171 self.hasValue = true;172 self.curVal = self.options.alpacaField.getValue();173 self.generatereference = false;174 if (self.create === false) {175 self.regenerateReference = true;176 }177 self.parentsAttrDataChanged = false;178 if (!self.create) {179 var ele = self.$el.find('.reference-field .btn-container button');180 ele.find('span').text(self.curVal);181 ele.focus();182 } else {183 if (self.render(self.$el.find('.reference-field-data button').length > 0)) {184 self.$el.find('.reference-field-data button').focus();185 }186 }187 if (!self.create && self.node.get("type") === 848) {188 if (self.formView.options.metadataView) {189 self.formView.options.metadataView.metadataHeaderView.metadataItemNameView.model.set(190 "name", response.results.nodeName);191 } else {192 self.formView.options.node.set("name", response.results.nodeName)193 }194 }195 }196 },197 error: function (xhr, status, text) {198 var errorContent = xhr.responseJSON ?199 ( xhr.responseJSON.errorDetail ? xhr.responseJSON.errorDetail :200 xhr.responseJSON.error) : lang.defaultErrorGenerateNumber;201 ModalAlert.showError(errorContent);202 }203 };204 connector.extendAjaxOptions(ajaxOptions);205 connector.makeAjaxCall(ajaxOptions);206 },207 getV2Url: function (url) {208 return url.replace('/v1', '/v2');209 },210 invalidFieldChange: function(event) {211 var fieldId = document.getElementById(event.target.id).closest('.alpaca-container-item').getAttribute('data-alpaca-container-item-name');212 var self = this;213 _.each(this.options.alpaca.schema.depattrlist, function (depattr) {214 if (depattr === fieldId) {215 _.each(self.parentsattrData, function (element) {216 if (element.id === fieldId) {217 element.data = null;218 }219 });220 }221 });222 var beforeChange = this.generatereference;223 this.checkReadyForGenerateReference();224 if (this.create) {225 this.regenerateReference = false;226 } else if (!this.hasValue) {227 this.generatereference = true;228 } else {229 this.generatereference = false;230 }231 if (beforeChange !== this.generatereference) {232 if (this.generatereference) {233 this.$el.find('button.reference-generate-number').removeClass('reference-generate-number-disabled').attr('aria-disabled', 'false');234 } else {235 this.$el.find('.reference-field-generate').addClass('cursor-disabled');236 this.$el.find('button.reference-generate-number').addClass('reference-generate-number-disabled').attr('aria-disabled', 'true');237 }238 }239 },240 parentChange: function (parent) {241 var that = parent;242 var beforeParentChange = this.generatereference;243 var afterParentChange;244 if (that !== this) {245 this.insertParentAttrData(parent);246 this.checkReadyForGenerateReference();247 if (this.create) {248 this.regenerateReference = false;249 } else if (!this.hasValue) {250 this.generatereference = true;251 } else {252 this.generatereference = false;253 }254 afterParentChange = this.generatereference;255 if ( beforeParentChange !== afterParentChange ) {256 if( this.generatereference ) {257 this.$el.find('button.reference-generate-number').removeClass('reference-generate-number-disabled').attr('aria-disabled', 'false');258 }else {259 this.$el.find('.reference-field-generate').addClass('cursor-disabled');260 this.$el.find('button.reference-generate-number').addClass('reference-generate-number-disabled').attr('aria-disabled', 'true');261 }262 }263 }264 },265 rmClassificationChange: function () {266 var beforeParentChange = this.generatereference;267 var afterParentChange;268 if (this.formView.model.collection && this.formView.model.collection.get("recordsDetails")) {269 this.rmClassificationsAttached = true;270 if (this.create) {271 this.generatereference = true;272 } else {273 this.regenerateReference = true;274 }275 } else {276 this.rmClassificationsAttached = false;277 this.generatereference = false;278 this.regenerateReference = false;279 }280 this.checkReadyForGenerateReference();281 var depFieldsText = this.getDependencyInlineText();282 var btnDesc = this.alpacaField.options.label + ", " + (this.hasValue ? lang.reGenRefNumber : lang.genRefNumber) + ", " 283 + (this.canRegenerateReference ? depFieldsText : "");284 this.$el.find('.reference-field .reference-field-generate-read .reference-field-generate-text').text(depFieldsText);285 this.$el.find('button.reference-generate-number').attr('aria-label',btnDesc);286 afterParentChange = this.generatereference;287 if ( beforeParentChange !== afterParentChange ) {288 if( this.generatereference ) {289 this.$el.find('.reference-field-generate').removeClass('cursor-disabled');290 this.$el.find('button.reference-generate-number').removeClass('reference-generate-number-disabled').attr('aria-disabled', 'false');291 }else {292 this.$el.find('.reference-field-generate').addClass('cursor-disabled');293 this.$el.find('button.reference-generate-number').addClass('reference-generate-number-disabled').attr('aria-disabled', 'true');294 }295 }296 },297 checkReadyForGenerateReference: function () {298 var count = 0;299 _.each(this.parentsattrData, function (attr) {300 if (attr.data !== undefined && attr.data !== null && attr.data !== "") {301 count++;302 }303 });304 if (count === this.parentsattrData.length) {305 if (this.create && this.hasValue) {306 this.generatereference = false;307 this.regenerateReference = false;308 } else {309 if (this.rmClassificationsAttached) {310 if (this.create) {311 this.generatereference = true;312 this.regenerateReference = false;313 }314 else if (this.parentsAttrDataChanged) {315 this.regenerateReference = true;316 } else {317 this.generatereference = true;318 }319 } else {320 this.generatereference = false;321 this.regenerateReference = false;322 }323 }324 }325 else {326 this.generatereference = false;327 this.regenerateReference = false;328 }329 },330 insertParentAttrData: function (parent) {331 var self = this;332 var parentData = parent.curVal;333 _.each(self.parentsattrData, function (attr) {334 if (attr.id === parent.options.dataId) {335 if (typeof parent.curVal === 'object') {336 parentData = parent.curVal.id;337 }338 if (String(attr.data) !== parentData) {339 attr.data = parentData;340 self.generatereference = true;341 self.regenerateReference = true;342 self.parentsAttrDataChanged = true;343 }344 }345 });346 },347 getAttrData: function (field) {348 var attrData, setData, setID, attrID;349 var str = field.split("_");350 var catID = str[0];351 var i = 0;352 if (str.length > 3) {353 if (str.length === 6) {354 setID = str[0] + "_" + str[1];355 setData = this.formView.alpaca.data[setID];356 attrID = field.replace(setID + "_", "");357 if (typeof setData !== 'undefined') {358 attrData = setData[attrID];359 }360 else {361 if (this.formView.metadataview.formViewList.length > 1) {362 for (i = 0; i < this.formView.metadataview.formViewList.length; i++) {363 if (typeof this.formView.metadataview.formViewList[i].alpaca.data[attrID] !==364 'undefined') {365 attrData = this.formView.metadataview.formViewList[i].alpaca.data[attrID];366 break;367 }368 }369 }370 }371 } else if (str.length === 7) {372 if (str[2] === catID) {373 setID = str[0] + "_" + str[1];374 setData = this.formView.alpaca.data[setID];375 attrID = str[2] + "_" + str[3] + "_" + str[4] + "_" + str[5];376 attrData = setData[attrID][0];377 } else {378 setID = str[0] + "_" + str[1];379 setData = this.formView.alpaca.data[setID][0];380 attrID = str[3] + "_" + str[4] + "_" + str[5] + "_" + str[6];381 attrData = setData[attrID];382 }383 } else {384 setID = str[0] + "_" + str[1];385 setData = this.formView.alpaca.data[setID][0];386 attrID = str[3] + "_" + str[4] + "_" + str[5] + "_" + str[6];387 attrData = setData[attrID][0];388 }389 }390 else {391 field = str[0] + "_" + str[1];392 attrData = this.formView.alpaca.data[field];393 if (typeof attrData === 'undefined' && this.formView.metadataview.formViewList.length > 1) {394 for (i = 0; i < this.formView.metadataview.formViewList.length; i++) {395 if (typeof this.formView.metadataview.formViewList[i].alpaca.data[field] !==396 'undefined') {397 attrData = this.formView.metadataview.formViewList[i].alpaca.data[field];398 break;399 }400 }401 }402 if (str.length === 3) {403 attrData = attrData[0];404 }405 }406 return attrData;407 },408 getAttrElement: function (field) {409 var element, setID, attrID;410 var str = field.split("_");411 var catID = str[0];412 var i = 0;413 if (str.length > 3) {414 if (str.length === 6) {415 setID = str[0] + "_" + str[1];416 element = this.formView.$el.alpaca().childrenByPropertyId[setID];417 attrID = field.replace(setID + "_", "");418 if (typeof element !== 'undefined') {419 element = element.childrenByPropertyId[attrID];420 }421 else {422 if (this.formView.metadataview.formViewList.length > 1) {423 for (i = 0; i < this.formView.metadataview.formViewList.length; i++) {424 if (typeof this.formView.metadataview.formViewList[i].$el.alpaca().childrenByPropertyId[attrID] !==425 'undefined') {426 element = this.formView.metadataview.formViewList[i].$el.alpaca().childrenByPropertyId[attrID];427 break;428 }429 }430 }431 }432 } else if (str.length === 7) {433 if (str[2] === catID) {434 setID = str[0] + "_" + str[1];435 element = this.formView.$el.alpaca().childrenByPropertyId[setID];436 attrID = str[2] + "_" + str[3] + "_" + str[4] + "_" + str[5];437 element = element.childrenByPropertyId[attrID].children[0];438 } else {439 setID = str[0] + "_" + str[1];440 element = this.formView.$el.alpaca().childrenByPropertyId[setID].children[0];441 attrID = str[3] + "_" + str[4] + "_" + str[5] + "_" + str[6];442 element = element.childrenByPropertyId[attrID];443 }444 } else {445 setID = str[0] + "_" + str[1];446 element = this.formView.$el.alpaca().childrenByPropertyId[setID].children[0];447 attrID = str[3] + "_" + str[4] + "_" + str[5] + "_" + str[6];448 element = element.childrenByPropertyId[attrID].children[0];449 }450 }451 else {452 field = str[0] + "_" + str[1];453 element = this.formView.$el.alpaca().childrenByPropertyId[field];454 if (typeof element === 'undefined' && this.formView.metadataview.formViewList.length > 1) {455 for (i = 0; i < this.formView.metadataview.formViewList.length; i++) {456 if (typeof this.formView.metadataview.formViewList[i].$el.alpaca().childrenByPropertyId[field] !==457 'undefined') {458 element = this.formView.metadataview.formViewList[i].$el.alpaca().childrenByPropertyId[field];459 break;460 }461 }462 }463 if (str.length === 3) {464 element = element.children[0];465 }466 }467 return element;468 },469 allowEditOnClickReadArea: function () {470 return false;471 },472 getFormData: function () {473 var urlParameters = "";474 var formData = {};475 var create = this.create;476 var node_id = this.node.attributes.id;477 var parent_id = this.node.attributes.parent_id;478 var category_id = this.catId;479 var attribute_keys ="";480 var attribute_values = "";481 var count = 0;482 var cache_id = this.options.alpaca.schema.cacheId;483 var rmClassData;484 if (create !== undefined) {485 formData.create = create;486 }487 if (!create) {488 if (node_id !== undefined) {489 formData.node_id = node_id;490 }491 return formData;492 }493 if (cache_id !== undefined) {494 formData.cache_id = cache_id;495 }496 if (parent_id !== undefined) {497 if (typeof parent_id === 'object') {498 parent_id = parent_id.id;499 }500 formData.parent_id = parent_id;501 }502 if (category_id !== undefined) {503 formData.category_id = category_id;504 }505 if (this.formView.model.collection.get("recordsDetails")) {506 rmClassData = this.formView.model.collection.get("recordsDetails").get("data")507 if( rmClassData.rmclassification_id && rmClassData.rmclassification_id[0] !== 0 ) {508 formData.rm_class_ids = rmClassData.rmclassification_id[0];509 } else if( rmClassData.inherited_rmclassification_id ){510 formData.rm_class_ids = rmClassData.inherited_rmclassification_id[0];511 }512 } 513 if (this.parentattrIds.length > 0) {514 var length = this.parentsattrData.length;515 _.each(this.parentsattrData, function (attr) {516 count++;517 if (count !== length) {518 attribute_keys = "" + attribute_keys + "'" + attr.id + "',";519 attribute_values = "" + attribute_values + "'" + attr.data + "',";520 } else {521 attribute_keys = attribute_keys + "'" + attr.id + "'";522 attribute_values = attribute_values + "'" + attr.data + "'";523 }524 });525 if (attribute_values !== undefined && attribute_keys !== undefined ) {526 formData.attribute_keys = attribute_keys.toString();527 formData.attribute_values = attribute_values.toString();528 }529 }530 return formData;531 }532 });533 return ReferenceFieldView;534})...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var url = 'www.google.com';4wpt.generateReference(url, function(err, data) {5 if (err) {6 console.log(err);7 } else {8 console.log(data);9 }10});11var wpt = require('webpagetest');12var wpt = new WebPageTest('www.webpagetest.org');13var url = 'www.google.com';14var options = {15};16wpt.runTest(url, options, function(err, data) {17 if (err) {18 console.log(err);19 } else {20 console.log(data);21 }22});23var wpt = require('webpagetest');24var wpt = new WebPageTest('www.webpagetest.org');25var testId = '170710_1V_1e8c';26wpt.getTestResults(testId, function(err, data) {27 if (err) {28 console.log(err);29 } else {30 console.log(data);31 }32});33var wpt = require('webpagetest');34var wpt = new WebPageTest('www.webpagetest.org');35var testId = '170710_1V_1e8c';36wpt.getTestStatus(testId, function(err, data) {37 if (err) {
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3var options = {4};5wpt.runTest(options, function(err, data) {6 if (err) return console.error(err);7 console.log('Test submitted successfully. View your test at: %s%s', wpt.testUrl, data.data.testId);8});9var wpt = require('webpagetest');10var wpt = new WebPageTest('www.webpagetest.org');11var testId = '170103_7F_6c0e6f8a2e1f5d5e9d5e9e5f5e5d5e5';12wpt.getTestResults(testId, function(err, data) {13 if (err) return console.error(err);14 console.log(data);15});16var wpt = require('webpagetest');17var wpt = new WebPageTest('www.webpagetest.org');18wpt.getLocations(function(err, data) {19 if (err) return console.error(err);20 console.log(data);21});22var wpt = require('webpagetest');23var wpt = new WebPageTest('www.webpagetest.org');24wpt.getTesters(function(err, data) {25 if (err) return console.error(err);26 console.log(data);27});28var wpt = require('webpagetest');29var wpt = new WebPageTest('www.webpagetest.org');30wpt.getTesters(function(err, data) {31 if (err) return console.error(err);32 console.log(data);33});34var wpt = require('webpagetest');35var wpt = new WebPageTest('www.webpagetest.org');36wpt.getTesters(function(err, data) {37 if (err) return console.error(err);38 console.log(data);39});
Using AI Code Generation
1var wpt = require('wpt-api');2var options = {3};4wpt.generateReference(options, function (err, data) {5 if (err) {6 console.log('Error: ', err);7 } else {8 console.log('Data: ', data);9 }10});11var wpt = require('wpt-api');12var options = {13};14wpt.generateReference(options, function (err, data) {15 if (err) {16 console.log('Error: ', err);17 } else {18 console.log('Data: ', data);19 }20});
Using AI Code Generation
1var wptools = require('wptools');2var page = wptools.page('Albert Einstein');3page.generateReference(function(err, response){4 console.log(response);5});6{7 "references": {8 {9 },10 {11 },12 {
Using AI Code Generation
1var wptools = require('wptools');2wptools.generateReference('en.wikipedia.org/wiki/Barack_Obama', function(err, resp) {3 console.log(resp);4});5{ text: 'Barack Obama',6var wptools = require('wptools');7wptools.generateReference('fr.wikipedia.org/wiki/Barack_Obama', function(err, resp) {8 console.log(resp);9});10{ text: 'Barack Obama',
Using AI Code Generation
1var wpt = require('wpt-api');2generateReference: function(url, key, location, label) {3 var options = {4 qs: {5 }6 };7 request(options, function(err, res, body) {8 if (err) {9 console.log(err);10 } else {11 console.log(body);12 }13 });14}15{statusCode: 200, headers: {…}, body: "OK", request: {…}}16request(options, function(err, res, body) {17 if (err) {18 console.log(err);19 } else {20 console.log(res.headers['x-test-id']);21 }22});23{statusCode: 200, headers: {…}, body: "OK", request: {…}}24request(options, function(err, res, body) {25 if (err) {26 console.log(err);27 } else {28 console.log(res.headers['x-test-id']);29 }30});31{
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!!