简体   繁体   English

保留每个JavaScript函数,twig,JointJS的变量值

[英]Keep the value of a variable for every JavaScript function, twig, JointJS

first of all, excuse me for my English, I'm French. 首先,请原谅我的英语,我是法语。

I am coming to you because I have a problem. 我来找你是因为我有问题。 I have a function of "drop", a twig loop that retrieves field names and I would generate a first variable with a loop (var = color_fieldName #color) and keep the value of this variable every time we repeated this function. 我有一个函数“ drop”,这是一个检索字段名称的树枝循环,我将使用循环生成第一个变量(var = color_fieldName #color),并在每次重复此函数时保留该变量的值。 In my code, the color changes every time: 在我的代码中,颜色每次都会改变:

 function drop(ev, id, x, y) { //We drop the item

            {% for element in listElement %}
                if ( {{ element.id }} == id)
                {
                    //We created a variable with a random color to each tag

                    {% for field in element.fields %}
                        {% if (field.valueType == "tag") and (field.valueBool == 1) %}
       /*THIS VARIABLE ==>*/  var color_{{field.name|replace({' ': ''})}};
                            var color = Colors.random();
                            color_{{field.name|replace({' ': ''})}} = color.rgb;**
                        {% endif %}
                    {% endfor %}

                    //Generation of legend for tag
                     {% set posLeg = 20 %}
                        {% for field in element.fields %}
                            {% if (field.valueType == "tag") and (field.valueBool == 1) %}
                                if(legend{{field.name|replace({' ': ''})}} == null){
                                    var fiedName = '{{field.name}}'
                                    var legend{{field.name|replace({' ': ''})}} = new joint.shapes.basic.Rect({
                                        markup: '<g class="rotatable"><g class="scalable"><rect/></g><text/><circle class="legend_{{element.name|replace({' ': ''})}}_{{field.name|replace({' ': ''})}}"/></g>',
                                        position: {x: {{posLeg}} , y: height-30},
                                        size: { width: (fiedName.length * 8)+20, height: 30 },
                                        attrs: {
                                                rect: { fill: 'white', stroke: 'none'},
                                                '.legend_{{element.name|replace({' ': ''})}}_{{field.name|replace({' ': ''})}}': {
                                                    r: 7, 
                                                    cx: 0,
                                                    cy: 15,
                                                    fill : color_{{field.name|replace({' ': ''})}},
                                                    stroke: 'black'
                                                },                                                              
                                                text: { 
                                                    text : '{{field.name}}',
                                                    fill : 'black'
                                                }
                                            },
                                    });
                                }
                                graph.addCell(legend{{field.name|replace({' ': ''})}});
                                {% set posLeg = posLeg + 120 %}
                            {% endif %}
                        {% endfor %}

                    lastDropped = id;

                    {% set posCy = 0 %}
                    var E{{ element.name|replace({' ': ''}) }} = new joint.shapes.basic.Image({
                       markup: '<g class="rotatable"><g class="scalable"><rect/></g><image/><text/>}{% for field in element.fields %}{% if (field.valueType == "tag") and (field.valueBool == 1) %}<circle class="{{element.name|replace({' ': ''})}}_{{field.name|replace({' ': ''})}}"/>{% endif %}{% endfor %}</g>',
                        position: {x: x, y: y},
                        size: {width: 100, height: 50},
                        attrs: {
                            image: {
                                {% if element.imageName %}
                                "xlink:href": "{{ asset('uploads/documents/'~element.imageName) }}",
                                {% else %}
                                "xlink:href": "{{ asset('/bundles/diagram/img/p.png') }}",
                                {% endif %}
                                width: 100, height: 50
                            },
                            text: {
                                text: '{{ element.name }}',
                                fill: 'black',
                            },
                            {% for field in element.fields %}
                                {% if (field.valueType == "tag") and (field.valueBool == 1) %}
                                    '.{{element.name|replace({' ': ''})}}_{{field.name|replace({' ': ''})}}': {
                                        r: 7, 
                                        cx: 90,
                                        cy: {{posCy}},                                                      
                                        fill : color_{{field.name|replace({' ': ''})}},
                                        stroke: 'black'
                                    },
                                 {% set posCy = posCy + 15 %}                                                             
                                {% endif %}
                            {% endfor %}
                            idElem: '{{ element.id }}',
                        }
                    });
                    graph.addCell(E{{ element.name|replace({' ': ''}) }});
                }
            {% endfor %}

    }

For information, I am under symfony 3, I use the library JOINTJS for diagrams and JavaScript 有关信息,我现在使用的是symfony 3,我将JOINTJS库用于图表和JavaScript

Thank you in advance ! 先感谢您 !

Create an object in the gobal scope, which stores the values : 在gobal范围内创建一个对象,该对象存储值:

var colorsDictionary = {};

function getColor(tag){
    if(tag in colorsDictionary){
        return colorsDictionary[tag];
    }
    else{
        colorsDictionary[tag] = Colors.random().rgb();
        return colorsDictionary[tag];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM