| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  | /* | 
					
						
							|  |  |  |  * groups.js | 
					
						
							| 
									
										
										
										
											2020-02-16 13:59:41 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:43:38 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii).
 | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:43:38 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:43:38 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:43:38 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:43:38 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var count = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $(document).ready(function () { | 
					
						
							| 
									
										
										
										
											2019-08-21 04:37:17 +02:00
										 |  |  |     updateListButtons(); | 
					
						
							| 
									
										
										
										
											2021-09-20 06:39:10 +02:00
										 |  |  |     $('.clone-transaction').click(cloneTransaction); | 
					
						
							| 
									
										
										
										
											2023-10-21 07:38:08 +02:00
										 |  |  |     $('.clone-transaction-and-edit').click(cloneTransactionAndEdit); | 
					
						
							| 
									
										
										
										
											2019-08-21 04:37:17 +02:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-10-24 17:27:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-08-21 04:37:17 +02:00
										 |  |  | function updateListButtons() { | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  |     // top button to select all / deselect all:
 | 
					
						
							|  |  |  |     $('input[name="select-all"]').change(function () { | 
					
						
							|  |  |  |         if (this.checked) { | 
					
						
							|  |  |  |             checkAll(); | 
					
						
							|  |  |  |             countChecked(); | 
					
						
							|  |  |  |             updateActionButtons(); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             uncheckAll(); | 
					
						
							|  |  |  |             countChecked(); | 
					
						
							|  |  |  |             updateActionButtons(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // click the mass edit button:
 | 
					
						
							|  |  |  |     $('.mass-edit').click(goToMassEdit); | 
					
						
							|  |  |  |     // click the bulk edit button:
 | 
					
						
							|  |  |  |     $('.bulk-edit').click(goToBulkEdit); | 
					
						
							|  |  |  |     // click the delete button:
 | 
					
						
							|  |  |  |     $('.mass-delete').click(goToMassDelete); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // click checkbox:
 | 
					
						
							|  |  |  |     $('.mass-select').unbind('change').change(function () { | 
					
						
							|  |  |  |         countChecked(); | 
					
						
							|  |  |  |         updateActionButtons(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-08-21 04:37:17 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-23 07:02:30 +02:00
										 |  |  | function getBaseUrl() { | 
					
						
							|  |  |  |     // go to specially crafted URL:
 | 
					
						
							|  |  |  |     var bases = document.getElementsByTagName('base'); | 
					
						
							|  |  |  |     var baseHref = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (bases.length > 0) { | 
					
						
							|  |  |  |         baseHref = bases[0].href; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-23 07:04:47 +02:00
										 |  |  |     if (null !== baseHref && '/' === baseHref.slice(-1)) { | 
					
						
							|  |  |  |         baseHref = baseHref.slice(0, -1); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-23 07:02:30 +02:00
										 |  |  |     console.log('baseHref for mass edit is "' + baseHref + '".'); | 
					
						
							|  |  |  |     return baseHref; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @returns {boolean} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function goToMassEdit() { | 
					
						
							| 
									
										
										
										
											2023-08-23 07:02:30 +02:00
										 |  |  |     var baseHref = getBaseUrl(); | 
					
						
							| 
									
										
										
										
											2023-09-20 06:18:04 +02:00
										 |  |  |     console.log('Mass edit URL is ' + mass_edit_url + '/' + getCheckboxes()); | 
					
						
							|  |  |  |     window.location.href = mass_edit_url + '/' + getCheckboxes(); | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function goToBulkEdit() { | 
					
						
							| 
									
										
										
										
											2023-08-23 07:02:30 +02:00
										 |  |  |     var baseHref = getBaseUrl(); | 
					
						
							| 
									
										
										
										
											2023-09-20 06:18:04 +02:00
										 |  |  |     console.log('Bulk edit URL is ' + bulk_edit_url + '/' + getCheckboxes()); | 
					
						
							|  |  |  |     window.location.href = bulk_edit_url + '/' + getCheckboxes(); | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function goToMassDelete() { | 
					
						
							| 
									
										
										
										
											2023-08-23 07:02:30 +02:00
										 |  |  |     var baseHref = getBaseUrl(); | 
					
						
							| 
									
										
										
										
											2023-09-20 06:18:04 +02:00
										 |  |  |     console.log('Mass delete URL is ' + mass_delete_url + '/' + getCheckboxes()); | 
					
						
							|  |  |  |     window.location.href = mass_delete_url + '/' + getCheckboxes(); | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @returns {Array} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | function getCheckboxes() { | 
					
						
							|  |  |  |     "use strict"; | 
					
						
							|  |  |  |     var list = []; | 
					
						
							|  |  |  |     $.each($('.mass-select'), function (i, v) { | 
					
						
							|  |  |  |         var checkbox = $(v); | 
					
						
							|  |  |  |         if (checkbox.prop('checked')) { | 
					
						
							|  |  |  |             // add to list.
 | 
					
						
							|  |  |  |             list.push(checkbox.val()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return list; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function countChecked() { | 
					
						
							|  |  |  |     count = $('.mass-select:checked').length; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function checkAll() { | 
					
						
							|  |  |  |     $('.mass-select').prop('checked', true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function uncheckAll() { | 
					
						
							|  |  |  |     $('.mass-select').prop('checked', false); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function updateActionButtons() { | 
					
						
							|  |  |  |     if (0 !== count) { | 
					
						
							|  |  |  |         $('.action-menu').show(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // also update labels:
 | 
					
						
							| 
									
										
										
										
											2021-09-25 05:35:29 +02:00
										 |  |  |         $('.mass-edit span.txt').text(edit_selected_txt + ' (' + count + ')'); | 
					
						
							|  |  |  |         $('.bulk-edit span.txt').text(edit_bulk_selected_txt + ' (' + count + ')'); | 
					
						
							|  |  |  |         $('.mass-delete span.txt').text(delete_selected_txt + ' (' + count + ')'); | 
					
						
							| 
									
										
										
										
											2019-07-04 17:31:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (0 === count) { | 
					
						
							|  |  |  |         $('.action-menu').hide(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-20 06:39:10 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-07-04 13:29:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 06:39:10 +02:00
										 |  |  | function cloneTransaction(e) { | 
					
						
							|  |  |  |     var button = $(e.currentTarget); | 
					
						
							|  |  |  |     var groupId = parseInt(button.data('id')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $.post(cloneGroupUrl, { | 
					
						
							|  |  |  |         _token: token, | 
					
						
							|  |  |  |         id: groupId | 
					
						
							|  |  |  |     }).done(function (data) { | 
					
						
							|  |  |  |         // lame but it works
 | 
					
						
							|  |  |  |         location.href = data.redirect; | 
					
						
							|  |  |  |     }).fail(function () { | 
					
						
							|  |  |  |         console.error('I failed :('); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-10-21 07:38:08 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | function cloneTransactionAndEdit(e) { | 
					
						
							|  |  |  |     var button = $(e.currentTarget); | 
					
						
							|  |  |  |     var groupId = parseInt(button.data('id')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $.post(cloneAndEditUrl, { | 
					
						
							|  |  |  |         id: groupId | 
					
						
							|  |  |  |     }).done(function (data) { | 
					
						
							|  |  |  |         // lame but it works
 | 
					
						
							|  |  |  |         location.href = data.redirect; | 
					
						
							|  |  |  |     }).fail(function () { | 
					
						
							|  |  |  |         console.error('I failed :('); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  | } |