Commit cd89331d authored by Vipul Basapati's avatar Vipul Basapati

Refactored the whole Menu module js to admin.js and solved the modal not...

Refactored the whole Menu module js to admin.js and solved the modal not showing error by removing setTimeout function
parent c95669b7
......@@ -43,7 +43,6 @@ var Backend = {}; // common variable used in all the files of the backend
},
ajaxrequest: function (url, method, data, csrf, callback) {
console.log(url);
var request = new XMLHttpRequest();
var loadingIcon = jQuery(".loading");
if (window.XMLHttpRequest) {
......@@ -56,11 +55,9 @@ var Backend = {}; // common variable used in all the files of the backend
request.open(method, url, true);
request.onloadstart = function() {
console.log('been here');
loadingIcon.show();
};
request.onloadend = function() {
console.log('been there');
loadingIcon.hide();
};
request.setRequestHeader('X-CSRF-TOKEN', csrf);
......@@ -85,7 +82,6 @@ var Backend = {}; // common variable used in all the files of the backend
jsontoformdata: function (srcjson) {
if (typeof srcjson !== "object")
if (typeof console !== "undefined") {
console.log("\"srcjson\" is not a JSON object");
return null;
}
u = encodeURIComponent;
......@@ -398,13 +394,100 @@ var Backend = {}; // common variable used in all the files of the backend
modal: jQuery("#showMenuModal"),
document: jQuery("document"),
addCustomUrlForm: "#menu-add-custom-url",
formUrl: ""
addModuleToMenuButton: ".add-module-to-menu",
removeMenuItemButton : ".remove-menu-item",
editMenuItemButton : ".edit-menu-item",
formUrl: "",
},
methods: {
getNewId : function(str) {
var arr = str.match(/"id":[0-9]+/gi);
if(arr) {
$.each(arr, function(index, item) {
arr[index] = parseInt(item.replace('"id":', ''));
});
return Math.max.apply(Math, arr) + 1;
}
return 1;
},
findItemById : function(item, id) {
if(item.id == id) {
return item;
}
var found = false;
var foundItem;
if(item.children){
$.each(item.children, function(index, childItem){
foundItem = Backend.Menu.methods.findItemById(childItem, id);
if(foundItem)
{
found = true;
return false;
}
});
}
if(found)
{
return foundItem;
}
return null;
},
addMenuItem : function(obj) {
Backend.Menu.selectors.menuItemContainer.nestable('add', {
"id": Backend.Menu.methods.getNewId(Backend.Menu.selectors.menuItemsData.val()),
"content": obj.name,
"name": obj.name,
"url": obj.url,
"url_type" : obj.url_type,
"open_in_new_tab": obj.open_in_new_tab,
"icon": obj.icon,
"view_permission_id": obj.view_permission_id
});
Backend.Menu.selectors.menuItemsData.val(
JSON.stringify(
Backend.Menu.selectors.menuItemContainer.nestable('serialise')
)
);
},
editMenuItem : function(obj) {
var newObject = {
"id": obj.id,
"content": obj.name,
"name": obj.name,
"url": obj.url,
"url_type": obj.url_type,
"open_in_new_tab": obj.open_in_new_tab,
"icon": obj.icon,
"view_permission_id": obj.view_permission_id
};
var menuItems = Backend.Menu.selectors.menuItemContainer.nestable('serialise');
var itemData;
$.each(menuItems, function(index, item){
itemData = Backend.Menu.methods.findItemById(item, id);
if(itemData) { return false; }
});
if(itemData.children) {
newObject.children = itemData.children;
}
Backend.Menu.selectors.menuItemContainer.nestable('replace', newObject);
Backend.Menu.selectors.menuItemsData.val(
JSON.stringify(
Backend.Menu.selectors.menuItemContainer.nestable('serialise')
)
);
}
},
init: function () {
this.addHandlers();
},
addHandlers: function () {
var context = this;
var formName = "_add_custom_url_form";
......@@ -420,18 +503,16 @@ var Backend = {}; // common variable used in all the files of the backend
});
this.selectors.addCustomUrlButton.click(function() {
let title = context.selectors.addCustomUrlButton.attr("data-header");
var title = context.selectors.addCustomUrlButton.attr("data-header");
context.selectors.modal.find(".modal-title").html(title);
context.selectors.modal.modal("show");
// setTimeout(function() {
callback = {
success: function (request) {
console.log(request);
if (request.status >= 200 && request.status < 400) {
// Success!
context.selectors.modal.find(".modal-body").html(request.responseText);
jQuery(document).find(context.selectors.modal).find(".view-permission-block").remove();
// jQuery(document).find(context.selectors.modal).find(".view-permission-block").remove();
jQuery(document).find(context.selectors.addCustomUrlForm).removeClass("hidden");
}
},
......@@ -441,6 +522,80 @@ var Backend = {}; // common variable used in all the files of the backend
}
Backend.Utils.ajaxrequest(context.selectors.formUrl + "/" + formName, "get", {}, Backend.Utils.csrf, callback);
});
jQuery(document).on("submit", context.selectors.addCustomUrlForm, function(e){
e.preventDefault();
var formData = jQuery(this).serializeArray().reduce(function(obj, item) {
obj[item.name] = item.value;
return obj;
}, {});
if(formData.name.length > 0) {
if(formData.id.length > 0) {
context.methods.editMenuItem(formData);
} else {
context.methods.addMenuItem(formData);
}
context.selectors.modal.modal("hide");
}
});
jQuery(document).on("click", context.selectors.addModuleToMenuButton, function(){
var dataObj = {
id: $(this).attr("data-id"),
name: $(this).attr("data-name"),
url: $(this).attr("data-url"),
url_type: $(this).attr("data-url_type"),
open_in_new_tab: $(this).attr("data-open_in_new_tab"),
view_permission_id: $(this).attr("data-view_permission_id"),
}
context.methods.addMenuItem(dataObj);
});
jQuery(document).on("click", context.selectors.removeMenuItemButton, function() {
context.selectors.menuItemContainer.nestable('remove', jQuery(this).parents(".dd-item").first().attr("data-id"));
Backend.Menu.selectors.menuItemsData.val(
JSON.stringify(
Backend.Menu.selectors.menuItemContainer.nestable('serialise')
)
);
});
jQuery(document).on("click", context.selectors.editMenuItemButton, function() {
id = jQuery(this).parents(".dd-item").first().attr("data-id");
var menuItems = context.selectors.menuItemContainer.nestable('serialise');
var itemData;
$.each(menuItems, function(index, item){
itemData = context.methods.findItemById(item, id);
if(itemData) { return false; }
});
if(itemData.id != undefined && itemData.id == id)
{
callback = {
success: function (request) {
if (request.status >= 200 && request.status < 400) {
// Success!
context.selectors.modal.find(".modal-body").html(request.responseText);
context.selectors.modal.find(".modal-dialog .modal-content .modal-header .modal-title").html("Edit: " + itemData.name);
$(document).find(context.selectors.modal).find(".mi-id").val(itemData.id);
$(document).find(context.selectors.modal).find(".mi-name").val(itemData.name);
$(document).find(context.selectors.modal).find(".mi-url").val(itemData.url);
$(document).find(context.selectors.modal).find(".mi-url_type_"+itemData.url_type).prop("checked", true);
if(itemData.open_in_new_tab == 1) {
$(document).find(context.selectors.modal).find(".mi-open_in_new_tab").prop("checked", true);
}
$(document).find(context.selectors.modal).find(".mi-icon").val(itemData.icon);
$(document).find(context.selectors.modal).find(".mi-view_permission_id").val(itemData.view_permission_id);
$(document).find("#menu-add-custom-url").removeClass("hidden");
context.selectors.modal.modal("show");
}
},
error: function (request) {
//Do Something
}
}
Backend.Utils.ajaxrequest(context.selectors.formUrl + "/" + formName, "get", {}, Backend.Utils.csrf, callback);
}
});
}
},
......
......@@ -56,147 +56,7 @@
@section("after-scripts")
{{ Html::script('js/nestable2/jquery.nestable.js') }}
<script type="text/javascript">
var formName = '_add_custom_url_form';
var lastId = null;
Backend.Menu.selectors.formUrl = "{{route('admin.menus.getform')}}";
Backend.Menu.init();
var getNewId = function(str) {
var arr = str.match(/"id":[0-9]+/gi);
if(arr) {
$.each(arr, function(index, item) {
arr[index] = parseInt(item.replace('"id":', ''));
});
return Math.max.apply(Math, arr) + 1;
}
return 1;
}
var addMenuItem = function(obj) {
$('#menu-items').nestable('add', {
"id": getNewId($(".menu-items-field").val()),
"content": obj.name,
"name": obj.name,
"url": obj.url,
"url_type" : obj.url_type,
"open_in_new_tab": obj.open_in_new_tab,
"icon": obj.icon,
"view_permission_id": obj.view_permission_id
});
$(".menu-items-field").val(JSON.stringify($('#menu-items').nestable('serialise')));
}
var editMenuItem = function(obj) {
var newObject = {
"id": obj.id,
"content": obj.name,
"name": obj.name,
"url": obj.url,
"url_type": obj.url_type,
"open_in_new_tab": obj.open_in_new_tab,
"icon": obj.icon,
"view_permission_id": obj.view_permission_id
};
var menuItems = $("#menu-items").nestable('serialise');
var itemData;
$.each(menuItems, function(index, item){
itemData = findItemById(item, id);
if(itemData) { return false; }
});
if(itemData.children) {
newObject.children = itemData.children;
}
$('#menu-items').nestable('replace', newObject);
$(".menu-items-field").val(JSON.stringify($('#menu-items').nestable('serialise')));
}
$(document).on("submit", "#menu-add-custom-url", function(e){
e.preventDefault();
var formData = $(this).serializeArray().reduce(function(obj, item) {
obj[item.name] = item.value;
return obj;
}, {});
if(formData.name.length > 0) {
if(formData.id.length > 0) {
editMenuItem(formData);
} else {
addMenuItem(formData);
}
$("#showMenuModal").modal("hide");
}
});
$(document).on("click", ".add-module-to-menu", function(){
var dataObj = {
id: $(this).attr("data-id"),
name: $(this).attr("data-name"),
url: $(this).attr("data-url"),
url_type: $(this).attr("data-url_type"),
open_in_new_tab: $(this).attr("data-open_in_new_tab"),
view_permission_id: $(this).attr("data-view_permission_id"),
}
addMenuItem(dataObj);
});
var findItemById = function(item, id) {
if(item.id == id) {
return item;
}
var found = false;
var foundItem;
if(item.children){
$.each(item.children, function(index, childItem){
foundItem = findItemById(childItem, id);
if(foundItem)
{
console.log(foundItem);
found = true;
return false;
}
});
}
if(found)
{
return foundItem;
}
return null;
};
$(document).ready(function(){
$(document).on("click", ".edit-menu-item", function() {
id = $(this).parents(".dd-item").first().attr("data-id");
$("#showMenuModal").modal("show");
var menuItems = $("#menu-items").nestable('serialise');
var itemData;
$.each(menuItems, function(index, item){
itemData = findItemById(item, id);
//console.log(itemData);
if(itemData) { return false; }
});
if(itemData.id != undefined && itemData.id == id)
{
setTimeout(function() {
$("#showMenuModal").find(".modal-dialog .modal-content .modal-header .modal-title").html("Edit: "+itemData.name);
$(document).find("#showMenuModal .mi-id").val(itemData.id);
$(document).find("#showMenuModal .mi-name").val(itemData.name);
$(document).find("#showMenuModal .mi-url").val(itemData.url);
$(document).find("#showMenuModal .mi-url_type_"+itemData.url_type).prop("checked", true);
if(itemData.open_in_new_tab == 1) {
$(document).find("#showMenuModal .mi-open_in_new_tab").prop("checked", true);
}
$(document).find("#showMenuModal .mi-icon").val(itemData.icon);
if(itemData.view_permission_id) {
$(document).find("#showMenuModal .mi-view_permission_id").val(itemData.view_permission_id);
} else {
$(document).find("#showMenuModal .view-permission-block").remove();
}
$(document).find("#menu-add-custom-url").removeClass("hidden");
}, 500 );
return;
}
});
$(document).on("click", ".remove-menu-item", function() {
$("#menu-items").nestable('remove', $(this).parents(".dd-item").first().attr("data-id"));
$(".menu-items-field").val(JSON.stringify($("#menu-items").nestable('serialise')));
});
});
</script>
@endsection
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment