条件演算子をカスタマイズし、kintoneクエリ文字列に変換することができます。
This commit is contained in:
@@ -113,7 +113,7 @@ import { finished } from 'stream';
|
|||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { defineComponent,ref,reactive, computed } from 'vue';
|
import { defineComponent,ref,reactive, computed, inject } from 'vue';
|
||||||
import { INode,ConditionTree,GroupNode,ConditionNode, LogicalOperator,Operator,NodeType } from '../../types/Conditions';
|
import { INode,ConditionTree,GroupNode,ConditionNode, LogicalOperator,Operator,NodeType } from '../../types/Conditions';
|
||||||
import ConditionObject from './ConditionObject.vue';
|
import ConditionObject from './ConditionObject.vue';
|
||||||
export default defineComponent( {
|
export default defineComponent( {
|
||||||
@@ -143,12 +143,9 @@ export default defineComponent( {
|
|||||||
return opts;
|
return opts;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const operator = inject('Operator')
|
||||||
const operators =computed(()=>{
|
const operators =computed(()=>{
|
||||||
const opts=[];
|
return operator ? operator : Object.values(Operator);
|
||||||
for(const op in Operator){
|
|
||||||
opts.push(Operator[op as keyof typeof Operator]);
|
|
||||||
}
|
|
||||||
return opts;
|
|
||||||
});
|
});
|
||||||
const tree = reactive(props.conditionTree);
|
const tree = reactive(props.conditionTree);
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { ConditionNode, ConditionTree, Operator } from 'app/src/types/Conditions';
|
import { ConditionNode, ConditionTree, Operator, OperatorListItem } from 'app/src/types/Conditions';
|
||||||
import { computed, defineComponent, provide, reactive, ref, watchEffect } from 'vue';
|
import { computed, defineComponent, provide, reactive, ref, watchEffect } from 'vue';
|
||||||
import ConditionEditor from '../ConditionEditor/ConditionEditor.vue';
|
import ConditionEditor from '../ConditionEditor/ConditionEditor.vue';
|
||||||
|
|
||||||
@@ -81,6 +81,9 @@ export default defineComponent({
|
|||||||
onlySourceSelect: {
|
onlySourceSelect: {
|
||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: false
|
default: false
|
||||||
|
},
|
||||||
|
operatorList: {
|
||||||
|
type: Array,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -95,6 +98,8 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
provide('Operator', props.operatorList);
|
||||||
|
|
||||||
const btnDisable = computed(() => {
|
const btnDisable = computed(() => {
|
||||||
const onlySourceSelect = props.onlySourceSelect;
|
const onlySourceSelect = props.onlySourceSelect;
|
||||||
|
|
||||||
@@ -116,7 +121,7 @@ export default defineComponent({
|
|||||||
if (props.modelValue && props.modelValue !== '') {
|
if (props.modelValue && props.modelValue !== '') {
|
||||||
tree.fromJson(props.modelValue);
|
tree.fromJson(props.modelValue);
|
||||||
} else {
|
} else {
|
||||||
const newNode = new ConditionNode({}, Operator.Equal, '', tree.root);
|
const newNode = new ConditionNode({}, (props.operatorList && props.operatorList.length > 0) ? props.operatorList[0] as OperatorListItem : Operator.Equal, '', tree.root);
|
||||||
tree.addNode(tree.root, newNode);
|
tree.addNode(tree.root, newNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,13 +137,15 @@ export default defineComponent({
|
|||||||
|
|
||||||
const onClosed = (val: string) => {
|
const onClosed = (val: string) => {
|
||||||
if (val == 'OK') {
|
if (val == 'OK') {
|
||||||
const conditionJson = tree.toJson();
|
|
||||||
isSetted.value = true;
|
isSetted.value = true;
|
||||||
|
tree.setQuery(tree.buildConditionQueryString(tree.root));
|
||||||
|
const conditionJson = tree.toJson();
|
||||||
emit('update:modelValue', conditionJson);
|
emit('update:modelValue', conditionJson);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
watchEffect(() => {
|
watchEffect(() => {
|
||||||
|
tree.setQuery(tree.buildConditionQueryString(tree.root));
|
||||||
const conditionJson = tree.toJson();
|
const conditionJson = tree.toJson();
|
||||||
emit('update:modelValue', conditionJson);
|
emit('update:modelValue', conditionJson);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -74,6 +74,11 @@ export class GroupNode implements INode {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type OperatorListItem = {
|
||||||
|
label: string;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
// 条件式ノード
|
// 条件式ノード
|
||||||
export class ConditionNode implements INode {
|
export class ConditionNode implements INode {
|
||||||
index: number;
|
index: number;
|
||||||
@@ -83,13 +88,13 @@ export class ConditionNode implements INode {
|
|||||||
return this.parent.logicalOperator;
|
return this.parent.logicalOperator;
|
||||||
};
|
};
|
||||||
object: any; // 比較元
|
object: any; // 比較元
|
||||||
operator: Operator; // 比較子
|
operator: Operator | OperatorListItem; // 比較子
|
||||||
value: any;
|
value: any;
|
||||||
get header():string{
|
get header():string{
|
||||||
return 'generic';
|
return 'generic';
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(object: any, operator: Operator, value: any, parent: GroupNode) {
|
constructor(object: any, operator: Operator | OperatorListItem, value: any, parent: GroupNode) {
|
||||||
this.index=0;
|
this.index=0;
|
||||||
this.type = NodeType.Condition;
|
this.type = NodeType.Condition;
|
||||||
this.object = object;
|
this.object = object;
|
||||||
@@ -113,10 +118,12 @@ export class ConditionNode implements INode {
|
|||||||
export class ConditionTree {
|
export class ConditionTree {
|
||||||
root: GroupNode;
|
root: GroupNode;
|
||||||
maxIndex:number;
|
maxIndex:number;
|
||||||
|
queryString:string;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.maxIndex=0;
|
this.maxIndex=0;
|
||||||
this.root = new GroupNode(LogicalOperator.AND, null);
|
this.root = new GroupNode(LogicalOperator.AND, null);
|
||||||
|
this.queryString='';
|
||||||
}
|
}
|
||||||
|
|
||||||
// ノード追加
|
// ノード追加
|
||||||
@@ -198,12 +205,49 @@ export class ConditionTree {
|
|||||||
if(value && typeof value ==='object' && ('label' in value)){
|
if(value && typeof value ==='object' && ('label' in value)){
|
||||||
value =condNode.value.label;
|
value =condNode.value.label;
|
||||||
}
|
}
|
||||||
return `${typeof condNode.object.name === 'object' ? condNode.object.name.name : condNode.object.name} ${condNode.operator} '${value}'`;
|
return `${typeof condNode.object.name === 'object' ? condNode.object.name.name : condNode.object.name} ${typeof condNode.operator === 'object' ? condNode.operator.label : condNode.operator} '${value}'`;
|
||||||
} else {
|
} else {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
buildConditionQueryString(node:INode){
|
||||||
|
if (node.type !== NodeType.Condition) {
|
||||||
|
let conditionString = '';
|
||||||
|
if(node.type !== NodeType.Root){
|
||||||
|
conditionString = '(';
|
||||||
|
}
|
||||||
|
|
||||||
|
const groupNode = node as GroupNode;
|
||||||
|
for (let i = 0; i < groupNode.children.length; i++) {
|
||||||
|
const childConditionString = this.buildConditionQueryString(groupNode.children[i]);
|
||||||
|
if (childConditionString !== '') {
|
||||||
|
conditionString += childConditionString;
|
||||||
|
if (i < groupNode.children.length - 1) {
|
||||||
|
conditionString += ` ${groupNode.logicalOperator.toLowerCase()} `;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(node.type !== NodeType.Root){
|
||||||
|
conditionString += ')';
|
||||||
|
}
|
||||||
|
return conditionString;
|
||||||
|
} else {
|
||||||
|
const condNode=node as ConditionNode;
|
||||||
|
if (condNode.object && condNode.operator ) {
|
||||||
|
let value=condNode.value;
|
||||||
|
if(value && typeof value ==='object' && ('label' in value)){
|
||||||
|
value =condNode.value.label;
|
||||||
|
}
|
||||||
|
return `${condNode.object.code} ${typeof condNode.operator === 'object' ? condNode.operator.value : condNode.operator} "${value}"`;
|
||||||
|
} else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param node ノード移動
|
* @param node ノード移動
|
||||||
@@ -325,7 +369,7 @@ export class ConditionTree {
|
|||||||
}
|
}
|
||||||
|
|
||||||
toJson():string{
|
toJson():string{
|
||||||
return JSON.stringify(this.root, (key, value) => {
|
return JSON.stringify({queryString :this.queryString, ...this.root}, (key, value) => {
|
||||||
if (key === 'parent') {
|
if (key === 'parent') {
|
||||||
return value ? value.type : null;
|
return value ? value.type : null;
|
||||||
}
|
}
|
||||||
@@ -333,4 +377,7 @@ export class ConditionTree {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setQuery(queryString:string){
|
||||||
|
this.queryString=queryString;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user