tom
2023-12-06 9e968679ed2e6937aeb7b50a6c450d5d19251f42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
LogicNode = Object.subClass({
    
    init: function(config) {
        this.level = config.level || 0;
        this.type = config.type;
        this.expression = config.expression;
        this.condition = config.condition;
        this.valuation = config.valuation;
        this.parent = config.parent;
        this.children = [];
    },
    
    toString: function(content) {
        var result = "";
        
        if (!this.condition) {
            this.expression = this.valuation.expression = this.valuation.editor.val();
        }
        else {
            this.condition.expression = this.condition.editor.val();
            this.valuation.expression = this.valuation.editor.val();
            
            this.expression = this.condition.expression + "?" + this.valuation.expression;
        }
        
        result = result + this.expression;
        var flag = this.level == 0 ? ";" : ",";
        
        var max = this.children.length;
        for (var i = 0; i < max; i++) {
            var child = this.children[i];
            result = result + flag + child.toString();
        }
    },
    
    show: function() {
        if (!this.condition) {
            this.valuation.editor.val(this.valuation.expression);
        }
        else {
            this.condition.editor.val(this.condition.expression);
            this.valuation.editor.val(this.valuation.expression);
        }
    },
    
    clear: function() {
        if (!this.condition) {
            if (this.valuation.el) {
                this.valuation.el.remove();
            }
        }
        else {
            if (this.condition.el) {
                this.condition.el.remove();
            }
            if (this.valuation.el) {
                this.valuation.el.remove();
            }            
        }    
            
        if (this.el) {
            this.el.remove();
        }
    }
});
 
LogicTree = Object.subClass({
    
    init: function(config) {
        this.type;
        this.condition = null;
        this.valuation = null;
        this.eidtor = null;
        this.children = [];
    },
    
    parse: function(content, flag) {
        this.clear();
        
        if (!content) {
            return;
        }
        
        flag = flag || ";";
        
        if (content.indexOf(";") < 0) {
            this.parseMathValue(this, content, 0);
            return;
        }
        
        var sequences = content.split(";");
        var max = sequences.length;
            
        for (var i = 0; i < max; i++) {
            sequence = sequences[i];
            var node = this.parseIFValue(this, sequence, 0);
        }
    },
        
    parseMathValue: function(parent, value, level) {
        var node = new LogicNode({
            parent: parent,
            level: level,
            type: "Math",
            valuation: {
                expression: value,
                el: null
            }
        });
        
        parent.children.push(node);
        return node;
    },
        
    parseIFValue: function(parent, content, level) {
        var pos = content.indexOf("?");
        
        //1. else if 
        if (pos < 0) {
            var node = new LogicNode({
                parent: parent,
                level: level,
                type: "ELSE",
                expression: content,
                valuation: {
                    expression: content,
                    el: null
                }
            });    
                    
            parent.children.push(node);        
            return;
        }
        
        //2. if condition
        var node = new LogicNode({
            parent: parent,
            level: level,
            type: "IF",
            condition: {
                expression: content.substring(0, pos),
                el: null
            }
        });
        node.expression = content;
        parent.children.push(node);
 
        //3. if valuation
        var pos_subflag = content.indexOf(",");
        if (pos_subflag < 0) {
            var math = content.substring(pos + 1);
            
            node.valuation = {
                expression: math,
                el: null
            }            
            return;
        }
        
        //4. children
        this.parse(node, ",", 1);
    },
    
    getString: function() {
        var result = "";
        
        var max = this.children.length;
        for (var i = 0; i < max; i++) {
            var child = this.children[i];
            result = result + child.toString();
        }
        
        return result;
    },
    
    show: function() {
        var max = this.children.length;
        for (var i = 0; i < max; i++) {
            var child = this.children[i];
            child.show();
        }
    },
    
    clear: function() {
        var max = this.children.length;
        for (var i = 0; i < max; i++) {
            var child = this.children[i];
            child.clear();
        }
        
        this.children = [];
    }
});
 
