Explorar o código

Updated drag & drop for segment templates

Samson Mutunga %!s(int64=3) %!d(string=hai) anos
pai
achega
cc9436be17

+ 20 - 0
public/css/style.css

@@ -3026,4 +3026,24 @@ body .vakata-context .vakata-context-separator>a {
 }
 .table-columns tr:last-child .move-down {
     display: none;
+}
+
+.table-dnd-on-drag {
+    background-color: #fff7de !important;
+    box-shadow: 6px 3px 5px #e9e9e9, 0 1px 0 #ccc inset, 0 -1px 0 #ccc inset;
+    transition: 0.3s;
+}
+.table-dnd-on-drag td {
+    font-weight: bold;
+    border-top: none;
+}
+.table-dnd-no-change {
+    opacity: 0.5;
+    transition: 0.3s;
+}
+.droppable-section-active {
+    border: 2px dashed #00bcd4;
+}
+[draggable] {
+    cursor:all-scroll;
 }

+ 157 - 3
resources/views/app/practice-management/visit-templates/visit-template-segment-templates/index.blade.php

@@ -1,6 +1,7 @@
 @extends('app.practice-management.visit-templates.single')
 @section('visitTemplateTab')
-<div class="row">
+
+<div id="visitTemplateSegmentTemplates" class="row">
     <div class="col-md-12">
         <div class="card mb-2">
             <div class="card-header border-bottom-0 px-1 py-2 d-flex align-items-center">
@@ -22,7 +23,7 @@
 
                     </div>
                     <div class="card-body p-0">
-                        <div class="table-responsive">
+                        <div class="table-responsive" data-position="LEFT" droppable-section>
                             @include('app.practice-management.visit-templates.visit-template-segment-templates.segment-templates-list', ['position' => 'LEFT'])
                         </div>
                     </div>
@@ -37,7 +38,7 @@
                         </div>
                     </div>
                     <div class="card-body p-0">
-                        <div class="table-responsive">
+                        <div class="table-responsive" data-position="RIGHT" droppable-section>
                             @include('app.practice-management.visit-templates.visit-template-segment-templates.segment-templates-list', ['position' => 'RIGHT'])
                         </div>
                     </div>
@@ -49,5 +50,158 @@
             {{ $visitTemplateSegmentTemplates->appends(request()->input())->links() }}
         </div>
     </div>
+
+    <div class="col-md-12 mt-4">
+        <div class="card mb-2">
+            <div class="card-header border-bottom-0 px-1 py-2 d-flex align-items-center">
+                <strong class="mr-4">
+                    <i class="fas fa-trash text-danger"></i>
+                    Deactivated Segment Templates
+                </strong>
+            </div>
+        </div>
+
+        <div class="row">
+            <div class="col-md-6">
+                <div class="card">
+                    <div class="card-body p-0">
+                        <div class="table-responsive">
+                            @include('app.practice-management.visit-templates.visit-template-segment-templates.segment-templates-list-deactivated')
+                        </div>
+                    </div>
+                </div>
+            </div>
+        </div>
+    </div>
+
 </div>
