8
8
*/
9
9
( function ( angular ) {
10
10
11
- angular . module ( 'mdKeyboard' , [ 'material.components.bottomSheet ' ] ) ;
11
+ angular . module ( 'mdKeyboard' , [ 'material.core ' ] ) ;
12
12
13
13
/* See http://www.greywyvern.com/code/javascript/keyboard for examples
14
14
* and usage instructions.
@@ -1089,49 +1089,168 @@ angular
1089
1089
1090
1090
angular
1091
1091
. module ( 'mdKeyboard' )
1092
- . provider ( 'mdKeyboard' , MdKeyboardProvider ) ;
1093
-
1094
- function MdKeyboardProvider ( keyboardLayouts , keyboardDeadkey ) {
1095
- var self = this ;
1096
-
1097
- this . layouts = keyboardLayouts ;
1098
- this . deadkey = keyboardDeadkey ;
1099
- this . themable = true ;
1100
- this . layout = 'US International' ;
1101
- this . symbols = {
1102
- '\u00a0' : "NB\nSP" , '\u200b' : "ZW\nSP" , '\u200c' : "ZW\nNJ" , '\u200d' : "ZW\nJ"
1103
- } ;
1104
- this . numpad = [
1092
+ . constant ( 'keyboardNumpad' , [
1105
1093
[ [ "$" ] , [ "\u00a3" ] , [ "\u20ac" ] , [ "\u00a5" ] ] ,
1106
1094
[ [ "7" ] , [ "8" ] , [ "9" ] , [ "/" ] ] ,
1107
1095
[ [ "4" ] , [ "5" ] , [ "6" ] , [ "*" ] ] ,
1108
1096
[ [ "1" ] , [ "2" ] , [ "3" ] , [ "-" ] ] ,
1109
1097
[ [ "0" ] , [ "." ] , [ "=" ] , [ "+" ] ]
1110
- ] ;
1098
+ ] ) ;
1111
1099
1112
- console . log ( this . deadkey ) ;
1100
+ angular
1101
+ . module ( 'mdKeyboard' )
1102
+ . constant ( 'keyboardSymbols' , {
1103
+ '\u00a0' : "NB\nSP" , '\u200b' : "ZW\nSP" , '\u200c' : "ZW\nNJ" , '\u200d' : "ZW\nJ"
1104
+ } ) ;
1113
1105
1114
- this . setThemable = function ( value ) {
1115
- this . themable = ! ! value ;
1116
- } ;
1117
- this . setLayout = function ( value ) {
1118
- this . layout = value || 'US International' ;
1106
+ angular
1107
+ . module ( 'mdKeyboard' )
1108
+ . provider ( '$mdKeyboardProvider' , MdKeyboardProvider ) ;
1109
+
1110
+ function MdKeyboardProvider ( keyboardLayouts , keyboardDeadkey , keyboardSymbols , keyboardNumpad ) {
1111
+ // how fast we need to flick down to close the sheet, pixels/ms
1112
+ var CLOSING_VELOCITY = 0.5 ;
1113
+ var PADDING = 80 ; // same as css
1114
+
1115
+ return keyboardProvider = {
1116
+ themable : true ,
1117
+ onShow : onShow ,
1118
+ onRemove : onRemove ,
1119
+ clickOutsideToClose : true ,
1120
+ disableParentScroll : true ,
1121
+
1122
+ layouts : keyboardLayouts ,
1123
+ deadkey : keyboardDeadkey ,
1124
+ symbols : keyboardSymbols ,
1125
+ numpad : keyboardNumpad ,
1126
+ layout : 'US International' ,
1127
+
1128
+ setNonce : function ( nonceValue ) {
1129
+ nonce = nonceValue ;
1130
+ } ,
1131
+ setDefaultTheme : function ( theme ) {
1132
+ defaultTheme = theme ;
1133
+ } ,
1134
+ alwaysWatchTheme : function ( alwaysWatch ) {
1135
+ alwaysWatchTheme = alwaysWatch ;
1136
+ } ,
1137
+ generateThemesOnDemand : function ( onDemand ) {
1138
+ generateOnDemand = onDemand ;
1139
+ } ,
1140
+ $get : function ( ) { }
1119
1141
} ;
1120
1142
1121
- this . $get = function ( ) {
1143
+ function onShow ( scope , element , options , controller ) {
1144
+
1145
+ element = $mdUtil . extractElementByName ( element , 'md-keyboard' ) ;
1146
+
1147
+ if ( options . clickOutsideToClose ) {
1148
+ backdrop . on ( 'click' , function ( ) {
1149
+ $mdUtil . nextTick ( $mdKeyboard . cancel , true ) ;
1150
+ } ) ;
1151
+ }
1152
+
1153
+ $mdTheming . inherit ( backdrop , options . parent ) ;
1154
+
1155
+ var keyboard = new Keyboard ( element , options . parent ) ;
1156
+ options . keyboard = keyboard ;
1157
+
1158
+ $mdTheming . inherit ( keyboard . element , options . parent ) ;
1159
+
1160
+ if ( options . disableParentScroll ) {
1161
+ options . restoreScroll = $mdUtil . disableScrollAround ( keyboard . element , options . parent ) ;
1162
+ }
1163
+
1164
+ return $animate . enter ( keyboard . element , options . parent )
1165
+ . then ( function ( ) {
1166
+ var focusable = $mdUtil . findFocusTarget ( element ) || angular . element (
1167
+ element [ 0 ] . querySelector ( 'button' ) ||
1168
+ element [ 0 ] . querySelector ( 'a' ) ||
1169
+ element [ 0 ] . querySelector ( '[ng-click]' )
1170
+ ) ;
1171
+ focusable . focus ( ) ;
1172
+
1173
+ if ( options . escapeToClose ) {
1174
+ options . rootElementKeyupCallback = function ( e ) {
1175
+ if ( e . keyCode === $mdConstant . KEY_CODE . ESCAPE ) {
1176
+ $mdUtil . nextTick ( $mdKeyboard . cancel , true ) ;
1177
+ }
1178
+ } ;
1179
+ $rootElement . on ( 'keyup' , options . rootElementKeyupCallback ) ;
1180
+ }
1181
+ } ) ;
1182
+
1183
+ }
1184
+
1185
+ function onRemove ( scope , element , options ) {
1186
+
1187
+ var keyboard = options . keyboard ;
1188
+
1189
+ $animate . leave ( backdrop ) ;
1190
+ return $animate . leave ( keyboard . element ) . then ( function ( ) {
1191
+ if ( options . disableParentScroll ) {
1192
+ options . restoreScroll ( ) ;
1193
+ delete options . restoreScroll ;
1194
+ }
1195
+
1196
+ keyboard . cleanup ( ) ;
1197
+ } ) ;
1198
+ }
1199
+
1200
+ /**
1201
+ * Keyboard class to apply bottom-sheet behavior to an element
1202
+ */
1203
+ function Keyboard ( element , parent ) {
1204
+ var deregister = $mdGesture . register ( parent , 'drag' , { horizontal : false } ) ;
1205
+ parent
1206
+ . on ( '$md.dragstart' , onDragStart )
1207
+ . on ( '$md.drag' , onDrag )
1208
+ . on ( '$md.dragend' , onDragEnd ) ;
1209
+
1122
1210
return {
1123
- getLayout : function ( value ) {
1124
- return self . layouts [ value || self . layout ] ;
1211
+ element : element ,
1212
+ cleanup : function cleanup ( ) {
1213
+ deregister ( ) ;
1214
+ parent . off ( '$md.dragstart' , onDragStart ) ;
1215
+ parent . off ( '$md.drag' , onDrag ) ;
1216
+ parent . off ( '$md.dragend' , onDragEnd ) ;
1125
1217
}
1126
1218
} ;
1127
- } ;
1219
+
1220
+ function onDragStart ( ev ) {
1221
+ // Disable transitions on transform so that it feels fast
1222
+ element . css ( $mdConstant . CSS . TRANSITION_DURATION , '0ms' ) ;
1223
+ }
1224
+
1225
+ function onDrag ( ev ) {
1226
+ var transform = ev . pointer . distanceY ;
1227
+ if ( transform < 5 ) {
1228
+ // Slow down drag when trying to drag up, and stop after PADDING
1229
+ transform = Math . max ( - PADDING , transform / 2 ) ;
1230
+ }
1231
+ element . css ( $mdConstant . CSS . TRANSFORM , 'translate3d(0,' + ( PADDING + transform ) + 'px,0)' ) ;
1232
+ }
1233
+
1234
+ function onDragEnd ( ev ) {
1235
+ if ( ev . pointer . distanceY > 0 &&
1236
+ ( ev . pointer . distanceY > 20 || Math . abs ( ev . pointer . velocityY ) > CLOSING_VELOCITY ) ) {
1237
+ var distanceRemaining = element . prop ( 'offsetHeight' ) - ev . pointer . distanceY ;
1238
+ var transitionDuration = Math . min ( distanceRemaining / ev . pointer . velocityY * 0.75 , 500 ) ;
1239
+ element . css ( $mdConstant . CSS . TRANSITION_DURATION , transitionDuration + 'ms' ) ;
1240
+ $mdUtil . nextTick ( $mdKeyboard . cancel , true ) ;
1241
+ } else {
1242
+ element . css ( $mdConstant . CSS . TRANSITION_DURATION , '' ) ;
1243
+ element . css ( $mdConstant . CSS . TRANSFORM , '' ) ;
1244
+ }
1245
+ }
1246
+ }
1128
1247
}
1129
1248
1130
1249
angular
1131
1250
. module ( 'mdKeyboard' )
1132
1251
. directive ( 'mdKeyboard' , MdKeyboardDirective ) ;
1133
1252
1134
- function MdKeyboardDirective ( $injector , $animate , $mdConstant , $mdUtil , $mdTheming , $mdBottomSheet , $rootElement , $mdGesture ) {
1253
+ function MdKeyboardDirective ( $injector , $animate , $mdConstant , $mdUtil , $mdTheming , $mdKeyboardProvider , $rootElement , $mdGesture ) {
1135
1254
return {
1136
1255
restrict : 'A' ,
1137
1256
require : '?ngModel' ,
@@ -1147,7 +1266,7 @@ function MdKeyboardDirective($injector, $animate, $mdConstant, $mdUtil, $mdThemi
1147
1266
return ;
1148
1267
}
1149
1268
1150
- var bottomSheet ;
1269
+ var keyboard ;
1151
1270
1152
1271
// Don't show virtual keyboard in mobile devices (default)
1153
1272
if ( $injector . has ( 'UAParser' ) ) {
@@ -1172,76 +1291,28 @@ function MdKeyboardDirective($injector, $animate, $mdConstant, $mdUtil, $mdThemi
1172
1291
*/
1173
1292
element
1174
1293
. bind ( 'focus' , showKeyboard )
1175
- . bind ( 'blur' , hideKeyboard ) ;
1294
+ /* .bind('blur', hideKeyboard)*/ ;
1176
1295
1177
1296
function showKeyboard ( ) {
1178
- bottomSheet = $mdBottomSheet
1179
- . show ( {
1180
- template :'<md-bottom-sheet class=md-grid layout=column ng-cloak><div ng-repeat="row in keyboard.keys" layout=row><div flex=shrink><md-button ng-repeat="key in row" class=md-raised ng-click=pressed($event) aria-label="Key {{key[0]}}">{{key[0]}}</md-button></div></div></md-bottom-sheet>' ,
1181
- controller : KeyboardController ,
1182
- clickOutsideToClose : attrs . clickOutsideToClose || false ,
1183
- escapeToClose : attrs . escapeToClose || false ,
1184
- preserveScope : attrs . preserveScope || true ,
1185
- useBackdrop : attrs . useBackdrop || false ,
1186
- onShow : onShowKeyboard
1187
- } ) ;
1297
+ keyboard = $mdKeyboardProvider . show ( {
1298
+ template :'<md-bottom-sheet class=md-grid layout=column ng-cloak><div ng-repeat="row in keyboard.keys" layout=row><div flex=shrink><md-button ng-repeat="key in row" class=md-raised ng-click=pressed($event) aria-label="Key {{key[0]}}">{{key[0]}}</md-button></div></div></md-bottom-sheet>' ,
1299
+ controller : KeyboardController ,
1300
+ clickOutsideToClose : attrs . clickOutsideToClose || false ,
1301
+ escapeToClose : attrs . escapeToClose || false ,
1302
+ preserveScope : attrs . preserveScope || true ,
1303
+ useBackdrop : attrs . useBackdrop || false
1304
+ } ) ;
1188
1305
}
1189
1306
1190
1307
function hideKeyboard ( ) {
1191
- if ( bottomSheet ) {
1192
- $mdBottomSheet . hide ( ) ;
1193
- bottomSheet = undefined ;
1308
+ if ( keyboard ) {
1309
+ $mdKeyboardProvider . hide ( ) ;
1310
+ keyboard = undefined ;
1194
1311
}
1195
1312
}
1196
1313
1197
- function onShowKeyboard ( scope , element , options , controller ) {
1198
- $log . debug ( element ) ;
1199
- element = $mdUtil . extractElementByName ( element , 'md-bottom-sheet' ) ;
1200
-
1201
- // Add a backdrop that will close on click
1202
- backdrop = $mdUtil . createBackdrop ( scope , "md-bottom-sheet-backdrop md-opaque" ) ;
1203
-
1204
- if ( options . clickOutsideToClose ) {
1205
- backdrop . on ( 'click' , function ( ) {
1206
- $mdUtil . nextTick ( $mdBottomSheet . cancel , true ) ;
1207
- } ) ;
1208
- }
1209
-
1210
- $mdTheming . inherit ( backdrop , options . parent ) ;
1211
-
1212
- $animate . enter ( backdrop , options . parent , null ) ;
1213
-
1214
- var bottomSheet = new BottomSheet ( element , options . parent ) ;
1215
- options . bottomSheet = bottomSheet ;
1216
-
1217
- $mdTheming . inherit ( bottomSheet . element , options . parent ) ;
1218
-
1219
- if ( options . disableParentScroll ) {
1220
- options . restoreScroll = $mdUtil . disableScrollAround ( bottomSheet . element , options . parent ) ;
1221
- }
1222
-
1223
- return $animate
1224
- . enter ( bottomSheet . element , options . parent )
1225
- . then ( function ( ) {
1226
- var focusable = $mdUtil . findFocusTarget ( element ) || angular . element (
1227
- element [ 0 ] . querySelector ( 'button' ) ||
1228
- element [ 0 ] . querySelector ( 'a' ) ||
1229
- element [ 0 ] . querySelector ( '[ng-click]' )
1230
- ) ;
1231
- focusable . focus ( ) ;
1232
-
1233
- if ( options . escapeToClose ) {
1234
- options . rootElementKeyupCallback = function ( e ) {
1235
- if ( e . keyCode === $mdConstant . KEY_CODE . ESCAPE ) {
1236
- $mdUtil . nextTick ( $mdBottomSheet . cancel , true ) ;
1237
- }
1238
- } ;
1239
- $rootElement . on ( 'keyup' , options . rootElementKeyupCallback ) ;
1240
- }
1241
- } ) ;
1242
- }
1243
-
1244
1314
function KeyboardController ( $scope , $log , mdKeyboard ) {
1315
+ //$log.debug(mdKeyboard, element);
1245
1316
//element.blur();
1246
1317
//element.focus();
1247
1318
@@ -1256,6 +1327,12 @@ function MdKeyboardDirective($injector, $animate, $mdConstant, $mdUtil, $mdThemi
1256
1327
scope . $on ( '$destroy' , function ( ) {
1257
1328
hideKeyboard ( ) ;
1258
1329
} ) ;
1330
+
1331
+ // When navigation force destroys an interimElement, then
1332
+ // listen and $destroy() that interim instance...
1333
+ scope . $on ( '$destroy' , function ( ) {
1334
+ $mdKeyboardProvider . destroy ( ) ;
1335
+ } ) ;
1259
1336
}
1260
1337
}
1261
1338
}
0 commit comments