Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Logic problem
Message
 
 
To
All
General information
Forum:
Javascript
Category:
Coding, syntax & commands
Title:
Logic problem
Miscellaneous
Thread ID:
01652713
Message ID:
01652713
Views:
48
Hi everybody,

Here is a new puzzle. We have a mover type of the control that also allows to re-sort items (e.g. move up or down). Here is the code for up/down movement:
$scope.moveUp = function (smItems) {
                    if (smItems != 'undefined' && smItems.length > 0) {
                        smItems.forEach(function (item) {
                            var sortOrder = $scope.assignedItems.indexOf(item);
                            $scope.assignedItems.move_element(sortOrder, -1);
                        });
                        if (!$scope.keepPristine)
                            $scope.form.$setDirty();
                    };
                };

                $scope.moveDown = function (smItems) {
                    if (smItems != 'undefined' && smItems.length > 0) {
                        smItems.forEach(function (item) {
                            var sortOrder = $scope.assignedItems.indexOf(item);
                            $scope.assignedItems.move_element(sortOrder, 1);
                        });
                        if (!$scope.keepPristine)
                            $scope.form.$setDirty();
                    };
                };
The directive itself is taken from somewhere, I think. Here is the method for moving elements:
 Array.prototype.move_element = function (index, delta) {

            // This method moves an element within the array
            // index = the array item you want to move
            // delta = the direction and number of spaces to move the item.
            //
            // For example:
            // move_element(myarray, 5, -1); // move up one space
            // move_element(myarray, 2, 1); // move down one space
            //
            // Returns true for success, false for error.

            var index2, temp_item;

            // Make sure the index is within the array bounds
            if (index < 0 || index >= this.length) {
                return false;
            }

            // Make sure the target index is within the array bounds
            index2 = index + delta;
            if (index2 < 0 || index2 >= this.length || index2 == index) {
                return false;
            }

            // Move the elements in the array
            temp_item = this[index2];
            this[index2] = this[index];
            this[index] = temp_item;

            return true;
        }
So, my colleagues who are testing our application noticed the following problem (which I can reproduce). We can move items up OK (it seems), but if two items are selected with no item in between them, then we can not move them down. If I select, say, 3rd and 5th item and have more items below, I can move my items down OK (both are moved down).

Do you see what is a bug in the logic here?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform