2020-04-13 19:11:13 +02:00
var collapsedGroups = { } ;
var shoppingListTable = $ ( '#shoppinglist-table' ) . DataTable ( {
2018-07-09 19:27:22 +02:00
'order' : [ [ 1 , 'asc' ] ] ,
2018-09-24 13:02:52 +02:00
"orderFixed" : [ [ 3 , 'asc' ] ] ,
2018-07-09 19:27:22 +02:00
'columnDefs' : [
2018-09-24 13:02:52 +02:00
{ 'orderable' : false , 'targets' : 0 } ,
2020-01-03 14:18:56 +01:00
{ 'searchable' : false , "targets" : 0 } ,
2018-09-24 13:02:52 +02:00
{ 'visible' : false , 'targets' : 3 }
2018-07-09 19:27:22 +02:00
] ,
2018-09-24 13:02:52 +02:00
'rowGroup' : {
2020-04-13 19:11:13 +02:00
dataSrc : 3 ,
startRender : function ( rows , group )
{
var collapsed = ! ! collapsedGroups [ group ] ;
var toggleClass = collapsed ? "fa-caret-right" : "fa-caret-down" ;
rows . nodes ( ) . each ( function ( row )
{
row . style . display = collapsed ? "none" : "" ;
} ) ;
return $ ( "<tr/>" )
. append ( '<td colspan="' + rows . columns ( ) [ 0 ] . length + '">' + group + ' <span class="fa fa-fw ' + toggleClass + '"/></td>' )
. attr ( "data-name" , group )
. toggleClass ( "collapsed" , collapsed ) ;
}
2018-09-08 08:56:32 +02:00
}
2018-07-09 19:27:22 +02:00
} ) ;
2019-01-05 20:06:35 +01:00
$ ( '#shoppinglist-table tbody' ) . removeClass ( "d-none" ) ;
2019-03-04 17:43:12 +01:00
shoppingListTable . columns . adjust ( ) . draw ( ) ;
2018-07-09 19:27:22 +02:00
2020-04-13 19:11:13 +02:00
$ ( document ) . on ( "click" , "tr.dtrg-group" , function ( )
{
var name = $ ( this ) . data ( 'name' ) ;
collapsedGroups [ name ] = ! collapsedGroups [ name ] ;
shoppingListTable . draw ( ) ;
} ) ;
2019-10-15 19:59:14 +02:00
$ ( "#search" ) . on ( "keyup" , Delay ( function ( )
2018-07-09 19:27:22 +02:00
{
var value = $ ( this ) . val ( ) ;
if ( value === "all" )
{
value = "" ;
}
2019-01-19 00:37:21 -07:00
2018-07-09 19:27:22 +02:00
shoppingListTable . search ( value ) . draw ( ) ;
2019-10-15 19:59:14 +02:00
} , 200 ) ) ;
2018-07-09 19:27:22 +02:00
2018-09-24 19:13:53 +02:00
$ ( "#status-filter" ) . on ( "change" , function ( )
{
var value = $ ( this ) . val ( ) ;
if ( value === "all" )
{
value = "" ;
}
// Transfer CSS classes of selected element to dropdown element (for background)
$ ( this ) . attr ( "class" , $ ( "#" + $ ( this ) . attr ( "id" ) + " option[value='" + value + "']" ) . attr ( "class" ) + " form-control" ) ;
2019-01-19 00:37:21 -07:00
2018-09-24 19:13:53 +02:00
shoppingListTable . column ( 4 ) . search ( value ) . draw ( ) ;
} ) ;
2019-04-20 17:04:40 +02:00
$ ( "#selected-shopping-list" ) . on ( "change" , function ( )
{
var value = $ ( this ) . val ( ) ;
window . location . href = U ( '/shoppinglist?list=' + value ) ;
} ) ;
2020-04-19 08:51:02 -04:00
$ ( ".status-filter-message" ) . on ( "click" , function ( )
2018-09-24 19:13:53 +02:00
{
var value = $ ( this ) . data ( "status-filter" ) ;
$ ( "#status-filter" ) . val ( value ) ;
$ ( "#status-filter" ) . trigger ( "change" ) ;
} ) ;
2019-04-20 17:04:40 +02:00
$ ( "#delete-selected-shopping-list" ) . on ( "click" , function ( )
{
2020-09-08 18:10:30 +02:00
var objectName = SanitizeHtml ( $ ( "#selected-shopping-list option:selected" ) . text ( ) ) ;
2019-04-20 17:04:40 +02:00
var objectId = $ ( "#selected-shopping-list" ) . val ( ) ;
bootbox . confirm ( {
2019-05-01 20:19:18 +02:00
message : _ _t ( 'Are you sure to delete shopping list "%s"?' , objectName ) ,
2019-09-24 10:24:47 +02:00
closeButton : false ,
2019-04-20 17:04:40 +02:00
buttons : {
confirm : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'Yes' ) ,
2019-04-20 17:04:40 +02:00
className : 'btn-success'
} ,
cancel : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'No' ) ,
2019-04-20 17:04:40 +02:00
className : 'btn-danger'
}
} ,
2020-08-30 12:18:16 +02:00
callback : function ( result )
2019-04-20 17:04:40 +02:00
{
if ( result === true )
{
Grocy . Api . Delete ( 'objects/shopping_lists/' + objectId , { } ,
2020-08-30 12:18:16 +02:00
function ( result )
2019-04-20 17:04:40 +02:00
{
window . location . href = U ( '/shoppinglist' ) ;
} ,
2020-08-30 12:18:16 +02:00
function ( xhr )
2019-04-20 17:04:40 +02:00
{
console . error ( xhr ) ;
}
) ;
}
}
} ) ;
} ) ;
$ ( document ) . on ( 'click' , '.shoppinglist-delete-button' , function ( e )
2017-04-21 15:36:04 +02:00
{
2018-09-24 09:30:26 +02:00
e . preventDefault ( ) ;
2019-01-19 00:37:21 -07:00
2020-04-13 18:26:53 +02:00
// Remove the focus from the current button
// to prevent that the tooltip stays until clicked anywhere else
document . activeElement . blur ( ) ;
2018-08-11 14:07:44 +02:00
var shoppingListItemId = $ ( e . currentTarget ) . attr ( 'data-shoppinglist-id' ) ;
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( ) ;
2018-08-11 14:07:44 +02:00
2020-08-30 12:18:16 +02:00
Grocy . Api . Delete ( 'objects/shopping_list/' + shoppingListItemId , { } ,
2017-04-21 15:36:04 +02:00
function ( result )
{
2020-01-28 19:27:18 +01:00
animateCSS ( "#shoppinglistitem-" + shoppingListItemId + "-row" , "fadeOut" , function ( )
2018-08-11 14:07:44 +02:00
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2020-01-28 19:27:18 +01:00
$ ( "#shoppinglistitem-" + shoppingListItemId + "-row" ) . remove ( ) ;
2018-11-17 14:50:52 +01:00
OnListItemRemoved ( ) ;
2018-08-11 14:07:44 +02:00
} ) ;
2017-04-21 15:36:04 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2017-04-21 15:36:04 +02:00
console . error ( xhr ) ;
}
) ;
} ) ;
$ ( document ) . on ( 'click' , '#add-products-below-min-stock-amount' , function ( e )
{
2019-04-20 17:04:40 +02:00
Grocy . Api . Post ( 'stock/shoppinglist/add-missing-products' , { "list_id" : $ ( "#selected-shopping-list" ) . val ( ) } ,
2017-04-21 15:36:04 +02:00
function ( result )
{
2019-04-20 17:04:40 +02:00
window . location . href = U ( '/shoppinglist?list=' + $ ( "#selected-shopping-list" ) . val ( ) ) ;
2017-04-21 15:36:04 +02:00
} ,
function ( xhr )
{
console . error ( xhr ) ;
}
) ;
} ) ;
2018-07-15 08:29:26 +02:00
$ ( document ) . on ( 'click' , '#clear-shopping-list' , function ( e )
{
bootbox . confirm ( {
2020-09-08 18:10:30 +02:00
message : _ _t ( 'Are you sure to empty shopping list "%s"?' , SanitizeHtml ( $ ( "#selected-shopping-list option:selected" ) . text ( ) ) ) ,
2019-09-24 10:24:47 +02:00
closeButton : false ,
2018-07-15 08:29:26 +02:00
buttons : {
confirm : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'Yes' ) ,
2018-07-15 08:29:26 +02:00
className : 'btn-success'
} ,
cancel : {
2019-05-01 20:19:18 +02:00
label : _ _t ( 'No' ) ,
2018-07-15 08:29:26 +02:00
className : 'btn-danger'
}
} ,
callback : function ( result )
{
if ( result === true )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . BeginUiBusy ( ) ;
2019-04-20 17:04:40 +02:00
Grocy . Api . Post ( 'stock/shoppinglist/clear' , { "list_id" : $ ( "#selected-shopping-list" ) . val ( ) } ,
2018-07-15 08:29:26 +02:00
function ( result )
{
2020-01-28 19:27:18 +01:00
animateCSS ( "#shoppinglist-table tbody tr" , "fadeOut" , function ( )
2018-08-11 14:16:11 +02:00
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2020-01-28 19:27:18 +01:00
$ ( "#shoppinglist-table tbody tr" ) . remove ( ) ;
2018-11-17 14:50:52 +01:00
OnListItemRemoved ( ) ;
2018-08-11 14:16:11 +02:00
} ) ;
2018-07-15 08:29:26 +02:00
} ,
function ( xhr )
{
2018-11-24 19:40:50 +01:00
Grocy . FrontendHelpers . EndUiBusy ( ) ;
2018-07-15 08:29:26 +02:00
console . error ( xhr ) ;
}
) ;
}
}
} ) ;
} ) ;
2018-11-14 21:40:17 +01:00
2018-11-17 14:50:52 +01:00
$ ( document ) . on ( 'click' , '.shopping-list-stock-add-workflow-list-item-button' , function ( e )
2018-11-14 21:40:17 +01:00
{
2018-11-17 14:50:52 +01:00
e . preventDefault ( ) ;
2019-01-19 00:37:21 -07:00
2020-04-13 18:26:53 +02:00
// Remove the focus from the current button
// to prevent that the tooltip stays until clicked anywhere else
document . activeElement . blur ( ) ;
2018-11-17 14:50:52 +01:00
var href = $ ( e . currentTarget ) . attr ( 'href' ) ;
$ ( "#shopping-list-stock-add-workflow-purchase-form-frame" ) . attr ( "src" , href ) ;
$ ( "#shopping-list-stock-add-workflow-modal" ) . modal ( "show" ) ;
if ( Grocy . ShoppingListToStockWorkflowAll )
{
2018-11-18 14:19:50 +01:00
$ ( "#shopping-list-stock-add-workflow-purchase-item-count" ) . removeClass ( "d-none" ) ;
2019-05-05 14:13:50 +02:00
$ ( "#shopping-list-stock-add-workflow-purchase-item-count" ) . text ( _ _t ( "Adding shopping list item %1$s of %2$s" , Grocy . ShoppingListToStockWorkflowCurrent , Grocy . ShoppingListToStockWorkflowCount ) ) ;
2018-11-26 20:02:01 +01:00
$ ( "#shopping-list-stock-add-workflow-skip-button" ) . removeClass ( "d-none" ) ;
}
else
{
$ ( "#shopping-list-stock-add-workflow-skip-button" ) . addClass ( "d-none" ) ;
2018-11-17 14:50:52 +01:00
}
} ) ;
Grocy . ShoppingListToStockWorkflowAll = false ;
Grocy . ShoppingListToStockWorkflowCount = 0 ;
Grocy . ShoppingListToStockWorkflowCurrent = 0 ;
$ ( document ) . on ( 'click' , '#add-all-items-to-stock-button' , function ( e )
{
Grocy . ShoppingListToStockWorkflowAll = true ;
Grocy . ShoppingListToStockWorkflowCount = $ ( ".shopping-list-stock-add-workflow-list-item-button" ) . length ;
Grocy . ShoppingListToStockWorkflowCurrent ++ ;
$ ( ".shopping-list-stock-add-workflow-list-item-button" ) . first ( ) . click ( ) ;
} ) ;
2018-11-26 20:02:01 +01:00
$ ( "#shopping-list-stock-add-workflow-modal" ) . on ( "hidden.bs.modal" , function ( e )
{
Grocy . ShoppingListToStockWorkflowAll = false ;
Grocy . ShoppingListToStockWorkflowCount = 0 ;
Grocy . ShoppingListToStockWorkflowCurrent = 0 ;
} )
2018-11-17 14:50:52 +01:00
$ ( window ) . on ( "message" , function ( e )
{
var data = e . originalEvent . data ;
2019-01-19 00:37:21 -07:00
2018-11-17 14:50:52 +01:00
if ( data . Message === "AfterItemAdded" )
{
$ ( ".shoppinglist-delete-button[data-shoppinglist-id='" + data . Payload + "']" ) . click ( ) ;
}
else if ( data . Message === "Ready" )
{
if ( ! Grocy . ShoppingListToStockWorkflowAll )
{
$ ( "#shopping-list-stock-add-workflow-modal" ) . modal ( "hide" ) ;
}
else
{
Grocy . ShoppingListToStockWorkflowCurrent ++ ;
if ( Grocy . ShoppingListToStockWorkflowCurrent <= Grocy . ShoppingListToStockWorkflowCount )
{
$ ( ".shopping-list-stock-add-workflow-list-item-button" ) [ 1 ] . click ( ) ;
}
else
{
$ ( "#shopping-list-stock-add-workflow-modal" ) . modal ( "hide" ) ;
}
}
}
} ) ;
2018-11-26 20:02:01 +01:00
$ ( document ) . on ( 'click' , '#shopping-list-stock-add-workflow-skip-button' , function ( e )
{
e . preventDefault ( ) ;
2019-01-19 00:37:21 -07:00
2018-11-26 20:02:01 +01:00
window . postMessage ( WindowMessageBag ( "Ready" ) , Grocy . BaseUrl ) ;
} ) ;
2019-07-06 17:56:59 +02:00
$ ( document ) . on ( 'click' , '.order-listitem-button' , function ( e )
{
e . preventDefault ( ) ;
2020-04-13 18:26:53 +02:00
// Remove the focus from the current button
// to prevent that the tooltip stays until clicked anywhere else
document . activeElement . blur ( ) ;
2019-07-06 17:56:59 +02:00
Grocy . FrontendHelpers . BeginUiBusy ( ) ;
var listItemId = $ ( e . currentTarget ) . attr ( 'data-item-id' ) ;
2020-08-29 16:41:27 +02:00
2019-07-06 17:56:59 +02:00
var done = 1 ;
if ( $ ( e . currentTarget ) . attr ( 'data-item-done' ) == 1 )
{
done = 0 ;
}
$ ( e . currentTarget ) . attr ( 'data-item-done' , done ) ;
Grocy . Api . Put ( 'objects/shopping_list/' + listItemId , { 'done' : done } ,
function ( )
{
if ( done == 1 )
{
$ ( '#shoppinglistitem-' + listItemId + '-row' ) . addClass ( "text-muted" ) ;
$ ( '#shoppinglistitem-' + listItemId + '-row' ) . addClass ( "text-strike-through" ) ;
}
else
{
$ ( '#shoppinglistitem-' + listItemId + '-row' ) . removeClass ( "text-muted" ) ;
$ ( '#shoppinglistitem-' + listItemId + '-row' ) . removeClass ( "text-strike-through" ) ;
}
Grocy . FrontendHelpers . EndUiBusy ( ) ;
} ,
function ( xhr )
{
Grocy . FrontendHelpers . EndUiBusy ( ) ;
console . error ( xhr ) ;
}
) ;
2020-01-24 22:26:36 +01:00
2020-08-29 16:41:27 +02:00
2020-01-24 22:26:36 +01:00
var statusInfoCell = $ ( "#shoppinglistitem-" + listItemId + "-status-info" ) ;
if ( done == 1 )
{
statusInfoCell . text ( statusInfoCell . text ( ) . replace ( "xxUNDONExx" , "" ) ) ;
}
else
{
statusInfoCell . text ( statusInfoCell . text ( ) + " xxUNDONExx" ) ;
}
shoppingListTable . rows ( ) . invalidate ( ) . draw ( false ) ;
$ ( "#status-filter" ) . trigger ( "change" ) ;
2019-07-06 17:56:59 +02:00
} ) ;
2018-11-17 14:50:52 +01:00
function OnListItemRemoved ( )
{
if ( $ ( ".shopping-list-stock-add-workflow-list-item-button" ) . length === 0 )
{
$ ( "#add-all-items-to-stock-button" ) . addClass ( "disabled" ) ;
}
2018-11-14 21:40:17 +01:00
}
2018-11-17 14:50:52 +01:00
OnListItemRemoved ( ) ;
2019-09-20 17:40:45 +02:00
$ ( document ) . on ( "click" , "#print-shopping-list-button" , function ( e )
{
$ ( ".print-timestamp" ) . text ( moment ( ) . format ( "l LT" ) ) ;
2019-09-20 18:08:38 +02:00
$ ( "#description-for-print" ) . html ( $ ( "#description" ) . val ( ) ) ;
2019-09-20 17:40:45 +02:00
window . print ( ) ;
} ) ;
2019-09-20 18:08:38 +02:00
$ ( "#description" ) . on ( "summernote.change" , function ( )
{
$ ( "#save-description-button" ) . removeClass ( "disabled" ) ;
2019-09-28 08:48:58 +02:00
if ( $ ( "#description" ) . summernote ( "isEmpty" ) )
{
$ ( "#clear-description-button" ) . addClass ( "disabled" ) ;
}
else
{
$ ( "#clear-description-button" ) . removeClass ( "disabled" ) ;
}
2019-09-20 18:08:38 +02:00
} ) ;
$ ( document ) . on ( "click" , "#save-description-button" , function ( e )
{
e . preventDefault ( ) ;
document . activeElement . blur ( ) ;
Grocy . Api . Put ( 'objects/shopping_lists/' + $ ( "#selected-shopping-list" ) . val ( ) , { description : $ ( "#description" ) . val ( ) } ,
function ( result )
{
$ ( "#save-description-button" ) . addClass ( "disabled" ) ;
} ,
function ( xhr )
{
2019-09-27 14:08:24 +02:00
console . error ( xhr ) ;
2019-09-20 18:08:38 +02:00
}
) ;
} ) ;
2019-09-28 08:48:58 +02:00
$ ( document ) . on ( "click" , "#clear-description-button" , function ( e )
{
e . preventDefault ( ) ;
document . activeElement . blur ( ) ;
$ ( "#description" ) . summernote ( "reset" ) ;
$ ( "#save-description-button" ) . click ( ) ;
} ) ;
2020-01-25 08:49:17 +01:00
$ ( ".switch-view-mode-button" ) . on ( 'click' , function ( e )
2020-01-24 22:05:08 +01:00
{
e . preventDefault ( ) ;
$ ( "#shoppinglist-main" ) . toggleClass ( "fullscreen" ) ;
2020-02-01 12:35:06 +01:00
$ ( ".dataTables_scrollHeadInner" ) . width ( "" ) ; // Remove absolute width on element set by DataTables
$ ( ".dataTables_scrollHeadInner table" ) . width ( "" ) ; // Remove absolute width on element set by DataTables
2020-01-24 22:05:08 +01:00
$ ( "body" ) . toggleClass ( "fullscreen-card" ) ;
$ ( "#shopping-list-normal-view-button" ) . toggleClass ( "d-none" ) ;
$ ( "#mainNav" ) . toggleClass ( "d-none" ) ;
if ( $ ( "body" ) . hasClass ( "fullscreen-card" ) )
{
2020-08-29 16:41:27 +02:00
window . location . hash = "#compact" ;
2020-01-24 22:05:08 +01:00
}
else
{
window . history . replaceState ( null , null , " " ) ;
}
} ) ;
2019-09-28 08:48:58 +02:00
$ ( "#description" ) . trigger ( "summernote.change" ) ;
$ ( "#save-description-button" ) . addClass ( "disabled" ) ;
2020-01-24 22:05:08 +01:00
2020-01-25 08:49:17 +01:00
if ( window . location . hash === "#compact" )
2020-01-24 22:05:08 +01:00
{
$ ( "#shopping-list-compact-view-button" ) . click ( ) ;
}
2020-02-09 15:36:03 +01:00
// Auto switch to compact view on mobile when enabled
if ( $ ( window ) . width ( ) < 768 & window . location . hash !== "#compact" && ! BoolVal ( Grocy . UserSettings . shopping _list _disable _auto _compact _view _on _mobile ) )
2020-01-24 22:05:08 +01:00
{
$ ( "#shopping-list-compact-view-button" ) . click ( ) ;
}