+
+<script>
+var draggableVisitTemplateSegmentTemplates = null;
+(function($){
+    draggableVisitTemplateSegmentTemplates = {
+        recordsPerPage: '<?= $visitTemplateSegmentTemplates->perPage() ?>',
+        currentPage: '<?= $visitTemplateSegmentTemplates->currentPage() ?>',
+        preservePositionIndexNumber: false,
+        draggedDeactivatedRecordUid: null,
+
+        recalculateRowsPositionIndexes: function(table, leftRightPosition){
+            var self = this;
+            var currentRowsData = [];
+            var rows = $(table).find('tbody tr');
+            $.each(rows, function(i, row){
+                var uid = $(row).data('uid');
+                var currentPositionIndex = parseInt($(row).data('positionindex'));
+                var heading = $(row).data('heading');
+                var subheading = $(row).data('subheading');
+                var displayTitle = $(row).data('displaytitle');
+                var leftOrRight = $(row).data('leftorright');
+                currentRowsData.push({uid:uid, position: currentPositionIndex,heading:heading, subheading:subheading, displayTitle:displayTitle, leftOrRight:leftOrRight});                
+            });
+            var _currentRowsData = JSON.stringify(currentRowsData);
+            
+            var sortedByPositionIndexAscending = currentRowsData.sort(function(a, b){
+                var aPositionIndex = a.position;
+                var bPositionIndex = b.position; 
+                return ((aPositionIndex < bPositionIndex) ? -1 : ((aPositionIndex > bPositionIndex) ? 1 : 0));
+            });
+
+            var originalCurrentRowsData = JSON.parse(_currentRowsData);
+
+            var updatedData = [];
+            $.each(originalCurrentRowsData, function(i, r){
+                if(self.preservePositionIndexNumber){
+                  r.position = sortedByPositionIndexAscending[i].position;  
+                }else{
+                    r.position = i;
+                    var currentPage = parseInt(self.currentPage);
+                    currentPage = isNaN(currentPage) ? 1:currentPage;
+                    if(currentPage > 1){
+                        r.position = i + parseInt(self.recordsPerPage);
+                    }                    
+                }
+                
+                updatedData.push(r);
+            });
+            self.updatePositionIndexes(updatedData);
+        },
+        updatePositionIndexes: async function(data){
+            var self = this;
+            var error = null;
+            for (var row of data) {
+                row.positionIndex = row.position;
+                var response = await $.post('/api/visitTemplateSegmentTemplate/updateBasic', row, function(res){return res});
+                if(!response.success){
+                    error = true;
+                    toastr.error(response.message);
+                }
+            }
+            fastReload();
+        },
+        initDraggableTable: function(){
+            var self = this;
+            $(".table-dnd").tableDnD({
+                onDragClass: 'table-dnd-on-drag',
+                onDrop: function(table, row){
+                    var tableLeftRightPosition = $(table).data('position');
+                    self.recalculateRowsPositionIndexes(table, tableLeftRightPosition);
+                },
+                onDragStart: function(table, row) {
+                    $(table).find('tbody tr').addClass('table-dnd-no-change');
+                    $(row).removeClass('table-dnd-no-change');
+                },
+                onDragStop: function(table, row) {
+                    $(table).find('tbody tr').removeClass('table-dnd-no-change');
+                },
+            });
+        },
+        initOnDroppableSection: function(){
+            var self = this;
+            $('[droppable-section]').on('dragover', function(e){
+                $(e.currentTarget).addClass('droppable-section-active');
+                return false;
+            }).on('drop', function (e) {
+                $(e.currentTarget).removeClass('droppable-section-active');
+                var position = $(e.currentTarget).data('position');
+                self.updateDraggedDeactivatedRecord(position);
+                return false;
+            });
+            $('[draggable-item]').on('dragstart', function(e){
+                self.draggedDeactivatedRecordUid = $(e.currentTarget).data('uid');
+            });
+        },
+        updateDraggedDeactivatedRecord: function(position){
+            var self = this;
+            var positionUrl = position == 'LEFT' ? 'moveToLeft':'moveToRight';
+            $.post('/api/visitTemplateSegmentTemplate/reactivate', {uid: self.draggedDeactivatedRecordUid, memo:'Restored by drag & drop'}, function(response){
+                if(response.success){
+                  $.post('/api/visitTemplateSegmentTemplate/'+positionUrl, {uid:self.draggedDeactivatedRecordUid}, function(res){
+                      if(res.success){
+                          self.draggedDeactivatedRecordUid = null;
+                          fastReload();
+                      }
+                  },'json');  
+                }
+            },'json');
+            
+        },
+        init: function(){
+            this.initDraggableTable();
+            this.initOnDroppableSection();
+        }
+    };
+    
+})(jQuery);
+</script>
+
+<script>
+        (function() {
+            function init() {
+                draggableVisitTemplateSegmentTemplates.init();
+            }
+            addMCInitializer('visitTemplateSegmentTemplates', init, '#visitTemplateSegmentTemplates')
+
+        }).call(window);
+    </script>
+
 @endSection

+ 67 - 0
resources/views/app/practice-management/visit-templates/visit-template-segment-templates/segment-templates-list-deactivated.blade.php

