1- module Todomvc.Controller where
1+ module Todomvc.Controller ( todoctrl ) where
22
33import qualified Data.Array as A
44import qualified Data.String as S
55import Data.Foldable
66import Data.Maybe
77import Control.Monad (unless , when )
88import Control.Monad.Eff
9- import Control.Monad.ST (ST (.. ), STArray (.. ), newSTArray , runSTArray )
9+ import Control.Monad.ST (ST (), STArray (), newSTArray , runSTArray )
1010
1111import Angular (copy , extend )
12- import Angular.Controller ( ReadScopeState (.. ), WriteScopeState (..), controller , readScopeState , writeScopeState , modifyScopeState )
13- import Angular.Location ( location , getPath , setPath )
14- import Angular.Scope ( Scope (..), watch )
12+ import Angular.Location ( Location ( ), getPath , setPath )
13+ import Angular.Scope ( Scope (), watch , readScope , extendScope , modifyScope )
14+ import Angular.This ( readThis , writeThis )
1515import Angular.ST (readSTArray , pushSTArray , pushAllSTArray , writeSTArray , spliceSTArray )
1616
17- import Todomvc.Storage (Store (.. ), Todo (.. ), get , put )
17+ import Todomvc.Storage (Store (), Todo (), get , put )
1818
1919addTodo scope = do
20- s <- readScopeState scope
20+ s <- readScope scope
2121 let title = S .trim s.newTodo
2222 let empty = (S .length title) == 0
2323 unless empty $ do
2424 let todo = { title: title, completed: false }
2525 todos <- get
2626 put $ todos <> [todo]
2727 pushSTArray s.todos todo
28- writeScopeState { newTodo: " " , remainingCount: s.remainingCount + 1 } scope
29- return unit
28+ extendScope { newTodo: " "
29+ , remainingCount: s.remainingCount + 1 } scope
3030
3131todoCompleted scope todo
32- = modifyScopeState (\s -> do
32+ = modifyScope (\s -> do
3333 let change = if todo.completed then -1 else 1
3434 arr <- readSTArray s.todos
3535 put arr
3636 return { remainingCount: s.remainingCount + change }
3737 ) scope
3838
3939clearCompletedTodos scope = do
40- s <- readScopeState scope
40+ s <- readScope scope
4141 arr <- readSTArray s.todos
4242 let res = A .filter (\a -> not a.completed) arr
4343 put res
4444 writeSTArray s.todos res
4545
4646markAll scope compl
47- = modifyScopeState (\s -> do
47+ = modifyScope (\s -> do
4848 arr <- readSTArray s.todos
4949 let res = (\a -> a { completed = not compl }) <$> arr
5050 put res
@@ -54,10 +54,11 @@ markAll scope compl
5454
5555editTodo scope todo = do
5656 cp <- copy todo
57- writeScopeState { editedTodo: Just todo , originalTodo: Just $ cp } scope
57+ extendScope { editedTodo: Just todo
58+ , originalTodo: Just $ cp } scope
5859
5960doneEditing scope todo
60- = modifyScopeState (\s -> do
61+ = modifyScope (\s -> do
6162 let title = S .trim todo.title
6263 when (S .length title == 0 ) $ removeTodo scope todo
6364 extend todo { title: title }
@@ -67,17 +68,17 @@ doneEditing scope todo
6768 ) scope
6869
6970removeTodo scope todo = do
70- s <- readScopeState scope
71+ s <- readScope scope
7172 arr <- readSTArray s.todos
7273 let i = A .findIndex (\a -> refEq a todo) arr
7374 unless (i == -1 ) do
7475 let c = if todo.completed then 0 else -1
75- writeScopeState { remainingCount: s.remainingCount + c} scope
76+ extendScope { remainingCount: s.remainingCount + c} scope
7677 spliceSTArray s.todos i 1 []
7778 put arr
7879
7980revertEditing scope todo = do
80- s <- readScopeState scope
81+ s <- readScope scope
8182 arr <- readSTArray s.todos
8283 let i = A .findIndex (\a -> refEq a todo) arr
8384 unless (i == -1 ) do
@@ -88,40 +89,46 @@ revertEditing scope todo = do
8889 return unit
8990 Nothing -> return unit
9091
91- watchRemainingCount scope = do
92- watch " remainingCount == 0" (Just (\a1 _ _ ->
93- writeScopeState { allChecked: a1 } scope >>= (\_ -> return unit))) false scope
92+ watchRemainingCount scope =
93+ let expr = " remainingCount == 0"
94+ listener = \a _ _ -> extendScope { allChecked: a } scope
95+ in watch expr (return listener) false scope
9496
95- watchLocationPath scope = do
96- watch " location.path()" ( Just (\a1 _ _ ->
97- case a1 of
98- " /completed" -> writeScopeState { statusFilter: {completed: true } } scope >>= (\_ -> return unit)
99- " /active" -> writeScopeState { statusFilter: {completed: false } } scope >>= (\_ -> return unit)
100- _ -> writeScopeState { statusFilter: {} } scope >>= (\_ -> return unit)
101- ) ) false scope
97+ watchLocationPath scope =
98+ let expr = " location.path()"
99+ listener = \a _ _ -> case a of
100+ " /completed" -> extendScope { statusFilter: { completed: true } } scope
101+ " /active" -> extendScope { statusFilter: { completed: false } } scope
102+ _ -> extendScope { statusFilter: { } } scope
103+ in watch expr (return listener ) false scope
102104
103- todoController m =
104- controller " TodoCtrl" m $ \scope _ -> do
105- loc <- location
106- path <- getPath loc
107- if S .length path == 0 then setPath " /" loc else return " "
108- watchRemainingCount scope
109- watchLocationPath scope
110- todosRef <- newSTArray 0 { title: " " , completed: false }
111- todos <- get
112- pushAllSTArray todosRef todos
113- let remainingCount = foldl (\b a -> if a.completed then b else b + 1 ) 0 todos
114- writeScopeState { newTodo: " "
115- , editedTodo: Nothing
116- , originalTodo: Nothing
117- , todos: todosRef
118- , remainingCount: remainingCount
119- , location: loc
120- , addTodo: addTodo scope
121- , todoCompleted: todoCompleted scope
122- , clearCompletedTodos: clearCompletedTodos scope
123- , markAll: markAll scope
124- , editTodo: editTodo scope
125- , doneEditing: doneEditing scope
126- , removeTodo: removeTodo scope
127- , revertEditing: revertEditing scope } scope
105+ controller scope location = do
106+ path <- getPath location
107+ if S .length path == 0 then setPath " /" location else return " "
108+ watchRemainingCount scope
109+ watchLocationPath scope
110+ todosRef <- newSTArray 0 { title: " " , completed: false }
111+ todos <- get
112+ pushAllSTArray todosRef todos
113+ let remainingCount = foldl (\b a -> if a.completed then b else b + 1 ) 0 todos
114+ extendScope { newTodo: " "
115+ , editedTodo: Nothing
116+ , originalTodo: Nothing
117+ , todos: todosRef
118+ , remainingCount: remainingCount
119+ , location: location
120+ , addTodo: addTodo scope
121+ , todoCompleted: todoCompleted scope
122+ , clearCompletedTodos: clearCompletedTodos scope
123+ , markAll: markAll scope
124+ , editTodo: editTodo scope
125+ , doneEditing: doneEditing scope
126+ , removeTodo: removeTodo scope
127+ , revertEditing: revertEditing scope } scope
128+
129+ foreign import todoctrl
130+ " /*@ngInject*/function todoctrl($scope, $location) { \
131+ \ var impl = controller($scope)($location); \
132+ \ return impl.apply(this, []); \
133+ \ } "
134+ :: forall e a . Scope a -> Location -> Eff e Unit
0 commit comments