Permissions (Add/Delete but not Edit) - getorgchart
I know that in order to allow editing of the whole widget you use attribute enableEdit=true (or false to disable) , but how can I disable "editing" of nodes but allow add/delete only?
Thank you,
George
A work-around , although I believe not the best way but it will do the trick until this is served as an option into the api.
<style>
/* use a CSS selector */
[data-btn-action="edit"] { display:none !important; }
</style>
Similarly for 'add' and 'del'. Hope this helps others too.
George
You can display your own buttons into the squares and add Listeners like this:
For display customized buttons:
var btnAdd = '<g data-action="add" id="btnInsertNode" class="btn" transform="matrix(0.14,0,0,0.14,0,0)"><rect style="opacity:0" x="0" y="0" height="300" width="300" /><path fill="#686868" d="M149.996,0C67.157,0,0.001,67.158,0.001,149.997c0,82.837,67.156,150,149.995,150s150-67.163,150-150 C299.996,67.156,232.835,0,149.996,0z M149.996,59.147c25.031,0,45.326,20.292,45.326,45.325 c0,25.036-20.292,45.328-45.326,45.328s-45.325-20.292-45.325-45.328C104.671,79.439,124.965,59.147,149.996,59.147z M168.692,212.557h-0.001v16.41v2.028h-18.264h-0.864H83.86c0-44.674,24.302-60.571,40.245-74.843 c7.724,4.15,16.532,6.531,25.892,6.601c9.358-0.07,18.168-2.451,25.887-6.601c7.143,6.393,15.953,13.121,23.511,22.606h-7.275 v10.374v13.051h-13.054h-10.374V212.557z M218.902,228.967v23.425h-16.41v-23.425h-23.428v-16.41h23.428v-23.425H218.9v23.425 h23.423v16.41H218.902z"/></g>';
var btnEdit = '<g data-action="edit" id="btnUpdateNode" class="btn" transform="matrix(0.14,0,0,0.14,50,0)"><rect style="opacity:0" x="0" y="0" height="300" width="300" /><path fill="#686868" d="M149.996,0C67.157,0,0.001,67.161,0.001,149.997S67.157,300,149.996,300s150.003-67.163,150.003-150.003 S232.835,0,149.996,0z M221.302,107.945l-14.247,14.247l-29.001-28.999l-11.002,11.002l29.001,29.001l-71.132,71.126 l-28.999-28.996L84.92,186.328l28.999,28.999l-7.088,7.088l-0.135-0.135c-0.786,1.294-2.064,2.238-3.582,2.575l-27.043,6.03 c-0.405,0.091-0.817,0.135-1.224,0.135c-1.476,0-2.91-0.581-3.973-1.647c-1.364-1.359-1.932-3.322-1.512-5.203l6.027-27.035 c0.34-1.517,1.286-2.798,2.578-3.582l-0.137-0.137L192.3,78.941c1.678-1.675,4.404-1.675,6.082,0.005l22.922,22.917 C222.982,103.541,222.982,106.267,221.302,107.945z"/></g>';
var btnDel = '<g data-action="delete" id="btnRemoveNode" class="btn" transform="matrix(0.14,0,0,0.14,100,0)"><rect style="opacity:0" x="0" y="0" height="300" width="300" /><path fill="#686868" d="M112.782,205.804c10.644,7.166,23.449,11.355,37.218,11.355c36.837,0,66.808-29.971,66.808-66.808 c0-13.769-4.189-26.574-11.355-37.218L112.782,205.804z"/> <path stroke="#686868" fill="#686868" d="M150,83.542c-36.839,0-66.808,29.969-66.808,66.808c0,15.595,5.384,29.946,14.374,41.326l93.758-93.758 C179.946,88.926,165.595,83.542,150,83.542z"/><path stroke="#686868" fill="#686868" d="M149.997,0C67.158,0,0.003,67.161,0.003,149.997S67.158,300,149.997,300s150-67.163,150-150.003S232.837,0,149.997,0z M150,237.907c-48.28,0-87.557-39.28-87.557-87.557c0-48.28,39.277-87.557,87.557-87.557c48.277,0,87.557,39.277,87.557,87.557 C237.557,198.627,198.277,237.907,150,237.907z"/></g>';
getOrgChart.themes.monica.box += '<g transform="matrix(1,0,0,1,350,10)">'
+ btnAdd
+ btnEdit
+ btnDel
+ '</g>';
For Listeners:
document.getElementById("btnInsertNode").addEventListener("click", function () {
orgchart.insertNode(2, { Name: "New Node" }/*, id optional*/);
});
document.getElementById("btnUpdateNode").addEventListener("click", function () {
orgchart.updateNode(7, 1, { Name: "7 New the parent node is 1" });
});
document.getElementById("btnRemoveNode").addEventListener("click", function () {
orgchart.removeNode(6);
});
This should help you: http://www.getorgchart.com/QuickStart/Methods/editNodeMethods.html
Related
SVG elements to zoom whole SVG group on click or mouseover
I would like to use the circles within my SVG file to trigger a zoom in centred on the circle. I have got it working with a div acting as the trigger for the zoom but if I instead apply id="pin" to one of the circle elements within the SVG it no longer zooms in. Can anyone tell me why this is? Is there a better way for me to achieve what I am trying to do? Ideally, I would like it to be possible to click to zoom and then to access other interactivity within the SVG while zoomed in. If this is not possible is there a simple way to zoom and pan an SVG and to be able to access SVG interactivity while zoomed? If I have missed something obvious please forgive me, I’m very much still learning the basics! Rough example: CodePen link <div id="pin">click to trigger zoom</div> <div class="map" id="mapFrame"> <svg class="image" id="mapSVG" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1920 1442.5"" style=" enable-background:new 0 0 1920 924.9;" xml:space="preserve"> <g id="Layer_7" data-name="Layer 7"> <image width="1800" height="1350" transform="translate(0) scale(1.069)" opacity="0.3" xlink:href="https://media.npr.org/assets/img/2020/07/04/seamus-coronavirus-d3-world-map-20200323_wide-a3888a851b91a905e9ad054ea03e177e23620015.png" /> </g> <g id="one"> <circle cx="929.664" cy="944.287" r="81.191"/> </g> <g id="two"> <circle cx="638.164" cy="456.863" r="81.191" /> </g> <g id="three"> <circle cx="1266.164" cy="498.868" r="81.191" /> </g> </svg> </div> <script src="app.js"></script> svg { width: 100%; height: auto; } #pin { position: absolute; height: 65px; width: 75px; top: 300px; left: 550px; padding: 10px; background-color: yellow; } let imgElement = document.querySelector('#mapFrame'); let pinElement = document.querySelector('#pin'); pinElement.addEventListener('click', function (e) { imgElement.style.transform = 'translate(-' + 0 + 'px,-' + 0 + 'px) scale(2)'; pinElement.style.display = 'none'; }); imgElement.addEventListener('click', function (e) { imgElement.style.transform = null; pinElement.style.display = 'block'; });
When you click on the circle, you are also clicking on the background image as well, triggering two events which is essentially cancelling the zoom. You can see this if you place alert('click 1'); and alert('click 2'); in your listeners. This doesn't happen on the #pin element because it's outside background div and avoids the event bubbling up. This is solved by adding event.stopPropagation(); Code from your CodePen: let imgElement = document.querySelector('#mapFrame'); let pinElement = document.querySelector('#one'); //changed to #one pinElement.addEventListener('click', function (e) { imgElement.style.transform = 'translate(-' + 0 + 'px,-' + 0 + 'px) scale(2)'; pinElement.style.display = 'none'; event.stopPropagation(); //added to prevent bubbling }); imgElement.addEventListener('click', function (e) { imgElement.style.transform = null; pinElement.style.display = 'block'; });
How can i make a svg <rect> box dynamic with changeable <text>?
How can i make a svg rect box dynamic with changeable text ? Like in my code, if the text "Hello" will more than 30 character ? <svg version="1.1" viewBox="0 0 500 500" preserveAspectRatio="xMinYMin meet" class="svg-content"> <g> <rect x="0" y="0" width="100" height="100" stroke-width="5" stroke="#000000" fill="none"></rect> ? <text x="0" y="50" font-family="Verdana" font-size="35" fill="blue">Hello</text> </g> </svg>
Probably the best approach would be to have the rect preset at specific width. Then create tspans to fill the text element, and dynamically resize the rect height as the characters exceed the preset width. Below is an example: <!DOCTYPE HTML> <html> <head> <title>Wrap Text Rectangle</title> </head> <body onload=wrapTextRect()> Place this text:<br> <textarea id="myTextValue" style='width:400px;height:60px;'> Hello! </textarea><br> <button onClick=wrapTextRect()> Wrap text in rect</button> <div id="svgDiv" style='background-color:lightgreen;width:400px;height:400px;'> <svg id="mySVG" width="400" height="400"> <rect id=myRect x=50 y=50 rx=10 ry=10 width=100 fill="#4682b4" stroke='black' stroke-width=5 opacity=.5 /> <text id=myText font-size=14 font-family="arial" fill="white" /> </svg> </div> SVG Source:<br> <textarea id=sourceValue style=width:500px;height:300px></textarea> <script> var NS="http://www.w3.org/2000/svg" //---onload and button--- function wrapTextRect() { //---clear previous--- for(var k=myText.childNodes.length-1;k>=0;k--) myText.removeChild(myText.childNodes.item(k)) var padding=10 var width=+myRect.getAttribute("width")-padding var x=+myRect.getAttribute("x") var y=+myRect.getAttribute("y") var fontSize=+myText.getAttribute("font-size") var text=myTextValue.value var words = text.split(' '); var text_element = document.getElementById('myText'); var tspan_element = document.createElementNS(NS, "tspan"); // Create first tspan element var text_node = document.createTextNode(words[0]); // Create text in tspan element tspan_element.setAttribute("x", x+padding); tspan_element.setAttribute("y", y+padding+fontSize); tspan_element.appendChild(text_node); // Add tspan element to DOM text_element.appendChild(tspan_element); // Add text to tspan element //---[EDIT] a single word that exceeds preset rect with--- if(words.length==1) { var len = tspan_element.getComputedTextLength() if(len>+myRect.getAttribute("width")) myRect.setAttribute("width", len+2*padding) } //---end [EDIT]------------------ for(var i=1; i<words.length; i++) { var len = tspan_element.firstChild.data.length // Find number of letters in string tspan_element.firstChild.data += " " + words[i]; // Add next word if (tspan_element.getComputedTextLength() > width-padding) { tspan_element.firstChild.data = tspan_element.firstChild.data.slice(0, len); // Remove added word var tspan_element = document.createElementNS(NS, "tspan"); // Create new tspan element tspan_element.setAttribute("x", x+padding); tspan_element.setAttribute("dy", fontSize); text_node = document.createTextNode(words[i]); tspan_element.appendChild(text_node); text_element.appendChild(tspan_element); } } var height = text_element.getBBox().height +2*padding; //-- get height plus padding myRect.setAttribute('height', height); //-- change rect height //---show svg source--- sourceValue.value=svgDiv.innerHTML } </script> </body> </html>
How to make svg animation with Animated in react-native
ReactNative: <ScrollView style={styles.container}> <Svg height="100" width="100"> <Circle cx="50" cy="50" r="50" stroke="blue" strokeWidth="2.5" fill="green"/> </Svg> </ScrollView> I want to make Circle scale with Animated.Value. I have tried this : let AnimatedScrollView = Animated.createAnimatedComponent(ScrollView); let AnimatedCircle = Animated.createAnimatedComponent(Circle); <ScrollView style={styles.container}> <Svg height="100" width="100"> <AnimatedCircle cx="50" cy="50" r={this.state.animator} stroke="blue" strokeWidth="2.5" fill="green"/> </Svg> </ScrollView> Then flash back with no error. How can I do? update 2016.8.24 I found a new way instead of requestAnimationFrame : constructor: this.state = { animator: new Animated.Value(0), radius: 1, }; this.state.animator.addListener((p) => { this.setState({ radius: p.value, }); }); render: <Circle cx="50" cy="50" r={this.state.radius} stroke="blue" strokeWidth="2.5" fill="green"/> But here the guides gives advice using it sparingly since it might have performance implications in the future. so what's the best way ?
Use setNativeProps for Much Better Performance. I did some more tinkering and found a more performant method that makes use of addListener and setNativeProps. Constructor constructor(props) { super(props); this.state = { circleRadius: new Animated.Value(50) }; this.state.circleRadius.addListener( (circleRadius) => { this._myCircle.setNativeProps({ r: circleRadius.value.toString() }); }); setTimeout( () => { Animated.spring( this.state.circleRadius, { toValue: 100, friction: 3 } ).start(); }, 2000) } Render render() { return( <Svg height="400" width="400"> <AnimatedCircle ref={ ref => this._myCircle = ref } cx="250" cy="250" r="50" fill="black" /> </Svg> ) } Result And this is what the animation looks like when the 2 second (2000 millisecond) timeout triggers. So, the main thing you needed to change was using setNativeProps instead of using setState in your listener. This makes a native call and bypasses re-calculating the entire component, which in my case was very complex and slow to do. Thanks for leading me towards the listener approach!
i have created a simple svg animations library based on a project by another person, for now, only Paths can be animated but more shapes and animations will be added in the future https://www.npmjs.com/package/react-native-svg-animations
No better answers, and I implemented by adding listener to Animated.Value variable, you can get more info from my question description.
Zoom on multiple areas in d3.js
I'm planning to have a geoJSON map inside my svg alongside other svg elements. I would like to be able to zoom (zoom+pan) in the map and keep the map in the same location with a bounding box. I can accomplish this by using a clipPath to keep the map within a rectangular area. The problem is that I also want to enable zooming and panning on my entire svg. If I do d3.select("svg").call(myzoom); this overrides any zoom I applied to my map. How can I apply zoom to both my entire svg and to my map? That is, I want to be able to zoom+pan on my map when my mouse is in the map's bounding box, and when the mouse is outside the bounding box, zoom+pan on the entire svg. Here's example code: http://bl.ocks.org/nuernber/aeaac0e8edcf7ca93ade. <svg id="svg" width="640" height="480" xmlns="http://www.w3.org/2000/svg" version="1.1"> <defs> <clipPath id="rectClip"> <rect x="150" y="25" width="400" height="400" style="stroke: gray; fill: none;"/> </clipPath> </defs> <g id="outer_group"> <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" /> <g id="svg_map" style="clip-path: url(#rectClip);"> </g> </g> </svg><br/> <script type="text/javascript"> var svg = d3.select("#svg_map"); var mapGroup = svg.append("g"); var projection = d3.geo.mercator(); var path = d3.geo.path().projection(projection); var zoom = d3.behavior.zoom() .translate(projection.translate()) .scale(projection.scale()) .on("zoom", zoomed); mapGroup.call(zoom); var pan = d3.behavior.zoom() .on("zoom", panned); d3.select("svg").call(pan); mapGroup.attr("transform", "translate(200,0) scale(2,2)"); d3.json("ne_110m_admin_0_countries/ne_110m_admin_0_countries.geojson", function(collection) { mapGroup.selectAll("path").data(collection.features) .enter().append("path") .attr("d", path) .attr("id", function(d) { return d.properties.name.replace(/\s+/g, "")}) .style("fill", "gray").style("stroke", "white").style("stroke-width",1); } ); function panned() { var x = d3.event.translate[0]; var y = d3.event.translate[1]; d3.select("#outer_group").attr("transform", "translate("+x+","+y+") scale(" + d3.event.scale + ")"); } function zoomed() { previousScale = d3.event.scale; projection.translate(d3.event.translate).scale(d3.event.scale); translationOffset = d3.event.translate; mapGroup.selectAll("path").attr("d", path); } </script>
You need two zoom behaviours for that. The first one would be attached to the SVG and the second one to the map. In the zoom handlers you would have to take care of taking the appropriate action for each.
DOM Exception 9 when inserting SVG element with Ember/Handlebars
I have a problem using Ember/Handlebars with SVG elements: Controller: display: function() { this.set('isDisplayed', true); } Template: <button {{action display}}>Display</button> <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> {{#if isDisplayed}} <text>Foo</text> {{/if}} </svg> When I click on the button, I get this error: Uncaught Error: NotSupportedError: DOM Exception 9 (ember.js:18709: Metamorph#htmlFunc on the createContextualFragment line) Is Ember/Handlebars correctly handling SVG ? What should I do to make this work ? [EDIT] A little JSBin to see this in action: http://jsbin.com/onejec/2/edit
Handling SVG is somewhat tricky if you want to rely on ember to render DOM elements for you. But as a starting point you could consider creating a svg wrapper like this: App.SVGView = Ember.View.extend({ tagName: 'svg', attributeBindings: ['height', 'width', 'xmlns', 'version'], height: 100, width: 100, xmlns: 'http://www.w3.org/2000/svg', version: '1.1', render: function(buffer) { return buffer.push('<text x="20" y="20" font-family="sans-serif" font-size="20px">Foo!</text>'); } }); And then hook into the render method to inject your text tag. This results in the following HTML markup: <svg id="svg_view" class="ember-view" height="100" width="100" xmlns="http://www.w3.org/2000/svg" version="1.1"> <text x="20" y="20" font-family="sans-serif" font-size="20px">Foo!</text> </svg> Here also your working modified jsbin. Edit If you need to re-render the view based on some properties that might change you could add an observer to the correspondent property and call this.rerender() inside that method, something like this: App.SVGView = Ember.View.extend({ tagName: 'svg', attributeBindings: ['height', 'width', 'xmlns', 'version'], height: 100, width: 100, xmlns: 'http://www.w3.org/2000/svg', version: '1.1', render: function(buffer) { return buffer.push('<text x="20" y="20" font-family="sans-serif" font-size="20px">Foo!</text>'); }, myProperty: 'This value might change', myPropertyChanged: function() { this.rerender(); }.observes('myProperty') }); Hope it helps.