home/autoph/public_html/projects/Rating-AutoHub/public/js/auth/roles.js 0000644 00000007375 15025017115 0022246 0 ustar 00 (function(e){
"use strict";
var search_type_filter = [];
var rolesList = null;
var url = window.location.href;
var id = url.match(/\/(\d+)$/)[1];
function fetchAbilities(){
$.ajax({
type: "GET",
url: "/getAbilities/" + id,
dataType: 'json',
data: null,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function(result){
$('#abilities-panel').jstree({
core:{
data: result.availableAbilities
},
checkbox: {
three_state: true
},
plugins:['checkbox']
// });
}).on('ready.jstree', function(){
var tree = $('#abilities-panel').jstree(true);
result.abilities.forEach(function(ability){
// console.log(ability);
var node = tree.get_json('#', { flat: true }).find(function(n) {
return n.text === ability;
});
if (node) {
tree.check_node(node.id);
}
})
});
},
error: function(error){
// toastRWithTime("Not sent",'error');
}
});
}
function submitRoleAbilities(){
$('#user_abilities').on('submit', function(e){
e.preventDefault();
var selectedNodes = $('#abilities-panel').jstree('get_checked', true);
var selectedData = [];
$.each(selectedNodes, function(index, node) {
var nodeId = node.id;
var nodeText = node.text;
var parentNode = $('#abilities-panel').jstree('get_parent', node);
var parentText = '';
if (parentNode !== '#') {
parentText = $('#abilities-panel').jstree('get_node', parentNode).text;
}
selectedData.push({
nodeId: nodeId,
nodeText: nodeText,
parentNodeText: parentText
});
});
// alert(selectedData)
// return;
$.ajax({
type: "POST",
url: "/updateRoleAbilities",
dataType: 'json',
data: {
id:id,
abilities:selectedData
},
// processData: false,
// contentType: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
success: function(result){
toastRWithTime("Success","success");
// window.location.href = '/userIndex';
},
error: function(error){
console.log(error);
toastRWithTime(error.responseJSON.message,"error")
}
});
});
}
$(function(){
fetchAbilities();
submitRoleAbilities();
$('.check-all').on('click', function(e){
e.preventDefault();
var tree = $('#abilities-panel').jstree(true);
tree.check_all();
toastRWithTime("all abilities checked","Success");
});
$('.uncheck-all').on('click', function(e){
e.preventDefault();
var tree = $('#abilities-panel').jstree(true);
tree.uncheck_all();
toastRWithTime("all abilities unchecked","error");
});
});
})();