drnaf
Version:
Dynamic React-Native Application Framework
202 lines (157 loc) • 5.1 kB
JavaScript
import React, { Component } from 'react'
import { View, Text, TouchableHighlight } from 'react-native'
import { l } from '../utilities/Logs';
export class DRNAFComponent extends Component {
constructor(props, name) {
super(props);
// props
this.ct = name + '->';
this.values = 123;
this.entities = {
actions: [],
value: {
type: 'number', // 'string', 'object'
data: 0,
},
collectionCallback: props.collectionCallback,
}
this.reference_key = props.reference_key +""
}
collectionCallback(props = {
fieldName: null,
data: null,
}) {
// prepare usage variables
const mtn = this.ct + "collectionCallback() ";
// null condition
if (!this.entities.collectionCallback) {
// log
l.e(mtn + "collection callback is not ready.");
// exit from this process
return;
}
// collection callback
this.entities.collectionCallback(props);
}
/**
*
* @param {*} object
* @param {object} fields
@template [{ name: 'width',
commands: ['non-int'],
then:[{
action: 'swap-to',
'swap-to': <object to swapped>
},{
action: 'delete',
}]
}
*/
deleteUselessField(object, fields) {
for (var a = 0; a < fields.length; a++) {
// prepare usage variables
const field = fields[a];
const cmds = field.commands;
// l.obj('cmds: ', cmds)
// l.obj('field: ', field);
var result = this.condition(cmds, {
name: field.name,
value: object[field.name]
})
// negative
const negative = field.negative !== null && field.negative !== undefined;
const shouldThen = !result.status && negative;
// l.m('should-then: ' + shouldThen)
// then
if (shouldThen) this.negative(object, field);
return result;
}
}
negative(object, field) {
for (var a = 0; a < field.negative.length; a++) {
// prepare usage variables
const n = field.negative[a];
const _s = {
action: n.action,
}
if (_s.action === 'delete') {
delete object[field.name];
} else if (_s.action === 'swap-to') {
object[field.name] = n[_s.action];
}
}
}
condition(cmds, field) {
// prepare usgae variables
var _command = "-";
const result = {
status: false,
message: "\"" + field.name + "\""
+ ' with value: ' + field.value
+ " command: " + cmds,
}
for (var a = 0; a < cmds.length; a++) {
// prepare usage variables
const cmd = cmds[a];
const fieldVal = field.value;
const c = {
command: cmd,
}
// non types
if (c.command === 'non-null') {
result.status = (fieldVal !== null);
} else if (c.command === 'non-int') {
result.status = (typeof fieldVal !== 'number');
} else if (c.command === 'non-percentage') {
result.status = this.isPercentage(fieldVal)
? false
: true
// types
} else if (c.command === 'int') {
result.status = (typeof fieldVal === 'number')
} else if (c.command === 'percentage') {
result.status = this.isPercentage(fieldVal);
// l.m('is percetage[' + fieldVal + ']: ' + this.isPercentage(fieldVal));
} else if (c.command === 'ready-to-use') {
result.status = fieldVal !== null
&& fieldVal !== undefined
}
if (result.status) break;
}
return result;
}
/** Micro methods */
isPercentage(val) {
// perpare usage variables
if (!(val + "").includes('%')
|| val === null
|| val === undefined)
return false;
val = (val + "").replace('%', '');
try {
return typeof Number.parseInt(val) === 'number';
} catch (e) {
}
return false;
}
/** Help utils */
nonNullNonUndefined( obj ) {
return (obj !== null && obj !== undefined);
}
referenceKey(element) {
return (typeof element === 'object'
&& typeof element.props === 'object')
? element.reference_key
: null
}
}
DRNAFComponent.isStyleValid = (fields) => {
for (var a = 0; a < fields.length; a++) {
// prepare usage variables
const f = fields[a];
if (typeof f.value !== f.type) {
return false
}
}
return true;
}