@@ -0,0 +1,67 @@
+<table data-position="{{ @$position }}" class="table table-sm table-striped border-top p-0 m-0">
+	<thead class="bg-light">
+		<tr>
+			<th class="border-0">Display title</th>
+			<th class="border-0">Heading</th>
+			<th class="border-0">Sub-heading</th>
+			<th class="border-0">Position Index</th>
+			<th class="border-0">Segment Template</th>
+			<th class="border-0">Left/Right</th>
+			<th class="border-0">Status</th>
+			<th class="border-0">&nbsp;</th>
+		</tr>
+	</thead>
+	<tbody>
+		<?php $rowsCount = 0; ?>
+		@foreach($visitTemplateSegmentTemplates as $template)
+			@if(!$template->is_active)
+				<?php $rowsCount++; ?>
+				<tr draggable="true" data-uid="{{ $template->uid }}" draggable-item>
+					<td><i class="fas fa-arrows-alt mr-2"></i> {{ $template->display_title }}</td>
+					<td>{{ $template->heading }}</td>
+					<td>{{ $template->subheading }}</td>
+					<td>{{ $template->position_index }}</td>
+					<td>{{ $template->segmentTemplate ? $template->segmentTemplate->default_display_title : '---' }}</td>
+					<td>{{ $template->left_or_right }}</td>
+					<td>
+						<div>
+							@if($template->is_active)
+							<span class="text-success">ACTIVE</span>
+							@if($template->reactivation_memo)
+							<div style="width:300px;">
+								<small class="text-muted"><b>Memo: </b>{{ $template->reactivation_memo }}</small>
+								@include('app.practice-management.visit-templates.visit-template-segment-templates.update-memo')
+							</div>
+							@endif
+							@else
+							<span class="text-danger">INACTIVE</span>
+							@if($template->deactivation_memo)
+							<div style="width:300px;">
+								<small class="text-muted"><b>Memo: </b>{{ $template->deactivation_memo }}</small>
+								@include('app.practice-management.visit-templates.visit-template-segment-templates.update-memo')
+							</div>
+							@endif
+							@endif
+						</div>
+					</td>
+					<td>
+						<div class="d-flex align-items-center">
+							<div class="mr-2">
+								@include('app.practice-management.visit-templates.visit-template-segment-templates.update', ['position' => @$position])
+							</div>
+							<div>
+								@include('app.practice-management.visit-templates.visit-template-segment-templates.activate-deactivate')
+							</div>
+						</div>
+					</td>
+				</tr>
+			@endif
+		@endforeach
+		@if(!$rowsCount)
+		<tr>
+			<td colspan="8">No deactivated records found!</td>
+		</tr>
+		@endif
+	</tbody>
+
+</table>

+ 5 - 7
resources/views/app/practice-management/visit-templates/visit-template-segment-templates/segment-templates-list.blade.php

@@ -1,4 +1,4 @@
-<table class="table table-sm table-striped border-top p-0 m-0">
+<table data-position="{{ @$position }}" class="table table-sm table-striped border-top p-0 m-0 table-dnd">
 	<thead class="bg-light">
 		<tr>
 			<th class="border-0">Display title</th>
@@ -6,7 +6,6 @@
 			<th class="border-0">Sub-heading</th>
 			<th class="border-0">Position Index</th>
 			<th class="border-0">Segment Template</th>
-			<th class="border-0">Created At</th>
 			<th class="border-0">Status</th>
 			<th class="border-0">&nbsp;</th>
 		</tr>
@@ -14,15 +13,14 @@
 	<tbody>
 		<?php $rowsCount = 0; ?>
 		@foreach($visitTemplateSegmentTemplates as $template)
-			@if(@$position === $template->left_or_right)
+			@if(@$position === $template->left_or_right && $template->is_active)
 				<?php $rowsCount++; ?>
-				<tr>
-					<td>{{ $template->display_title }}</td>
+				<tr id="{{ $template->uid }}" data-uid="{{ $template->uid }}" data-positionindex="{{ $template->position_index }}" data-heading="{{ $template->heading }}" data-subheading="{{ $template->subheading }}" data-displaytitle="{{ $template->display_title }}" data-leftorright="{{ $template->left_or_right }}">
+					<td class="text-nowrap">&boxH; &nbsp; &nbsp; {{ $template->display_title }}</td>
 					<td>{{ $template->heading }}</td>
 					<td>{{ $template->subheading }}</td>
 					<td>{{ $template->position_index }}</td>
 					<td>{{ $template->segmentTemplate ? $template->segmentTemplate->default_display_title : '---' }}</td>
-					<td>{{ friendly_date_time($template->created_at) }}</td>
 					<td>
 						<div>
 							@if($template->is_active)
@@ -59,7 +57,7 @@
 		@endforeach
 		@if(!$rowsCount)
 		<tr>
-			<td colspan="8">No records found!</td>
+			<td colspan="7">No records found!</td>
 		</tr>
 		@endif
 	</tbody>

+ 1 - 0
resources/views/layouts/template.blade.php

@@ -75,6 +75,7 @@
             crossorigin="anonymous"></script>
 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
+    <script src="https://cdnjs.cloudflare.com/ajax/libs/TableDnD/1.0.5/jquery.tablednd.min.js"></script>
 
     @yield('head')
 </head>