|
| 1 | +module Angular.NgModelController |
| 2 | + ( NgModelController(..) |
| 3 | + , NgModelCtrl(..) |
| 4 | + , Parser(..) |
| 5 | + , Formatter(..) |
| 6 | + , ValidationErrorKey(..) |
| 7 | + , render |
| 8 | + , setRender |
| 9 | + , isEmpty |
| 10 | + , setIsEmpty |
| 11 | + , setValidity |
| 12 | + , setPristine |
| 13 | + , setViewValue |
| 14 | + , viewValue |
| 15 | + , modelValue |
| 16 | + , setModelValue |
| 17 | + , appendParsers |
| 18 | + , prependParsers |
| 19 | + , appendFormatters |
| 20 | + , prependFormatters |
| 21 | + , appendViewChangeListeners |
| 22 | + , prependViewChangeListeners |
| 23 | + , error |
| 24 | + , pristine |
| 25 | + , dirty |
| 26 | + , valid |
| 27 | + , invalid |
| 28 | + ) where |
| 29 | + |
| 30 | +import Control.Monad.Eff |
| 31 | +import Data.Maybe |
| 32 | + |
| 33 | +foreign import data NgModelController :: * -> * |
| 34 | + |
| 35 | +foreign import data NgModelCtrl :: ! |
| 36 | + |
| 37 | +type ValidationErrorKey = String |
| 38 | + |
| 39 | +type Parser a = String -> Maybe a |
| 40 | + |
| 41 | +type Formatter a = a -> String |
| 42 | + |
| 43 | +foreign import render |
| 44 | + " function render($ctrl){ \ |
| 45 | + \ return function(){ \ |
| 46 | + \ return $ctrl.$render(); \ |
| 47 | + \ }; \ |
| 48 | + \ } " |
| 49 | + :: forall e a. NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 50 | + |
| 51 | +foreign import setRender |
| 52 | + " function setRender(render){ \ |
| 53 | + \ return function($ctrl){ \ |
| 54 | + \ $ctrl.$render = render; \ |
| 55 | + \ return {}; \ |
| 56 | + \ }; \ |
| 57 | + \ } " |
| 58 | + :: forall e a. Eff (ngmodel :: NgModelCtrl | e) Unit -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 59 | + |
| 60 | +foreign import isEmpty |
| 61 | + " function isEmpty(value){ \ |
| 62 | + \ return function($ctrl){ \ |
| 63 | + \ return $ctrl.$isEmpty(value); \ |
| 64 | + \ }; \ |
| 65 | + \ } " |
| 66 | + :: forall e a. a -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Boolean |
| 67 | + |
| 68 | +foreign import setIsEmpty |
| 69 | + " function setIsEmpty(isEmpty){ \ |
| 70 | + \ return function($ctrl){ \ |
| 71 | + \ $ctrl.$isEmpty = function(a){return isEmpty(a)();}; \ |
| 72 | + \ return $ctrl; \ |
| 73 | + \ }; \ |
| 74 | + \ } " |
| 75 | + :: forall e a b. (b -> Eff (ngmodel :: NgModelCtrl | e) Boolean) -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) (NgModelController b) |
| 76 | + |
| 77 | +foreign import setValidity |
| 78 | + " function setValidity(validationErrorKey){ \ |
| 79 | + \ return function(isValid){ \ |
| 80 | + \ return function($ctrl){ \ |
| 81 | + \ return $ctrl.$setValidity(validationErrorKey, isValid); \ |
| 82 | + \ }; \ |
| 83 | + \ }; \ |
| 84 | + \ } " |
| 85 | + :: forall e a. ValidationErrorKey -> Boolean -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 86 | + |
| 87 | +foreign import setPristine |
| 88 | + " function setPristine($ctrl){ \ |
| 89 | + \ return function(){ \ |
| 90 | + \ return $ctrl.$setPristine(); \ |
| 91 | + \ }; \ |
| 92 | + \ } " |
| 93 | + :: forall e a. NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 94 | + |
| 95 | +foreign import setViewValue |
| 96 | + " function setViewValue(value){ \ |
| 97 | + \ return function($ctrl){ \ |
| 98 | + \ return function(){ \ |
| 99 | + \ return $ctrl.$setViewValue(value); \ |
| 100 | + \ }; \ |
| 101 | + \ }; \ |
| 102 | + \ } " |
| 103 | + :: forall e a. String -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 104 | + |
| 105 | +foreign import viewValue |
| 106 | + " function viewValue($ctrl){ \ |
| 107 | + \ return function(){ \ |
| 108 | + \ return $ctrl.$viewValue; \ |
| 109 | + \ }; \ |
| 110 | + \ } " |
| 111 | + :: forall e a. NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) String |
| 112 | + |
| 113 | +foreign import modelValue |
| 114 | + " function modelValue($ctrl){ \ |
| 115 | + \ return function(){ \ |
| 116 | + \ return $ctrl.$modelValue; \ |
| 117 | + \ }; \ |
| 118 | + \ } " |
| 119 | + :: forall e a. NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) a |
| 120 | + |
| 121 | +foreign import setModelValue |
| 122 | + " function setModelValue(value){ \ |
| 123 | + \ return function($ctrl){ \ |
| 124 | + \ return function(){ \ |
| 125 | + \ $ctrl.$modelValue = value; \ |
| 126 | + \ return $ctrl; \ |
| 127 | + \ }; \ |
| 128 | + \ }; \ |
| 129 | + \ } " |
| 130 | + :: forall e a b. b -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) (NgModelController b) |
| 131 | + |
| 132 | +foreign import appendParsers |
| 133 | + " function appendParsers(parsers){ \ |
| 134 | + \ return function($ctrl){ \ |
| 135 | + \ return function(){ \ |
| 136 | + \ var as = []; \ |
| 137 | + \ angular.forEach(parsers, function(p){ \ |
| 138 | + \ as.push(function(v){ \ |
| 139 | + \ return p(v).values[0]; \ |
| 140 | + \ }); \ |
| 141 | + \ }); \ |
| 142 | + \ $ctrl.$parsers.push.apply($ctrl.$parsers, as); \ |
| 143 | + \ return {}; \ |
| 144 | + \ }; \ |
| 145 | + \ }; \ |
| 146 | + \ } " |
| 147 | + :: forall e a. [Parser a] -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 148 | + |
| 149 | +foreign import prependParsers |
| 150 | + " function prependParsers(parsers){ \ |
| 151 | + \ return function($ctrl){ \ |
| 152 | + \ return function(){ \ |
| 153 | + \ var as = []; \ |
| 154 | + \ angular.forEach(parsers, function(p){ \ |
| 155 | + \ as.push(function(v){ \ |
| 156 | + \ return p(v).values[0]; \ |
| 157 | + \ }); \ |
| 158 | + \ }); \ |
| 159 | + \ $ctrl.$parsers.unshift.apply($ctrl.$parsers, as); \ |
| 160 | + \ return {}; \ |
| 161 | + \ }; \ |
| 162 | + \ }; \ |
| 163 | + \ } " |
| 164 | + :: forall e a. [Parser a] -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 165 | + |
| 166 | +foreign import appendFormatters |
| 167 | + " function appendFormatters(formatters){ \ |
| 168 | + \ return function($ctrl){ \ |
| 169 | + \ return function(){ \ |
| 170 | + \ $ctrl.$formatters.push.apply($ctrl.$formatters, formatters); \ |
| 171 | + \ return {}; \ |
| 172 | + \ }; \ |
| 173 | + \ }; \ |
| 174 | + \ } " |
| 175 | + :: forall e a. [Formatter a] -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 176 | + |
| 177 | +foreign import prependFormatters |
| 178 | + " function prependFormatters(formatters){ \ |
| 179 | + \ return function($ctrl){ \ |
| 180 | + \ return function(){ \ |
| 181 | + \ $ctrl.$formatters.unshift.apply($ctrl.$formatters, formatters); \ |
| 182 | + \ return {}; \ |
| 183 | + \ }; \ |
| 184 | + \ }; \ |
| 185 | + \ } " |
| 186 | + :: forall e a. [Formatter a] -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 187 | + |
| 188 | +foreign import appendViewChangeListeners |
| 189 | + " function appendViewChangeListeners(listeners){ \ |
| 190 | + \ return function($ctrl){ \ |
| 191 | + \ return function(){ \ |
| 192 | + \ $ctrl.$viewChangeListeners.push.apply($ctrl.$viewChangeListeners, listeners); \ |
| 193 | + \ return {}; \ |
| 194 | + \ }; \ |
| 195 | + \ }; \ |
| 196 | + \ } " |
| 197 | + :: forall e a. [Eff e Unit] -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 198 | + |
| 199 | +foreign import prependViewChangeListeners |
| 200 | + " function prependViewChangeListeners(listeners){ \ |
| 201 | + \ return function($ctrl){ \ |
| 202 | + \ return function(){ \ |
| 203 | + \ $ctrl.$viewChangeListeners.unshift.apply($ctrl.$viewChangeListeners, listeners); \ |
| 204 | + \ return {}; \ |
| 205 | + \ }; \ |
| 206 | + \ }; \ |
| 207 | + \ } " |
| 208 | + :: forall e a. [Eff e Unit] -> NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Unit |
| 209 | + |
| 210 | +foreign import error |
| 211 | + " function error($ctrl){ \ |
| 212 | + \ return function(){ \ |
| 213 | + \ return $ctrl.$error; \ |
| 214 | + \ }; \ |
| 215 | + \ } " |
| 216 | + :: forall e a b. NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) { | b } |
| 217 | + |
| 218 | +foreign import pristine |
| 219 | + " function pristine($ctrl){ \ |
| 220 | + \ return function(){ \ |
| 221 | + \ return $ctrl.$pristine; \ |
| 222 | + \ }; \ |
| 223 | + \ } " |
| 224 | + :: forall e a. NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Boolean |
| 225 | + |
| 226 | +foreign import dirty |
| 227 | + " function dirty($ctrl){ \ |
| 228 | + \ return function(){ \ |
| 229 | + \ return $ctrl.$dirty; \ |
| 230 | + \ }; \ |
| 231 | + \ } " |
| 232 | + :: forall e a. NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Boolean |
| 233 | + |
| 234 | +foreign import valid |
| 235 | + " function valid($ctrl){ \ |
| 236 | + \ return function(){ \ |
| 237 | + \ return $ctrl.$valid; \ |
| 238 | + \ }; \ |
| 239 | + \ } " |
| 240 | + :: forall e a. NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Boolean |
| 241 | + |
| 242 | +foreign import invalid |
| 243 | + " function invalid($ctrl){ \ |
| 244 | + \ return function(){ \ |
| 245 | + \ return $ctrl.$invalid; \ |
| 246 | + \ }; \ |
| 247 | + \ } " |
| 248 | + :: forall e a. NgModelController a -> Eff (ngmodel :: NgModelCtrl | e) Boolean |
0 commit comments