From 7fed2788eb3ba3e22aec9dcbdfacef2135e68f26 Mon Sep 17 00:00:00 2001
From: CupCakeArmy <nicco.borgioli@gmail.com>
Date: Sun, 24 Apr 2016 19:19:50 +0200
Subject: [PATCH] New Modal Button System

---
 README.md              | 25 ++++++++++++++-----
 admin/lib/cust/main.js |  5 ++--
 admin/tools/users.tool | 55 ++++++++++++++++++++++++------------------
 3 files changed, 54 insertions(+), 31 deletions(-)

diff --git a/README.md b/README.md
index 617eea8..7125ce9 100644
--- a/README.md
+++ b/README.md
@@ -50,8 +50,16 @@ The JS library ```tools``` is the backbone of the single tools. It enables the t
 * ```data /js object``` Object containning the infos
 	* ```head /text, HTML (optional)``` Sets the modal title.
 	* ```body /text, HTML (optional)``` Sets the modal body.
-	* ```button /text, HTML (optional)``` Confirm button text.
-	* ```success /function (optional)``` Function called on confirm button press.
+	* ```button /object (optional)``` Buttons in the footer. (Multiple allowed!! :D)
+		* ```type /string (optional)``` Type of boostrap button
+			* primary
+			* success
+			* info
+			* warning
+			* danger
+			* link
+		* ```close /bool (optional)``` Close modal on click
+		* ```callback /function (optional)``` Callback for the button
 	* ```cancel /function (optional)``` Function called on cancel or modal is closed.
 
 ##### Example:
@@ -62,10 +70,15 @@ tools.modal();
 tools.modal({
 	head: 'Download Flash Player!!',
 	body: 'Your browser needs this super important plugin',
-	button: 'DOWNLOAD!',
-	success: function(){
-		some.nasty.virus();
-	},
+	button: {
+		'DOWNLOAD!':{
+			type: 'success',
+			close: true,
+			callback: function(){
+				some.nasty.virusdownload.now();
+			}
+		},
+	},	
 	cancel: function(){
 		console.log('No Virus for you -.-');
 	}
diff --git a/admin/lib/cust/main.js b/admin/lib/cust/main.js
index ee2fb09..5a7c71f 100644
--- a/admin/lib/cust/main.js
+++ b/admin/lib/cust/main.js
@@ -41,13 +41,14 @@ var tools = {
         $('.modalCont .modal-footer').html('<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>');
         var modalBtnId = 0;
         for (var curBtn in attr.button) {
-            if (attr.button[curBtn].type === undefined && attr.button[curBtn].type != '')
+            if (attr.button[curBtn].type === undefined || attr.button[curBtn].type == '')
                 attr.button[curBtn].type = 'primary';
-            $('.modalCont .modal-footer').append('<button type="button" class="btn btn-primary modal-btn-' + modalBtnId + '">' + curBtn + '</button>');
+            $('.modalCont .modal-footer').append('<button type="button" class="btn btn-' + attr.button[curBtn].type + ' modal-btn-' + modalBtnId + '">' + curBtn + '</button>');
             if (attr.button[curBtn].callback !== undefined && typeof(attr.button[curBtn].callback) == 'function')
                 $('.modal-btn-' + modalBtnId).off('click').on('click', attr.button[curBtn].callback);
             if (attr.button[curBtn].close === true)
                 $('.modal-btn-' + modalBtnId).on('click', tools.modalClose);
+            modalBtnId++;
         }
         $('.modalCont').modal('show');
     },
diff --git a/admin/tools/users.tool b/admin/tools/users.tool
index 1219673..feb9ff5 100644
--- a/admin/tools/users.tool
+++ b/admin/tools/users.tool
@@ -89,8 +89,13 @@
 				tools.modal({
 					head: ret.firstname + ' ' + ret.lastname,
 					body: tmp,
-					button: 'OK',
-					success: users.inspectUser
+					button: {
+						'Update': {
+							type: 'success',
+							close: true,
+							callback: users.inspectUser,
+						}
+					}
 				});
 
 			});;
@@ -134,22 +139,29 @@
 			tools.modal({
 				head: 'New User',
 				body: tmp,
-				button: 'Add',
-				success: function() {
-					var newUserData = {};
-					$('.users-user-add-table tr').each(function() {
-						newUserData[$(this).children('td:nth-child(1)').html()] = $(this).children('td:nth-child(2)').html();
-					});
-					amivcore.users.POST({
-						data: newUserData
-					}, function(ret) {
-						if (!ret.hasOwnProperty('_status') || ret['_status'] != 'OK')
-							tools.log(JSON.stringify(ret.responseJSON['_issues']), 'e');
-						console.log(ret);
-						users.get();
-					});
+				button: {
+					'Add': {
+						type: 'success',
+						close: true,
+						callback: function() {
+							var newUserData = {};
+							$('.users-user-add-table tr').each(function() {
+								newUserData[$(this).children('td:nth-child(1)').html()] = $(this).children('td:nth-child(2)').html();
+							});
+							amivcore.users.POST({
+								data: newUserData
+							}, function(ret) {
+								if (!ret.hasOwnProperty('_status') || ret['_status'] != 'OK')
+									tools.log(JSON.stringify(ret.responseJSON['_issues']), 'e');
+								else {
+									tools.log('User Added', 's');
+									users.get();
+								}
+							});
+						}
+					}
 				}
-			})
+			});
 		}
 	};
 
@@ -168,17 +180,14 @@
 					body: '<div class="form-group"><input type="number" value="' + users.page.cur() + '" class="form-control users-go-page"></div>',
 					button: {
 						'Go': {
-							type: 'primary',
+							type: 'success',
 							close: true,
 							callback: function() {
 								users.page.set($('.users-go-page').val());
 							},
-						},
-					},
-					success: function() {
-						users.page.set($('.users-go-page').val());
+						}
 					}
-				})
+				});
 			}
 		},
 		'<span class="glyphicon glyphicon-arrow-right" aria-hidden="true"></span>': {
-- 
GitLab