FormulaDesigner = Control.subClass({
    
    items: true,
    
    init: function(config) {
        config.css = util.combine(config.css, {
            container: null
        });
        
        this.logicTree = new LogicTree({});
        this.mathLine = {};
        this.selected = null;
        
        Control.call(this, config);
    },
    
    getContainerTemplate: function() {
        return $(Formula_Template.container.join(""));
    },
    
    createContent: function() {
        this.logicTree.el = this.container;
        
        //1. body
        this.logicTree.el.body = $("#formula_body", this.container);
        
        //2. header
        this.header = $("#formula_header", this.container);
        this.header.nameEditor = $("#editor_name", this.header);
        this.header.alignEditor = $("#editor_align", this.header);
        this.header.formaterEditor = $("#editor_formatter", this.header);
        this.header.widthEditor = $("#width_editor", this.header);
    },
    
    loadSetting: function(setting) {
        this.setVariantName(setting.name);
        this.setVariantAlign(setting.align);
        this.setVariantFormatter(setting.formatter);
        this.setVariantWidth(setting.width);
    },
    
    loadExpression: function(expression) {
        this.logicTree.parse(expression);
        this.createLines();
        this.logicTree.show();
    },
    
    createLines: function() {
        var lines = this.logicTree.children;
        var max = lines.length;
        
        for (var i = 0; i < max; i++) {
            this.createOneLine(lines[i]);
        }
    },
    
    createOneLine: function(logic) {
        if ("Math" == logic.type) {
            this.createMathLine(logic);
        }
        else if ("IF" == logic.type) {
            this.createIFLine(logic);
        }
        else if ("ELSE" == logic.type) {
            this.createElseLine(logic);
        }
    },
    
    createMathLine: function(logic) {
        //1. element
        var el = logic.valuation.el = $(Formula_Template.mathLine.join(""));
        logic.valuation.editor = $("#mathEditor", el);
        
        //2. append to parent
        logic.parent.el.body.append(el);
        return result;        
    },
    
    createIFLogic: function(parentLogic) {
        //1. clear math line
        if (this.mathLine.el) {
            this.mathLine.el.remove();
            this.mathLine = {};
        }
        
        //2. login tree
        this.createIFLine(parentLogic);
        this.createElseLine(parentLogic);
    },
    
    createIFLine: function(logic) {
        this.createLogicLine(logic, "IF");
    },
    
    createElseIFLine: function(logic, priorLogic) {
        this.createLogicLine(logic, "ELSE-IF", priorLogic);
    },
    
    createElseLine: function(logic) {
        this.createLogicLine(logic, "ELSE");
    },
    
    createLogicLine: function(logic, type, priorLogic) {
        //1. create element
        var el; var existsButtons;
        
        if ("IF" == type || "ELSE-IF" == type) {
            el = $(Formula_Template.logicIF.join(""));
            logic.condition.editor = $("#conditionEditor", el);
            logic.valuation.editor = $("#valuationEditor", el);
            existsButtons = true;
        }
        else {
            el = logic.valuation.el = $(Formula_Template.logicELSE.join(""));
            logic.valuation.editor = $("#valuationEditor", el);
            existsButtons = false;
        }
        
        logic.el = el;
        el.body = $("#logic_body", el);
        
        //2. caption 
        var caption = $("#conditionCaption", el);
        caption.html(this.getLogicLabel(type));
        
        //4. event
        if (existsButtons) {
            var me = this;
            el.bntAddOne = $("#btn_addOne", el);
            el.bntAddOne.click(function() {
                me.createElseIFLine(parentLogic, result);
            });
            
            el.bntAddChild = $("#btn_addChild", el);
            el.bntAddChild.click(function() {
                me.createIFLogic(result);
            });
            
            el.bntDeleteOne = $("#btn_deleteOne", el); 
            el.bntDeleteOne.click(function() {
                me.deleteLogic(result);
            });            
        }
        
        //5. append to parent
        var parentLogic = logic.parent;
        
        if (priorLogic) {
            result.el.insertBefore(priorLogic.el);
        }
        else {
            var parentEl = parentLogic.el.body;
            parentEl.append(el);
        }
        
        //6. animate
        return result;
    },
    
    deleteLogic: function(logic) {
        var parent = logic.parent;
        var logicList = parent.children;
        
        //1. 只有if和else
        if (logicList.length <= 2) {
            this.initLogicBody(parent);
        }
        else {
            //1. delete from parent
            var index = logicList.indexOf(logic);
            logicList.splice(index, 1);    
            
            //2. delete el
            logic.el.remove();
        }
    },
    
    initLogicBody: function(logic) {
        //1. empty parent children
        logic.children = [];
        
        //2. empty body
        logic.el.body.empty();
        
        //3. append valuation
        var valuation = $(Formula_Template.valuation.join(""));
        logic.el.body.append(valuation);
    },
    
    removeLogicValuation: function(logic) {
        if (!logic.el.valuation) {
            return;
        }
        
        logic.el.valuation.remove();
        logic.el.valuation = null;
    },
    
    getExpressionString: function() {
        if (logicLine) {
            var result = logicLine.expression.getString();
            return result;
        }
        
        var result = "";
        var logicTree = this.logicTree;
        var max = logicTree.length;
        
        for (var i = 0; i < max; i++) {
            var logicLine = logicTree[i];
            result = result + logicLine.expression.getString();
        }
        
        return result;
    },
    
    getLogicLabel: function(type) {
        if ("IF" == type) {
            return "如果:";
        }
        else if ("ELSE-IF" == type) {
            return "如果:";
        }
        else if ("ELSE" == type) {
            return "否则:";
        }
    },
    
    clear: function() {
        //1. math
        if (this.mathLine.el)  {
            this.mathLine.el.remove();
        }
        this.mathLine = {};
        
        //2. 
        var children = this.logicTree.children;
        var max = children.length;
        
        for (var i = 0; i < max; i++) {
            var logic = children[i];
            logic.el.remove();
        }
        
        this.logicTree.children = [];
    },
    
    setVariantName: function(name) {
        this.header.nameEditor.val(name);
    },
    
    setVariantAlign: function(align) {
        var filter = "[value='" + align + "']";
        var option = $(filter, this.header.alignEditor);
        if (option) {
            option.attr("selected", "selected");
        }
    },
    
    setVariantFormatter: function(formatter) {
        var filter = "[value='" + formatter + "']";
        var option = $(filter, this.header.formaterEditor);
        if (option) {
            option.attr("selected", "selected");
        }
    },
    
    setVariantWidth: function(width) {
        width = width || 1;
        var filter = "[value='" + width + "']";
        var option = $(filter, this.header.widthEditor);
        if (option) {
            option.attr("selected", "selected");
        }
    }
    
});
 
 
//*************************************
Formula_Template = {};
 
Formula_Template.container = [
    '<div class="formula">',
        '<div id="formula_header" class="formula-header">',
            '<span>列名:</span>',
            '<input id="editor_name" type="text" value="达成率"/>',
            '<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>',
            '<span>显示格式:</span>',
            '<select id="editor_formatter">',
                '<option value="text">文本</option>',
                '<option value="percent">百分比</option>',
                '<option value="money">金额</option>',
                '<option value="decimal">数值</option>',
            '</select>',
            '<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>',
            '<span>对齐方式:</span>',
            '<select id="editor_align">',
                '<option value="left">左对齐</option>',
                '<option value="center">居中对齐</option>',
                '<option value="right">右对齐</option>',
            '</select>',
            '<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>',
            '<span>列宽度:</span>',
            '<select id="width_editor">',
                '<option value="0.6">0.6</option>',
                '<option value="1">1</option>',
                '<option value="1.5">1.5</option>',
                '<option value="2.5">2.5</option>',
            '</select>',                        
        '</div>',
        '<div class="formula-body">',
            '<div id="formula_body" class="formula-body-inner">',
            '</div>',
        '</div>',
    '</div>'
];
 
Formula_Template.mathLine = [
    '<div id="math_body" class="math-body">',
        '<span id="mathCaption" class="math-caption">=</span>',
        '<input id="mathEditor" class="math-input" type="text" placeholder="请编辑公式"/>',
    '</div>'   
];
 
Formula_Template.logicIF = [
    '<div class="logic">',
        '<div class="logic-condition">',
            '<div class="indicator-line"></div>',
            '<div class="indicator-if"></div>',
            '<span id="conditionCaption" class="condition-caption"></span>',
            '<input id="conditionEditor" class="logic-input-condition" type="text"/>',
            '<div class="logic-btn-group">',
                '<div id="btn_addOne" class="logic-btn">+&nbsp;同级</div>',
                '<div id="btn_addChild" class="logic-btn">+&nbsp;下级</div>',
                '<div id="btn_deleteOne" class="logic-btn">-&nbsp;删除</div>',                
            '</div>',
        '</div>',                
        '<div id="logic_body" class="logic-body">',
            '<div class="indicator-line"></div>',
            '<div id="valuation_body" class="valuation-body">',
                '<input id="valuationEditor" class="logic-input-valuation" type="text"/>',
            '</div>', 
        '</div>',
    '</div>'   
];
 
Formula_Template.logicELSE = [
    '<div class="logic">',
        '<div id="logic_body" class="logic-condition">',
            '<div class="indicator-else"></div>',
            '<span id="conditionCaption" class="condition-caption"></span>',
            '<input id="valuationEditor" class="logic-input-valuation" type="text"/>',
        '</div>',
    '</div>'   
];
 
Formula_Template.valuation = [
    '<div class="indicator-line"></div>',
    '<div id="valuation_body" class="valuation-body">',
        '<input id="valuationEditor" type="text"/>',
    '</div>', 
];