Revision 843 (by ahitrov, 2022/11/01 13:57:31) Section multi-item drag-n-drop move

<style>
.section-content tr.disabled { background:#f0f0f0; }
.section-content tr.disabled td.actions a { display:none; }
.drag-over td { background:#f0f0f0; padding-bottom:10px; }
.hide { display:none; }
</style>
<script type="text/javascript">
<!--
function checkbox_common_toggle ( sClassSelector ) {
   $('.' + sClassSelector).each(function(){
	var oField = $(this)[0];
	if ( oField.checked ) {
		oField.checked = 0;
	} else {
		oField.checked = 1;
	}
   });
}
% if ( @inline_pickups ) {
$(document).ready(function() {
%	foreach my $pickup ( @inline_pickups ) {
%		my $lookup_opts = $pickup->{lookup_opts};
%		my $search_opts = join( '&', map { $_.'='.$lookup_opts->{$_} } keys %$lookup_opts );

    $('.autocomplete_<% $pickup->{attr} %>').autocomplete({
		minLength : 2,
		source	: '/contenido/ajax/document_search.html?<% $search_opts %>',
		select	: function( event, ui )
		{
			var sStoreId = $(this).attr('rel');
			var item = ui.item;
			$('#' + sStoreId).val( item.id );
		},
		change	: function( event, ui )
		{
			var sStoreId = $(this).attr('rel');
			var sValue = $(this).val();
			if ( sValue == '' ) {
				$('#' + sStoreId).val('');
			}
		}
    });

%	}    
});
% }

function mydump(arr,level) {
    var dumped_text = "";
    if(!level) level = 0;

    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";

    if(typeof(arr) == 'object') {  
        for(var item in arr) {
            var value = arr[item];

            if(typeof(value) == 'object') { 
                dumped_text += level_padding + "'" + item + "' ...\n";
                dumped_text += mydump(value,level+1);
            } else {
                dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
            }
        }
    } else { 
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
}

var oMenus = {
% my $cls_count = scalar keys %document_classes;
% foreach my $class ( keys %document_classes ) {
%	$cls_count--;
%	my $class_name = $class;
%	$class_name =~ s/:/-/g;
%	my ($prop) = grep { $_->{attr} eq 'status' } $class->new( $keeper )->structure;
%	if ( ref $prop && $prop->{type} eq 'status' ) {
%		my @menu;
%		foreach my $case ( @{ref $prop->{cases} eq 'ARRAY' ? $prop->{cases} : $keeper->default_status()} ) {
%			my $name = $case->[1];
%			$name =~ s/'/\\'/g;
%			my $key = $case->[0];
%			if ( $key =~ /^\d+$/ && $key >= 0 && $key <= 10 ) {
%				push @menu, "'$key' : { 'name' : '$name', icon: '$key' }";
%			} else {
%				push @menu, "'$key' : { 'name' : '$name' }";
%			}
%		}
	'<% $class_name %>' : { <% join(", ", @menu) %> }<% $cls_count ? ',' : '' %>
%	}
% }

}

function set_status_toggle( ev, nID, $class_name ) {
	ev.preventDefault();
	$('#row-' + nID).addClass('disabled');
	$.ajax({
		'url'	: '/contenido/ajax/document_status.html',
		'data'	: { 'class' : $class_name, 'id' : nID, 'toggle' : 1, 's' : <% ref $section ? $section->id : 0 %>, 'params' : '<% $params %>' },
		'dataType'	: 'json',
		'success'	: function( data ) {
			if ( data.error ) {
				alert( data.error );
			}
			if ( data.success ) {
				$('.section-content').html(data.html);
				$('.context-menu-'+ $class_name).on('click', function( ev ) {
					var nID = parseInt($(this).data('id'));
					set_status_toggle(ev, nID, $class_name)
				});
			}
		}
	});
}

function set_status( $selector, $class_name ) {
	var oMenu = oMenus[$class_name];
	$.contextMenu({
		selector: $selector, 
		trigger: 'left',
		callback: function(key, options) {
			var nID = parseInt($(this).data('id'));
			$('#row-' + nID).addClass('disabled');
			if ( nID != key ) {
				$.ajax({
					'url'	: '/contenido/ajax/document_status.html',
					'data'	: { 'class' : $class_name, 'id' : nID, 'status' : key, 's' : <% ref $section ? $section->id : 0 %>, 'params' : '<% $params %>' },
					'dataType'	: 'json',
					'success'	: function( data ) {
						if ( data.error ) {
							alert( data.error );
						}
						if ( data.success ) {
							$('.section-content').html(data.html);
							set_status( '.context-menu-'+ $class_name, $class_name);
						}
					}
				});
			}
		},
		items: oMenu
	});
}

$(document).ready(function(){
% if ( $section->_sorted() ) {
	$('.move-up').on('click', function( ev ){
		ev.preventDefault();
		var $nID = $(this).data('id');
		$.ajax({
			'url'	: '/contenido/ajax/document_move.html',
			'data'	: { 's' : <% ref $section ? $section->id : 0 %>, 'id' : $nID, 'move' : 'up' },
			'dataType'	: 'json',
			'success'	: function( data ){
				if ( data.error ) {
					alert( data.error );
				} else if ( data.before ) {
					var $nBefore = data.before;
					$('#row-' + $nID).insertBefore('#row-' + $nBefore);
				}
			}
		});
	});

	$('.move-down').on('click', function( ev ){
		ev.preventDefault();
		var $nID = $(this).data('id');
		$.ajax({
			'url'	: '/contenido/ajax/document_move.html',
			'data'	: { 's' : <% ref $section ? $section->id : 0 %>, 'id' : $nID, 'move' : 'down' },
			'dataType'	: 'json',
			'success'	: function( data ){
				if ( data.error ) {
					alert( data.error );
				} else if ( data.after ) {
					var $nAfter = data.after;
					$('#row-' + $nID).insertAfter('#row-' + $nAfter);
				}
			}
		});
	});
% }

% foreach my $class ( keys %document_classes ) {
%	my $class_name = $class;
%	$class_name =~ s/:/-/g;
%	my ($prop) = grep { $_->{attr} eq 'status' } $class->new( $keeper )->structure;
%	if ( ref $prop && $prop->{type} eq 'status' ) {
%		my @cases = @{ref $prop->{cases} eq 'ARRAY' ? $prop->{cases} : $keeper->default_status()};
%		if ( @cases > 2 ) {
	set_status( '.context-menu-<% $class_name %>', '<% $class_name %>' );
%		} else {
	$('.context-menu-<% $class_name %>').on('click', function( ev ) {
		var nID = parseInt($(this).data('id'));
		set_status_toggle(ev, nID, '<% $class_name %>')
	});
%		}
%	}
% }
% if ( $section->_sorted() ) {

	$dataRows = $('.data-row').each(function( index ) {
		$( this )[0].addEventListener('dragstart', dragStart);
		$( this )[0].addEventListener('dragenter', dragEnter);
		$( this )[0].addEventListener('dragover', dragOver);
		$( this )[0].addEventListener('dragleave', dragLeave);
		$( this )[0].addEventListener('drop', dragDrop);
	});
	function dragEnter(e) {
		e.preventDefault();
		$row = $(e.target).closest('tr');
		$row.addClass('drag-over');
	}
	function dragOver(e) {
		e.preventDefault();
		$row = $(e.target).closest('tr');
		$row.addClass('drag-over');
	}
	function dragLeave(e) {
		e.preventDefault();
		$row = $(e.target).closest('tr');
		$row.removeClass('drag-over');
	}
	function dragStart(e) {
		$row = $(e.target).closest('tr');
		e.dataTransfer.setData('text/plain', $row.attr('id'));
	}
	function dragDrop(e) {
		e.preventDefault();
		$target = $(e.target).closest('tr');
		$target.removeClass('drag-over');
		rowID = e.dataTransfer.getData('text/plain');
		$source = $('#' + rowID);

		sourceID = $source.data('id');
		targetID = $target.data('id');
		if ( sourceID == targetID ) {
			return;
		}
		var checkedIds = [];
		$('tr.data-row').each(function(index){
			$checkInput = $(this).find('.common-check');
			if ( $(this).data('id') == sourceID ) {
				checkedIds.push($(this).data('id'));
				$checkInput.prop('checked', true);
			} else if ( $checkInput.prop('checked') ) {
				checkedIds.push($(this).data('id'));
			}
		});
		if ( sourceID != checkedIds[0] ) {
			$source = $('#row-' + checkedIds[0]);
		}
		$.ajax({
			'url'	: '/contenido/ajax/document_move.html',
			'data'	: { 's' : <% ref $section ? $section->id : 0 %>, 'id' : checkedIds.join(','), 'move' : 'after', 'aid' : targetID },
			'dataType'	: 'json',
			'success'	: function( data ){
				if ( data.error ) {
					alert( data.error );
				} else if ( data.after ) {
					checkedIds.forEach(function(rowID, index, ids) {
						$source = $('#row-' + rowID);
						$source.detach().insertAfter($target);
						$source.find('.common-check').attr('checked', false);
						$target = $source;
					});
					console.log('Moved after ' + data.after);
				}
			}
		});


	}
% }
});
//-->
</script>
<form name="section_browse" action="sections.html" method="POST">
<table width="100%" border="0" cellpadding="4" cellspacing="0" class="tlistdocs">
<tr bgcolor="#efefef">
<th><a href="javascript:void(0)" onclick="checkbox_common_toggle('common-check'); return false;"><img src="/contenido/i/checkbox-14x14-green.gif" width="14" height="14" title="Выбор документов для групповых операций" align="absmiddle" border="0" style="margin-left:3px;"></a></th>
%
%	foreach (@$columns) {
<th>\
%		if ( $_->{inline} && $_->{type} eq 'checkbox' ) {
<a href="javascript:void(0)" onclick="checkbox_common_toggle('<% $_->{attr} %>-check'); return false;"><img src="/contenido/i/checkbox-14x14-green.gif" width="14" height="14" title="Выбор документов для групповых операций" align="absmiddle" border="0" style="margin:0 5px 0 2px;"></a>\
%		}
<% $_->{shortname} || $_->{rusname} %></th>
%	}
%
</tr>
<tbody class="section-content">
%
%		unless (@$documents) {
<tr><td align="center" colspan="<% scalar @$columns %>">Документы не найдены</td></tr>
%		}
%		foreach my $document (@$documents) {
%			next unless ref($document);
<& /contenido/components/section_browse_row.msn, document => $document, columns => $columns, section => $section,
	toopi => $toopi, inline_status => $inline_status, lookup_elemets => \%lookup_elements,
	filter => $filter, params_unsection => $params_unsection, params_unclassed => $params_unclassed
&>
%		} #- foreach @documents
</tbody>
</table>
<input type="hidden" name="id" value="<% $section->id %>">
%	if ( ref $filter eq 'HASH' ) {
%		while ( my ($key, $value) = each %$filter ) {
%			next	if $key eq 's';
<input type="hidden" name="<% $key %>" value="<% $value |h %>">
%		}
%	}
%	if ( $section->default_document_class || $section->default_table_class ) {
<div style="display:inline-block; width:45%; margin:10px 0; min-height:80px; vertical-align:top;">
<& /contenido/components/inputs/parent.msn, name => 'tree', check => $section->id, style => 'width:100%;' &>
	<div style="text-align:left; margin-top:5px;">
<input type="submit" name="move" value="Перенести" class="input_btn"><input
 type="submit" name="link" value="Привязать" class="input_btn"><input
 type="submit" name="unlink" value="Отвязать от текущей" class="input_btn" onclick="return confirm('Объекты, не привязанные к другим секциям могут быть потеряны');">
	</div>
</div>
%	}
%	if ( $inline_status || $delete_status ) {
<div style="display:inline-block; width:45%; text-align:right; margin:5px; padding-top:29px; vertical-align:top;">
%		if ( $inline_status ) {
<input type="submit" name="update" value="Сохранить изменения" class="input_btn">\
%		}
%		if ( $delete_status ) {
<input type="submit" name="delete" value="Удалить выбранные" class="input_btn" onclick="return confirm('Все отмеченные позиции будут удалены');">
%		}
</div>
%	}
<br clear="all">
</form>
<%args>

	$section	=> undef
	$documents	=> undef
	$columns	=> undef
	$id		=> undef
	$filter		=> undef

</%args>
<%init>

   return	unless ref $documents eq 'ARRAY';
   return	unless ref $columns eq 'ARRAY';
   return	unless ref $section;

   my $toopi = $project->documents();
   my $inline_status = 0;
   my $delete_status = 0;
   my $params = ref $filter eq 'HASH' ? join ('&', map { $_.'='.$filter->{$_} } keys %$filter ) : '';
   my $params_unclassed = ref $filter eq 'HASH' ? join ('&', map { $_.'='.$filter->{$_} } grep { $_ ne 'class' } keys %$filter ) : '';
   my $params_unsection = ref $filter eq 'HASH' ? join ('&', map { $_.'='.$filter->{$_} } grep { $_ ne 's' } keys %$filter ) : '';

   my %lookup_elements;
   my %document_classes;
   my @inline_pickups = grep {
		my $type = exists $_->{inline_type} ? $_->{inline_type} : $_->{type};
		exists $_->{inline} && ($type eq 'pickup' || $type eq 'autocomplete')
	} @$columns;

   map {
	$_->{document_access} = $user->section_accesses($user, $_->section);
	if ( $_->{document_access} == 2 ) {
		$delete_status = 1;
	}
	$document_classes{$_->class} = 1;
   } @$documents;
   map {
	if ( exists $_->{inline} && $_->{inline} ) {
		$inline_status = 1;
	}
   } @$columns;

</%init>