mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
78 lines
2.3 KiB
JavaScript
78 lines
2.3 KiB
JavaScript
var ITEM_TPL_REORDER_BUTTON =
|
|
'<div data-prevent-scroll="true" class="item-right-edit item-reorder ng-hide">' +
|
|
'</div>';
|
|
|
|
/**
|
|
* @ngdoc directive
|
|
* @name ionReorderButton
|
|
* @parent ionic.directive:ionItem
|
|
* @module ionic
|
|
* @restrict E
|
|
* Creates a reorder button inside a list item, that is visible when the
|
|
* {@link ionic.directive:ionList ionList parent's} `show-reorder` evaluates to true or
|
|
* `$ionicListDelegate.showReorder(true)` is called.
|
|
*
|
|
* Can be dragged to reorder items in the list. Takes any ionicon class.
|
|
*
|
|
* When an item reorder is complete, the `on-reorder` callback given in the attribute is called
|
|
* (see below).
|
|
*
|
|
* See {@link ionic.directive:ionList} for a complete example.
|
|
*
|
|
* @usage
|
|
*
|
|
* ```html
|
|
* <ion-list ng-controller="MyCtrl">
|
|
* <ion-item ng-repeat="item in items">
|
|
* Item {{$index}}
|
|
* <ion-reorder-button class="ion-navicon"
|
|
* on-reorder="moveItem(item, $fromIndex, $toIndex)">
|
|
* </ion-reorder>
|
|
* </ion-item>
|
|
* </ion-list>
|
|
* ```
|
|
* ```js
|
|
* function MyCtrl($scope) {
|
|
* $scope.items = [1, 2, 3, 4];
|
|
* $scope.moveItem = function(item, fromIndex, toIndex) {
|
|
* //Move the item in the array
|
|
* $scope.items.splice(fromIndex, 1);
|
|
* $scope.items.splice(toIndex, 0, item);
|
|
* };
|
|
* }
|
|
* ```
|
|
*
|
|
* @param {expression=} on-reorder Expression to call when an item is reordered.
|
|
* Parameters given: $fromIndex, $toIndex.
|
|
*/
|
|
IonicModule
|
|
.directive('ionReorderButton', ['$animate', function($animate) {
|
|
return {
|
|
restrict: 'E',
|
|
require: ['^ionItem', '^ionList'],
|
|
priority: Number.MAX_VALUE,
|
|
compile: function($element, $attr) {
|
|
$attr.$set('class', ($attr['class'] || '') + ' button icon button-icon', true);
|
|
$element[0].setAttribute('data-prevent-scroll', true);
|
|
return function($scope, $element, $attr, ctrls) {
|
|
var itemCtrl = ctrls[0];
|
|
var listCtrl = ctrls[1];
|
|
$scope.$onReorder = function(oldIndex, newIndex) {
|
|
$scope.$eval($attr.onReorder, {
|
|
$fromIndex: oldIndex,
|
|
$toIndex: newIndex
|
|
});
|
|
};
|
|
|
|
var container = jqLite(ITEM_TPL_REORDER_BUTTON);
|
|
container.append($element);
|
|
itemCtrl.$element.append(container).addClass('item-right-editable');
|
|
|
|
if (listCtrl.showReorder()) {
|
|
$animate.removeClass(container, 'ng-hide');
|
|
}
|
|
};
|
|
}
|
|
};
|
|
}]);
|