mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
refactor: replace var usage with let/const (#7064)
This commit is contained in:
20
tns-core-modules/ui/builder/binding-builder.d.ts
vendored
20
tns-core-modules/ui/builder/binding-builder.d.ts
vendored
@@ -5,17 +5,17 @@
|
||||
|
||||
//@private
|
||||
export module bindingConstants {
|
||||
export var sourceProperty: string;
|
||||
export var targetProperty: string;
|
||||
export var expression: string;
|
||||
export var twoWay: string;
|
||||
export var source: string;
|
||||
export var bindingValueKey: string;
|
||||
export var parentValueKey: string;
|
||||
export var parentsValueKey: string;
|
||||
export var newPropertyValueKey: string;
|
||||
export const sourceProperty: string;
|
||||
export const targetProperty: string;
|
||||
export const expression: string;
|
||||
export const twoWay: string;
|
||||
export const source: string;
|
||||
export const bindingValueKey: string;
|
||||
export const parentValueKey: string;
|
||||
export const parentsValueKey: string;
|
||||
export const newPropertyValueKey: string;
|
||||
}
|
||||
|
||||
export function getBindingOptions(name: string, value: string): any;
|
||||
|
||||
export var parentsRegex: RegExp;
|
||||
export const parentsRegex: RegExp;
|
||||
|
||||
@@ -1,54 +1,55 @@
|
||||
// regex that contains all symbols applicable for expression used to AI detect an expression.
|
||||
var expressionSymbolsRegex = /[\+\-\*\/%\?:<>=!\|&\(\)^~]/;
|
||||
const expressionSymbolsRegex = /[\+\-\*\/%\?:<>=!\|&\(\)^~]/;
|
||||
|
||||
export module bindingConstants {
|
||||
export var sourceProperty = "sourceProperty";
|
||||
export var targetProperty = "targetProperty";
|
||||
export var expression = "expression";
|
||||
export var twoWay = "twoWay";
|
||||
export var source = "source";
|
||||
export var bindingValueKey = "$value";
|
||||
export var parentValueKey = "$parent";
|
||||
export var parentsValueKey = "$parents";
|
||||
export var newPropertyValueKey = "$newPropertyValue";
|
||||
export const sourceProperty = "sourceProperty";
|
||||
export const targetProperty = "targetProperty";
|
||||
export const expression = "expression";
|
||||
export const twoWay = "twoWay";
|
||||
export const source = "source";
|
||||
export const bindingValueKey = "$value";
|
||||
export const parentValueKey = "$parent";
|
||||
export const parentsValueKey = "$parents";
|
||||
export const newPropertyValueKey = "$newPropertyValue";
|
||||
};
|
||||
|
||||
var hasEqualSignRegex = /=+/;
|
||||
var equalSignComparisionOperatorsRegex = /(==|===|>=|<=|!=|!==)/;
|
||||
const hasEqualSignRegex = /=+/;
|
||||
const equalSignComparisionOperatorsRegex = /(==|===|>=|<=|!=|!==)/;
|
||||
|
||||
// this regex is used to search for all instaces of '$parents[]' within an expression
|
||||
export var parentsRegex = /\$parents\s*\[\s*(['"]*)\w*\1\s*\]/g;
|
||||
export const parentsRegex = /\$parents\s*\[\s*(['"]*)\w*\1\s*\]/g;
|
||||
|
||||
function isNamedParam(value) {
|
||||
var equalSignIndex = value.search(hasEqualSignRegex);
|
||||
const equalSignIndex = value.search(hasEqualSignRegex);
|
||||
if (equalSignIndex > -1) {
|
||||
var equalSignSurround = value.substr(equalSignIndex > 0 ? equalSignIndex - 1 : 0, 3);
|
||||
const equalSignSurround = value.substr(equalSignIndex > 0 ? equalSignIndex - 1 : 0, 3);
|
||||
if (equalSignSurround.search(equalSignComparisionOperatorsRegex) === -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function areNamedParams(params: Array<any>) {
|
||||
var i;
|
||||
for (i = 0; i < params.length; i++) {
|
||||
for (let i = 0; i < params.length; i++) {
|
||||
if (isNamedParam(params[i])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
var namedParamConstants = {
|
||||
const namedParamConstants = {
|
||||
propName: "propName",
|
||||
propValue: "propValue"
|
||||
}
|
||||
|
||||
function getPropertyNameValuePair(param, knownOptions, callback) {
|
||||
var nameValuePair = {};
|
||||
var propertyName = param.substr(0, param.indexOf("=")).trim();
|
||||
var propertyValue = param.substr(param.indexOf("=") + 1).trim();
|
||||
let nameValuePair = {};
|
||||
let propertyName = param.substr(0, param.indexOf("=")).trim();
|
||||
const propertyValue = param.substr(param.indexOf("=") + 1).trim();
|
||||
if (knownOptions) {
|
||||
if (!propertyName) {
|
||||
propertyName = knownOptions.defaultProperty;
|
||||
@@ -73,25 +74,26 @@ function getPropertyNameValuePair(param, knownOptions, callback) {
|
||||
}
|
||||
|
||||
function parseNamedProperties(parameterList, knownOptions, callback) {
|
||||
var result = {};
|
||||
var i;
|
||||
for (i = 0; i < parameterList.length; i++) {
|
||||
var nameValuePair = getPropertyNameValuePair(parameterList[i], knownOptions, callback);
|
||||
const result = {};
|
||||
let nameValuePair;
|
||||
for (let i = 0; i < parameterList.length; i++) {
|
||||
nameValuePair = getPropertyNameValuePair(parameterList[i], knownOptions, callback);
|
||||
if (nameValuePair) {
|
||||
result[nameValuePair[namedParamConstants.propName]] = nameValuePair[namedParamConstants.propValue];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function getParamsArray(value: string) {
|
||||
var result = [];
|
||||
var i;
|
||||
var skipComma = 0;
|
||||
var indexReached = 0;
|
||||
var singleQuoteBlock, doubleQuoteBlock = false;
|
||||
const result = [];
|
||||
let skipComma = 0;
|
||||
let indexReached = 0;
|
||||
let singleQuoteBlock = false;
|
||||
let doubleQuoteBlock = false;
|
||||
|
||||
for (i = 0; i < value.length; i++) {
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
if (value[i] === "\"") {
|
||||
doubleQuoteBlock = !doubleQuoteBlock;
|
||||
}
|
||||
@@ -121,26 +123,28 @@ function getParamsArray(value: string) {
|
||||
|
||||
function isExpression(expression: string): boolean {
|
||||
if (expression.search(expressionSymbolsRegex) > -1) {
|
||||
var parentsMatches = expression.match(parentsRegex);
|
||||
const parentsMatches = expression.match(parentsRegex);
|
||||
if (parentsMatches) {
|
||||
var restOfExpression = expression.substr(expression.indexOf(parentsMatches[0]) + parentsMatches[0].length);
|
||||
const restOfExpression = expression.substr(expression.indexOf(parentsMatches[0]) + parentsMatches[0].length);
|
||||
// no more expression regognition symbols so it is safe for sourceProperty
|
||||
if (!(restOfExpression.search(expressionSymbolsRegex) > -1)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function getBindingOptions(name: string, value: string): any {
|
||||
var namedParams = [];
|
||||
var params = getParamsArray(value);
|
||||
let namedParams = [];
|
||||
const params = getParamsArray(value);
|
||||
if (!areNamedParams(params)) {
|
||||
if (params.length === 1) {
|
||||
var trimmedValue = params[0].trim();
|
||||
var sourceProp;
|
||||
const trimmedValue = params[0].trim();
|
||||
let sourceProp;
|
||||
if (isExpression(trimmedValue)) {
|
||||
sourceProp = bindingConstants.bindingValueKey;
|
||||
namedParams.push(bindingConstants.expression + " = " + trimmedValue);
|
||||
@@ -155,7 +159,7 @@ export function getBindingOptions(name: string, value: string): any {
|
||||
else {
|
||||
namedParams.push(bindingConstants.sourceProperty + " = " + params[0].trim());
|
||||
namedParams.push(bindingConstants.expression + " = " + params[1].trim());
|
||||
var twoWay = params[2] ? params[2].toLowerCase().trim() === "true" : true;
|
||||
const twoWay = params[2] ? params[2].toLowerCase().trim() === "true" : true;
|
||||
namedParams.push(bindingConstants.twoWay + " = " + twoWay);
|
||||
}
|
||||
}
|
||||
@@ -163,8 +167,8 @@ export function getBindingOptions(name: string, value: string): any {
|
||||
namedParams = params;
|
||||
}
|
||||
|
||||
var bindingPropertyHandler = function (prop, value) {
|
||||
var result = {};
|
||||
const bindingPropertyHandler = function (prop, value) {
|
||||
const result = {};
|
||||
result[namedParamConstants.propName] = prop;
|
||||
if (prop === bindingConstants.twoWay) {
|
||||
// create a real boolean value
|
||||
@@ -182,12 +186,12 @@ export function getBindingOptions(name: string, value: string): any {
|
||||
return result;
|
||||
};
|
||||
|
||||
var bindingOptionsParameters = parseNamedProperties(namedParams, xmlBindingProperties, bindingPropertyHandler);
|
||||
var bindOptions = {
|
||||
const bindingOptionsParameters = parseNamedProperties(namedParams, xmlBindingProperties, bindingPropertyHandler);
|
||||
const bindOptions = {
|
||||
targetProperty: name
|
||||
};
|
||||
|
||||
for (var prop in bindingOptionsParameters) {
|
||||
for (let prop in bindingOptionsParameters) {
|
||||
if (bindingOptionsParameters.hasOwnProperty(prop)) {
|
||||
bindOptions[prop] = bindingOptionsParameters[prop];
|
||||
}
|
||||
@@ -200,10 +204,10 @@ export function getBindingOptions(name: string, value: string): any {
|
||||
return bindOptions;
|
||||
}
|
||||
|
||||
var xmlBindingProperties = {
|
||||
const xmlBindingProperties = {
|
||||
sourceProperty: true,
|
||||
expression: true,
|
||||
twoWay: true,
|
||||
source: true,
|
||||
defaultProperty: bindingConstants.sourceProperty
|
||||
};
|
||||
};
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { LoadOptions } from ".";
|
||||
import { View, ViewBase, Template, KeyedTemplate } from "../core/view";
|
||||
import { ViewEntry } from "../frame";
|
||||
import * as traceModule from "../../trace";
|
||||
|
||||
// Types.
|
||||
import { debug, ScopeError, SourceError, Source } from "../../utils/debug";
|
||||
@@ -12,14 +13,13 @@ import { ComponentModule, setPropertyValue, getComponentModule } from "./compone
|
||||
import { platformNames, device } from "../../platform";
|
||||
import { resolveFileName } from "../../file-system/file-name-resolver";
|
||||
import { profile } from "../../profiling";
|
||||
import * as traceModule from "../../trace";
|
||||
|
||||
const ios = platformNames.ios.toLowerCase();
|
||||
const android = platformNames.android.toLowerCase();
|
||||
|
||||
const defaultNameSpaceMatcher = /tns\.xsd$/i;
|
||||
|
||||
var trace: typeof traceModule;
|
||||
let trace: typeof traceModule;
|
||||
function ensureTrace() {
|
||||
if (!trace) {
|
||||
trace = require("trace");
|
||||
@@ -151,7 +151,7 @@ function loadInternal(fileName: string, context?: any, moduleNamePath?: string):
|
||||
const filePathRelativeToApp = (moduleNamePath && moduleNamePath.startsWith(appPath) ? "./" + moduleNamePath.substr(appPath.length + 1) : moduleNamePath) + ".xml";
|
||||
|
||||
if (global.moduleExists(filePathRelativeToApp)) {
|
||||
var text = global.loadModule(filePathRelativeToApp);
|
||||
const text = global.loadModule(filePathRelativeToApp);
|
||||
componentModule = parseInternal(text, context, fileName, moduleNamePath);
|
||||
} else if (fileName && File.exists(fileName)) {
|
||||
const file = File.fromPath(fileName);
|
||||
@@ -228,7 +228,7 @@ function loadCustomComponent(componentPath: string, componentName?: string, attr
|
||||
// webpack modules require paths to be relative to /app folder.
|
||||
let cssModulePath = fullComponentPathFilePathWithoutExt + ".css";
|
||||
if (cssModulePath.startsWith("/")) {
|
||||
var app = knownFolders.currentApp().path + "/";
|
||||
const app = knownFolders.currentApp().path + "/";
|
||||
if (cssModulePath.startsWith(app)) {
|
||||
cssModulePath = "./" + cssModulePath.substr(app.length);
|
||||
}
|
||||
@@ -238,7 +238,7 @@ function loadCustomComponent(componentPath: string, componentName?: string, attr
|
||||
if (global.moduleExists(cssModulePath)) {
|
||||
(<any>parentPage).addCssFile(cssModulePath);
|
||||
} else {
|
||||
var cssFilePath = resolveFileName(fullComponentPathFilePathWithoutExt, "css");
|
||||
const cssFilePath = resolveFileName(fullComponentPathFilePathWithoutExt, "css");
|
||||
// Add component CSS file if exists.
|
||||
if (cssFilePath) {
|
||||
if (parentPage && typeof (<any>parentPage).addCssFile === "function") {
|
||||
@@ -270,11 +270,11 @@ function getExports(instance: ViewBase): any {
|
||||
}
|
||||
|
||||
function parseInternal(value: string, context: any, uri?: string, moduleNamePath?: string): ComponentModule {
|
||||
var start: xml2ui.XmlStringParser;
|
||||
var ui: xml2ui.ComponentParser;
|
||||
let start: xml2ui.XmlStringParser;
|
||||
let ui: xml2ui.ComponentParser;
|
||||
|
||||
var errorFormat = (debug && uri) ? xml2ui.SourceErrorFormat(uri) : xml2ui.PositionErrorFormat;
|
||||
var componentSourceTracker = (debug && uri) ? xml2ui.ComponentSourceTracker(uri) : () => {
|
||||
const errorFormat = (debug && uri) ? xml2ui.SourceErrorFormat(uri) : xml2ui.PositionErrorFormat;
|
||||
const componentSourceTracker = (debug && uri) ? xml2ui.ComponentSourceTracker(uri) : () => {
|
||||
// no-op
|
||||
};
|
||||
|
||||
@@ -320,7 +320,7 @@ namespace xml2ui {
|
||||
}
|
||||
|
||||
public parse(value: string) {
|
||||
var xmlParser = new xml.XmlParser((args: xml.ParserEvent) => {
|
||||
const xmlParser = new xml.XmlParser((args: xml.ParserEvent) => {
|
||||
try {
|
||||
this.next(args);
|
||||
} catch (e) {
|
||||
@@ -346,7 +346,7 @@ namespace xml2ui {
|
||||
|
||||
export function SourceErrorFormat(uri): ErrorFormatter {
|
||||
return (e: Error, p: xml.Position) => {
|
||||
var source = p ? new Source(uri, p.line, p.column) : new Source(uri, -1, -1);
|
||||
const source = p ? new Source(uri, p.line, p.column) : new Source(uri, -1, -1);
|
||||
e = new SourceError(e, source, "Building UI from XML.");
|
||||
return e;
|
||||
}
|
||||
@@ -359,7 +359,7 @@ namespace xml2ui {
|
||||
export function ComponentSourceTracker(uri): SourceTracker {
|
||||
return (component: any, p: xml.Position) => {
|
||||
if (!Source.get(component)) {
|
||||
var source = p ? new Source(uri, p.line, p.column) : new Source(uri, -1, -1);
|
||||
const source = p ? new Source(uri, p.line, p.column) : new Source(uri, -1, -1);
|
||||
Source.set(component, source);
|
||||
}
|
||||
}
|
||||
@@ -528,12 +528,12 @@ namespace xml2ui {
|
||||
}
|
||||
|
||||
public buildTemplate(): Template {
|
||||
var context = this._context;
|
||||
var errorFormat = this._templateProperty.errorFormat;
|
||||
var sourceTracker = this._templateProperty.sourceTracker;
|
||||
var template: Template = profile("Template()", () => {
|
||||
var start: xml2ui.XmlArgsReplay;
|
||||
var ui: xml2ui.ComponentParser;
|
||||
const context = this._context;
|
||||
const errorFormat = this._templateProperty.errorFormat;
|
||||
const sourceTracker = this._templateProperty.sourceTracker;
|
||||
const template: Template = profile("Template()", () => {
|
||||
let start: xml2ui.XmlArgsReplay;
|
||||
let ui: xml2ui.ComponentParser;
|
||||
|
||||
(start = new xml2ui.XmlArgsReplay(this._recordedXmlStream, errorFormat))
|
||||
// No platform filter, it has been filtered already
|
||||
@@ -543,6 +543,7 @@ namespace xml2ui {
|
||||
|
||||
return ui.rootComponentModule.component;
|
||||
});
|
||||
|
||||
return template;
|
||||
}
|
||||
}
|
||||
@@ -633,14 +634,14 @@ namespace xml2ui {
|
||||
public parse(args: xml.ParserEvent): XmlStateConsumer {
|
||||
|
||||
// Get the current parent.
|
||||
var parent = this.parents[this.parents.length - 1];
|
||||
var complexProperty = this.complexProperties[this.complexProperties.length - 1];
|
||||
const parent = this.parents[this.parents.length - 1];
|
||||
const complexProperty = this.complexProperties[this.complexProperties.length - 1];
|
||||
|
||||
// Create component instance from every element declaration.
|
||||
if (args.eventType === xml.ParserEventType.StartElement) {
|
||||
if (ComponentParser.isComplexProperty(args.elementName)) {
|
||||
|
||||
var name = ComponentParser.getComplexPropertyName(args.elementName);
|
||||
const name = ComponentParser.getComplexPropertyName(args.elementName);
|
||||
|
||||
const complexProperty: ComponentParser.ComplexProperty = {
|
||||
parent: parent,
|
||||
@@ -677,7 +678,7 @@ namespace xml2ui {
|
||||
|
||||
} else {
|
||||
|
||||
var componentModule = this.buildComponent(args);
|
||||
const componentModule = this.buildComponent(args);
|
||||
|
||||
if (componentModule) {
|
||||
this.sourceTracker(componentModule.component, args.position);
|
||||
@@ -734,10 +735,10 @@ namespace xml2ui {
|
||||
}
|
||||
|
||||
public static getComplexPropertyName(fullName: string): string {
|
||||
var name: string;
|
||||
let name: string;
|
||||
|
||||
if (isString(fullName)) {
|
||||
var names = fullName.split(".");
|
||||
const names = fullName.split(".");
|
||||
name = names[names.length - 1];
|
||||
}
|
||||
|
||||
@@ -754,7 +755,7 @@ namespace xml2ui {
|
||||
|
||||
private static addToComplexProperty(parent: ComponentModule, complexProperty: ComponentParser.ComplexProperty, elementModule: ComponentModule) {
|
||||
// If property name is known collection we populate array with elements.
|
||||
var parentComponent = <any>parent.component;
|
||||
const parentComponent = <any>parent.component;
|
||||
if (ComponentParser.isKnownCollection(complexProperty.name, parent.exports)) {
|
||||
complexProperty.items.push(elementModule.component);
|
||||
} else if (parentComponent._addChildFromBuilder) {
|
||||
|
||||
@@ -28,10 +28,10 @@ const CSSFILE = "cssFile";
|
||||
const IMPORT = "import";
|
||||
|
||||
const createComponentInstance = profile("createComponentInstance", (elementName: string, namespace: string): { instance: View, instanceModule: Object } => {
|
||||
var instance: View;
|
||||
var instanceModule: Object;
|
||||
let instance: View;
|
||||
let instanceModule: Object;
|
||||
// Get module id.
|
||||
var moduleId = MODULES[elementName] || UI_PATH +
|
||||
let moduleId = MODULES[elementName] || UI_PATH +
|
||||
(elementName.toLowerCase().indexOf("layout") !== -1 ? "layouts/" : "") +
|
||||
elementName.split(/(?=[A-Z])/).join("-").toLowerCase();
|
||||
|
||||
@@ -141,10 +141,10 @@ const applyComponentAttributes = profile("applyComponentAttributes", (instance:
|
||||
if (instance && instanceModule) {
|
||||
for (let attr in attributes) {
|
||||
|
||||
var attrValue = <string>attributes[attr];
|
||||
const attrValue = <string>attributes[attr];
|
||||
|
||||
if (attr.indexOf(":") !== -1) {
|
||||
var platformName = attr.split(":")[0].trim();
|
||||
const platformName = attr.split(":")[0].trim();
|
||||
|
||||
if (platformName.toLowerCase() === platform.device.os.toLowerCase()) {
|
||||
attr = attr.split(":")[1].trim();
|
||||
@@ -186,7 +186,7 @@ export function getComponentModule(elementName: string, namespace: string, attri
|
||||
|
||||
applyComponentAttributes(instance, instanceModule, moduleExports, attributes);
|
||||
|
||||
var componentModule;
|
||||
let componentModule;
|
||||
if (instance && instanceModule) {
|
||||
componentModule = { component: instance, exports: instanceModule };
|
||||
}
|
||||
@@ -196,7 +196,7 @@ export function getComponentModule(elementName: string, namespace: string, attri
|
||||
export function setPropertyValue(instance: View, instanceModule: Object, exports: Object, propertyName: string, propertyValue: any) {
|
||||
// Note: instanceModule can be null if we are loading custom compnenet with no code-behind.
|
||||
if (isBinding(propertyValue) && instance.bind) {
|
||||
var bindOptions = getBindingOptions(propertyName, getBindingExpressionFromAttribute(propertyValue));
|
||||
const bindOptions = getBindingOptions(propertyName, getBindingExpressionFromAttribute(propertyValue));
|
||||
instance.bind({
|
||||
sourceProperty: bindOptions[bindingConstants.sourceProperty],
|
||||
targetProperty: bindOptions[bindingConstants.targetProperty],
|
||||
@@ -206,7 +206,7 @@ export function setPropertyValue(instance: View, instanceModule: Object, exports
|
||||
}
|
||||
else if (isEventOrGesture(propertyName, instance)) {
|
||||
// Get the event handler from page module exports.
|
||||
var handler = exports && exports[propertyValue];
|
||||
const handler = exports && exports[propertyValue];
|
||||
|
||||
// Check if the handler is function and add it to the instance for specified event name.
|
||||
if (typeof handler === "function") {
|
||||
@@ -226,10 +226,10 @@ function getBindingExpressionFromAttribute(value: string): string {
|
||||
}
|
||||
|
||||
function isBinding(value: any): boolean {
|
||||
var isBinding;
|
||||
let isBinding;
|
||||
|
||||
if (typeof value === "string") {
|
||||
var str = value.trim();
|
||||
const str = value.trim();
|
||||
isBinding = str.indexOf("{{") === 0 && str.lastIndexOf("}}") === str.length - 2;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user