if (!!$('#JsTreeDiv').data('jstree')) { $('#JsTreeDiv').off('select_node.jstree deselect_node.jstree', self.treeNodeSelected); $('#JsTreeDiv').jstree('refresh'); } else { self.setupJsTree(); }
self.setupJsTree = function () { $('#JsTreeDiv').jstree({ 'core': { 'data': { type: 'GET', url: '../api/GroupMgmt/GetGroupTree', contentType: 'application/json; charset=utf-8', dataType: 'json', data: function (node) { return { customerId: self.customerId(), roleCode: self.selectedRoleCode() }; }, success: function (data) { return data; }, error: function (jqXHR, textStatus, errorThrown) { notifyUser(textStatus + '/' + errorThrown, 'error', 'center'); } }, 'multiple': true }, 'checkbox' : { keep_selected_style : false, three_state: false, visible : true, whole_node : true }, 'plugins' : [ "checkbox" ] }); $('#JsTreeDiv').on('select_node.jstree deselect_node.jstree', self.treeNodeSelected); $('#JsTreeDiv').on('refresh.jstree', function() { $('#JsTreeDiv').on('select_node.jstree deselect_node.jstree', self.treeNodeSelected); }); }; self.treeNodeSelected = function(node, selected) { var groupCode = !!selected ? selected.node.original.value : $('#JsTreeDiv').jstree('get_selected')[0]; $.ajax({ type: 'POST', url: '../api/RoleMgmt/PostUpdateRoleGroupAssociation', data: JSON.stringify({ groupId: groupCode, customerId: self.customerId(), roleCode: self.selectedRoleCode(), checkboxValue: selected.node.state.selected }), contentType: 'application/json; charset=utf-8', dataType: 'json', success: function(data) { if (!!data.Error) { //an error occured...undo the selection/deselection of the node selected.node.state.selected ? $('#JsTreeDiv').jstree('deselect_node', groupCode, true) : $('#JsTreeDiv').jstree('select_node', groupCode, true); notifyUser(data.Error, 'error', 'center'); } else { //add or remove group from array and sort if needed if (selected.node.state.selected) { self.userGroups.push({ group_nm: selected.node.original.text, group_cd: selected.node.original.value }); self.userGroups.sort(function(left, right) { return left.group_nm == right.group_nm ? 0 : (left.group_nm < right.group_nm ? -1 : 1); }); } else { self.userGroups.remove(function(item) { return item.group_cd === selected.node.original.value; }); } //disable tabs 2 and 3 if there are no groups in the dropdown menu self.userGroups().length === 0 ? $('#tabs').tabs('option', 'disabled', [2, 3]) : $('#tabs').tabs('option', 'disabled', []); } }, error: function(jqXHR, textStatus, errorThrown) { notifyUser(textStatus + '/' + errorThrown, 'error', 'center'); } }); };