|
6 | 6 |
|
7 | 7 | var datascource = { |
8 | 8 | 'name': 'Ball game', |
9 | | - 'relationship': '001', |
10 | 9 | 'children': [ |
11 | | - { 'name': 'Football', 'relationship': '110' }, |
12 | | - { 'name': 'Basketball', 'relationship': '110' }, |
13 | | - { 'name': 'Volleyball', 'relationship': '110' } |
| 10 | + { 'name': 'Football' }, |
| 11 | + { 'name': 'Basketball' }, |
| 12 | + { 'name': 'Volleyball' } |
14 | 13 | ] |
15 | 14 | }; |
16 | 15 |
|
|
25 | 24 | 'parentNodeSymbol': 'fa-th-large', |
26 | 25 | 'createNode': function($node, data) { |
27 | 26 | $node[0].id = getId(); |
28 | | - $node.on('click', function(event) { |
29 | | - if (!$(event.target).is('.edge')) { |
30 | | - $('#selected-node').val(data.name).data('node', $node); |
31 | | - } |
32 | | - }); |
33 | 27 | } |
34 | 28 | }) |
| 29 | + .on('click', '.node', function() { |
| 30 | + var $this = $(this); |
| 31 | + $('#selected-node').val($this.find('.title').text()).data('node', $this); |
| 32 | + }) |
35 | 33 | .on('click', '.orgchart', function(event) { |
36 | 34 | if (!$(event.target).closest('.node').length) { |
37 | 35 | $('#selected-node').val(''); |
38 | 36 | } |
39 | 37 | }); |
40 | 38 |
|
41 | 39 | $('input[name="chart-state"]').on('click', function() { |
42 | | - $('#edit-panel, .orgchart').toggleClass('view-state'); |
| 40 | + $('.orgchart').toggleClass('view-state', this.value !== 'view'); |
| 41 | + $('#edit-panel').toggleClass('view-state', this.value === 'view'); |
43 | 42 | if ($(this).val() === 'edit') { |
44 | 43 | $('.orgchart').find('tr').removeClass('hidden') |
45 | 44 | .find('td').removeClass('hidden') |
|
85 | 84 | return; |
86 | 85 | } |
87 | 86 | var nodeType = $('input[name="node-type"]:checked'); |
88 | | - if (nodeType.val() !== 'parent' && !$node) { |
89 | | - alert('Please select one node in orgchart'); |
90 | | - return; |
91 | | - } |
92 | 87 | if (!nodeType.length) { |
93 | 88 | alert('Please select a node type'); |
94 | 89 | return; |
95 | 90 | } |
| 91 | + if (nodeType.val() !== 'parent' && !$('.orgchart').length) { |
| 92 | + alert('Please creat the root node firstly when you want to build up the orgchart from the scratch'); |
| 93 | + return; |
| 94 | + } |
| 95 | + if (nodeType.val() !== 'parent' && !$node) { |
| 96 | + alert('Please select one node in orgchart'); |
| 97 | + return; |
| 98 | + } |
96 | 99 | if (nodeType.val() === 'parent') { |
97 | | - $chartContainer.orgchart('addParent', $chartContainer.find('.node:first'), { 'name': nodeVals[0], 'Id': getId() }); |
| 100 | + if (!$chartContainer.children().length) {// if the original chart has been deleted |
| 101 | + $chartContainer.orgchart({ |
| 102 | + 'data' : { 'name': nodeVals[0] }, |
| 103 | + 'exportButton': true, |
| 104 | + 'exportFilename': 'SportsChart', |
| 105 | + 'parentNodeSymbol': 'fa-th-large', |
| 106 | + 'createNode': function($node, data) { |
| 107 | + $node[0].id = getId(); |
| 108 | + } |
| 109 | + }) |
| 110 | + .find('.orgchart').addClass('view-state'); |
| 111 | + } else { |
| 112 | + $chartContainer.orgchart('addParent', $chartContainer.find('.node:first'), { 'name': nodeVals[0], 'Id': getId() }); |
| 113 | + } |
98 | 114 | } else if (nodeType.val() === 'siblings') { |
99 | 115 | $chartContainer.orgchart('addSiblings', $node, |
100 | 116 | { 'siblings': nodeVals.map(function(item) { return { 'name': item, 'relationship': '110', 'Id': getId() }; }) |
|
121 | 137 | if (!$node) { |
122 | 138 | alert('Please select one node in orgchart'); |
123 | 139 | return; |
| 140 | + } else if ($node[0] === $('.orgchart').find('.node:first')[0]) { |
| 141 | + if (!window.confirm('Are you sure you want to delete the whole chart?')) { |
| 142 | + return; |
| 143 | + } |
124 | 144 | } |
125 | 145 | $('#chart-container').orgchart('removeNodes', $node); |
126 | | - $('#selected-node').data('node', null); |
| 146 | + $('#selected-node').val('').data('node', null); |
127 | 147 | }); |
128 | 148 |
|
129 | 149 | $('#btn-reset').on('click', function() { |
130 | | - $('.orgchart').trigger('click'); |
| 150 | + $('.orgchart').find('.focused').removeClass('focused'); |
| 151 | + $('#selected-node').val(''); |
131 | 152 | $('#new-nodelist').find('input:first').val('').parent().siblings().remove(); |
132 | 153 | $('#node-type-panel').find('input').prop('checked', false); |
133 | 154 | }); |
|
0 commit comments