| @@ -51,9 +51,9 @@ | ||
| 51 | 51 | ->first(); |
| 52 | 52 | |
| 53 | 53 | if ($firstItem) { |
| 54 | 54 | if ($firstItem->position < 0.02) { |
| 55 | - self::reIndexTasksPositions($this->toArray()); | |
| 55 | + self::reIndexStagesPositions($this->toArray()); | |
| 56 | 56 | return $this->moveToNewPosition($newIndex); |
| 57 | 57 | } |
| 58 | 58 | $index = round($firstItem->position / 2, 2); |
| 59 | 59 | } else { |
| @@ -86,27 +86,21 @@ | ||
| 86 | 86 | $this->save(); |
| 87 | 87 | return $this; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | - $newPosition = ($prevTask->position + $nextItem->position) / 2; | |
| 91 | - | |
| 92 | - // check if new position is already taken | |
| 93 | - $exist = $stageQuery | |
| 94 | - ->where('position', $newPosition) | |
| 95 | - ->where('id', '!=', $this->id) | |
| 96 | - ->first(); | |
| 97 | - | |
| 98 | - if ($exist) { | |
| 99 | - self::reIndexTasksPositions($this->toArray()); | |
| 90 | + if ($nextItem->position - $prevTask->position < 0.02) { | |
| 91 | + self::reIndexStagesPositions($this->toArray()); | |
| 100 | 92 | return $this->moveToNewPosition($newIndex); |
| 101 | 93 | } |
| 102 | 94 | |
| 95 | + $newPosition = ($prevTask->position + $nextItem->position)/ 2; | |
| 96 | + | |
| 103 | 97 | $this->position = $newPosition; |
| 104 | 98 | $this->save(); |
| 105 | 99 | return $this; |
| 106 | 100 | } |
| 107 | 101 | |
| 108 | - public static function reIndexTasksPositions($stage) | |
| 102 | + public static function reIndexStagesPositions($stage) | |
| 109 | 103 | { |
| 110 | 104 | $allStages = self::where('board_id', $stage['board_id'])->where('type', 'stage')->orderBy('position', 'asc')->whereNull('archived_at')->get(); |
| 111 | 105 | |
| 112 | 106 | foreach ($allStages as $index => $stage) { |
| @@ -112,7 +106,12 @@ | ||
| 112 | 106 | foreach ($allStages as $index => $stage) { |
| 113 | 107 | $stage->position = $index + 1; |
| 114 | 108 | $stage->save(); |
| 115 | 109 | } |
| 110 | + } | |
| 111 | + | |
| 112 | + public function tasks() | |
| 113 | + { | |
| 114 | + return $this->hasMany(Task::class, 'stage_id'); | |
| 116 | 115 | } |
| 117 | 116 | |
| 118 | 117 | } |