@@ -1167,15 +1167,17 @@ angular
1167
1167
. module ( 'material.components.keyboard' )
1168
1168
. provider ( '$mdKeyboard' , MdKeyboardProvider ) ;
1169
1169
1170
- function MdKeyboardProvider ( $$interimElementProvider , keyboardLayouts , keyboardDeadkey , keyboardSymbols , keyboardNumpad ) {
1170
+ function MdKeyboardProvider ( $$interimElementProvider , $injector , keyboardLayouts , keyboardDeadkey , keyboardSymbols , keyboardNumpad ) {
1171
1171
// how fast we need to flick down to close the sheet, pixels/ms
1172
+ var SCOPE ;
1172
1173
var CLOSING_VELOCITY = 0.5 ;
1173
1174
var PADDING = 80 ; // same as css
1174
1175
var LAYOUT = 'US International' ;
1175
1176
var LAYOUTS = keyboardLayouts ;
1176
1177
var DEADKEY = keyboardDeadkey ;
1177
1178
var SYMBOLS = keyboardSymbols ;
1178
1179
var NUMPAD = keyboardNumpad ;
1180
+ var VISIBLE = false ;
1179
1181
1180
1182
var $mdKeyboard = $$interimElementProvider ( '$mdKeyboard' )
1181
1183
. setDefaults ( {
@@ -1185,14 +1187,16 @@ function MdKeyboardProvider($$interimElementProvider, keyboardLayouts, keyboardD
1185
1187
. addMethod ( 'getLayout' , getLayout )
1186
1188
. addMethod ( 'getLayouts' , getLayouts )
1187
1189
. addMethod ( 'useLayout' , useLayout )
1188
- . addMethod ( 'addLayout' , addLayout ) ;
1190
+ . addMethod ( 'addLayout' , addLayout )
1191
+ . addMethod ( 'isVisible' , isVisible ) ;
1189
1192
1190
1193
// should be available in provider (config phase) not only
1191
1194
// in service as defined in $$interimElementProvider
1192
1195
$mdKeyboard . getLayout = getLayout ;
1193
1196
$mdKeyboard . getLayouts = getLayouts ;
1194
1197
$mdKeyboard . useLayout = useLayout ;
1195
1198
$mdKeyboard . addLayout = addLayout ;
1199
+ $mdKeyboard . isVisible = isVisible ;
1196
1200
1197
1201
// get currently used layout object
1198
1202
function getLayout ( ) {
@@ -1202,7 +1206,7 @@ function MdKeyboardProvider($$interimElementProvider, keyboardLayouts, keyboardD
1202
1206
// get names of available layouts
1203
1207
function getLayouts ( ) {
1204
1208
var layouts = [ ] ;
1205
- angular . forEach ( LAYOUTS , function ( obj , layout ) {
1209
+ angular . forEach ( LAYOUTS , function ( obj , layout ) {
1206
1210
layouts . push ( layout ) ;
1207
1211
} ) ;
1208
1212
return layouts ;
@@ -1212,6 +1216,12 @@ function MdKeyboardProvider($$interimElementProvider, keyboardLayouts, keyboardD
1212
1216
function useLayout ( layout ) {
1213
1217
if ( LAYOUTS [ layout ] ) {
1214
1218
LAYOUT = layout ;
1219
+ if ( SCOPE ) {
1220
+ SCOPE . $broadcast ( '$mdKeyboardLayoutChanged' , layout ) ;
1221
+ }
1222
+ //console.log($injector.get('$rootScope'), $injector.get('$scope'));
1223
+ //$rootScope = $injector.get('$rootScope');
1224
+ //$rootScope.$broadcast('$mdKeyboardLayoutChanged', layout);
1215
1225
} else {
1216
1226
var msg = "" +
1217
1227
"The keyboard layout '" + layout + "' does not exists. \n" +
@@ -1232,6 +1242,11 @@ function MdKeyboardProvider($$interimElementProvider, keyboardLayouts, keyboardD
1232
1242
}
1233
1243
}
1234
1244
1245
+ // return if keyboard is visible
1246
+ function isVisible ( ) {
1247
+ return VISIBLE ;
1248
+ }
1249
+
1235
1250
return $mdKeyboard ;
1236
1251
1237
1252
/* @ngInject */
@@ -1263,6 +1278,9 @@ function MdKeyboardProvider($$interimElementProvider, keyboardLayouts, keyboardD
1263
1278
options . keyboard = keyboard ;
1264
1279
options . parent . prepend ( keyboard . element ) ;
1265
1280
1281
+ SCOPE = scope ;
1282
+ VISIBLE = true ;
1283
+
1266
1284
$mdTheming . inherit ( keyboard . element , options . parent ) ;
1267
1285
1268
1286
if ( options . disableParentScroll ) {
@@ -1291,6 +1309,7 @@ function MdKeyboardProvider($$interimElementProvider, keyboardLayouts, keyboardD
1291
1309
if ( options . disableParentScroll ) {
1292
1310
options . restoreScroll ( ) ;
1293
1311
delete options . restoreScroll ;
1312
+ VISIBLE = false ;
1294
1313
}
1295
1314
1296
1315
keyboard . cleanup ( ) ;
@@ -1374,7 +1393,7 @@ function MdKeyboardDirective($mdKeyboard, $mdTheming) {
1374
1393
} ;
1375
1394
}
1376
1395
1377
- function useKeyboardDirective ( $mdKeyboard , $injector , $log , $rootScope ) {
1396
+ function useKeyboardDirective ( $mdKeyboard , $injector , $timeout , $ log, $rootScope ) {
1378
1397
return {
1379
1398
restrict : 'A' ,
1380
1399
require : '?ngModel' ,
@@ -1407,25 +1426,37 @@ function useKeyboardDirective($mdKeyboard, $injector, $log, $rootScope) {
1407
1426
. bind ( 'blur' , hideKeyboard ) ;
1408
1427
1409
1428
function showKeyboard ( ) {
1410
- if ( ! keyboard ) {
1411
- keyboard = $mdKeyboard . show ( {
1412
- template :'<md-keyboard class=md-grid layout=column ng-cloak><div ng-repeat="row in keyboard.keys" layout=row><div flex ng-repeat="key in row" ng-switch=key[0] ng-class=getKeyClass(key)><span ng-switch-when=Bksp><md-button class="md-raised key-bksp" ng-mousedown="pressed($event, key[0])" aria-label={{key[0]}}><md-icon>keyboard_backspace</md-icon></md-button></span> <span ng-switch-when=Tab><md-button class="md-raised key-tab" ng-mousedown="pressed($event, key[0])" aria-label={{key[0]}}><md-icon>keyboard_tab</md-icon></md-button></span> <span ng-switch-when=Caps><md-button class="md-raised key-caps" ng-class="{\'locked\': capsLocked, \'md-focused\': capsLocked}" ng-mousedown="pressed($event, key[0])" ng-click=toggleCapsLock() aria-label={{key[0]}}><md-icon>keyboard_capslock</md-icon></md-button></span> <span ng-switch-when=Enter><md-button class="md-raised key-enter" ng-mousedown="pressed($event, key[0])" aria-label={{key[0]}}><md-icon>keyboard_return</md-icon></md-button></span> <span ng-switch-when=Shift><md-button class="md-raised key-shift" ng-mousedown="pressed($event, key[0]); toggleCaps()" ng-mouseup=toggleCaps() aria-label={{key[0]}}>{{key[0]}}</md-button></span> <span ng-switch-when=Spacer></span> <span ng-switch-default><md-button class="md-raised key-char" ng-mousedown="pressed($event, key[!capsLocked && !caps ? 0 : 1])" aria-label="{{key[!capsLocked && !caps ? 0 : 1]}}">{{key[!capsLocked && !caps ? 0 : 1]}}</md-button></span></div></div></md-keyboard>' ,
1429
+ if ( $rootScope . keyboardTimeout ) {
1430
+ $timeout . cancel ( $rootScope . keyboardTimeout ) ;
1431
+ }
1432
+
1433
+ // no keyboard active, so add new
1434
+ if ( ! $mdKeyboard . isVisible ( ) ) {
1435
+ $mdKeyboard . currentModel = ngModelCtrl ;
1436
+ $mdKeyboard . show ( {
1437
+ template :'<md-keyboard class=md-grid layout=column ng-cloak><div ng-repeat="row in keyboard.keys" layout=row><div flex ng-repeat="key in row" ng-switch=key[0] ng-class=getKeyClass(key)><span ng-switch-when=Bksp><md-button class="md-raised key-bksp" ng-mousedown="pressed($event, key[0])" aria-label={{key[0]}}><md-icon>keyboard_backspace</md-icon></md-button></span> <span ng-switch-when=Tab><md-button class="md-raised key-tab" ng-mousedown="pressed($event, key[0])" aria-label={{key[0]}}><md-icon>keyboard_tab</md-icon></md-button></span> <span ng-switch-when=Caps><md-button class="md-raised key-caps" ng-class="{\'locked\': capsLocked, \'md-focused\': capsLocked}" ng-mousedown="pressed($event, key[0])" ng-click=toggleCapsLock() aria-label={{key[0]}}><md-icon>keyboard_capslock</md-icon></md-button></span> <span ng-switch-when=Enter><md-button class="md-raised key-enter" ng-mousedown="pressed($event, key[0])" aria-label={{key[0]}}><md-icon>keyboard_return</md-icon></md-button></span> <span ng-switch-when=Shift><md-button class="md-raised key-shift" ng-mousedown="pressed($event, key[0]); toggleCaps()" aria-label={{key[0]}}>{{key[0]}}</md-button></span> <span ng-switch-when=Spacer></span> <span ng-switch-default><md-button class="md-raised key-char" ng-mousedown="pressed($event, key[!capsLocked && !caps ? 0 : 1])" aria-label="{{key[!capsLocked && !caps ? 0 : 1]}}">{{key[!capsLocked && !caps ? 0 : 1]}}</md-button></span></div></div></md-keyboard>' ,
1413
1438
controller : mdKeyboardController ,
1414
1439
bindToController : true
1415
1440
} ) ;
1416
1441
}
1442
+
1443
+ // use existing keyboard
1444
+ else {
1445
+ $mdKeyboard . currentModel = ngModelCtrl ;
1446
+ $mdKeyboard . useLayout ( attrs . useKeyboard ) ;
1447
+ }
1417
1448
}
1418
1449
1419
1450
function mdKeyboardController ( $scope ) {
1420
1451
if ( attrs . useKeyboard ) {
1421
1452
$mdKeyboard . useLayout ( attrs . useKeyboard ) ;
1422
1453
}
1423
1454
1424
- var toggleCaps = function ( ) {
1455
+ var toggleCaps = function ( ) {
1425
1456
$scope . caps = ! $scope . caps ;
1426
1457
} ;
1427
1458
1428
- var toggleCapsLock = function ( ) {
1459
+ var toggleCapsLock = function ( ) {
1429
1460
$scope . capsLocked = ! $scope . capsLocked ;
1430
1461
} ;
1431
1462
@@ -1458,11 +1489,28 @@ function useKeyboardDirective($mdKeyboard, $injector, $log, $rootScope) {
1458
1489
$scope . toggleCaps = toggleCaps ;
1459
1490
$scope . toggleCapsLock = toggleCapsLock ;
1460
1491
$scope . pressed = triggerKey ;
1492
+
1493
+ $scope . $on ( '$mdKeyboardLayoutChanged' , function ( ) {
1494
+ $scope . keyboard = $mdKeyboard . getLayout ( ) ;
1495
+ $scope . pressed = triggerKey ;
1496
+ } ) ;
1461
1497
} ;
1462
1498
1463
1499
_init ( ) ;
1464
1500
}
1465
1501
1502
+ function _getCaretPosition ( ) {
1503
+ if ( 'selectionStart' in element ) {
1504
+ return element . selectionStart ;
1505
+ } else if ( document . selection ) {
1506
+ element . focus ( ) ;
1507
+ var sel = document . selection . createRange ( ) ;
1508
+ var selLen = document . selection . createRange ( ) . text . length ;
1509
+ sel . moveStart ( 'character' , - element . value . length ) ;
1510
+ return sel . text . length - selLen ;
1511
+ }
1512
+ } ;
1513
+
1466
1514
function triggerKey ( $event , key ) {
1467
1515
$event . preventDefault ( ) ;
1468
1516
$log . debug ( 'key pressed: ' + key ) ;
@@ -1499,9 +1547,9 @@ function useKeyboardDirective($mdKeyboard, $injector, $log, $rootScope) {
1499
1547
//} else self.VKI_insert("\t");
1500
1548
//return false;
1501
1549
1502
- ngModelCtrl . $setViewValue ( ( ngModelCtrl . $viewValue || '' ) + "\t" ) ;
1503
- ngModelCtrl . $validate ( ) ;
1504
- ngModelCtrl . $render ( ) ;
1550
+ $mdKeyboard . currentModel . $setViewValue ( ( $mdKeyboard . currentModel . $viewValue || '' ) + "\t" ) ;
1551
+ $mdKeyboard . currentModel . $validate ( ) ;
1552
+ $mdKeyboard . currentModel . $render ( ) ;
1505
1553
1506
1554
break ;
1507
1555
@@ -1528,9 +1576,9 @@ function useKeyboardDirective($mdKeyboard, $injector, $log, $rootScope) {
1528
1576
//self.keyInputCallback();
1529
1577
//return true;
1530
1578
1531
- ngModelCtrl . $setViewValue ( ( ngModelCtrl . $viewValue || '' ) . slice ( 0 , - 1 ) ) ;
1532
- ngModelCtrl . $validate ( ) ;
1533
- ngModelCtrl . $render ( ) ;
1579
+ $mdKeyboard . currentModel . $setViewValue ( ( $mdKeyboard . currentModel . $viewValue || '' ) . slice ( 0 , - 1 ) ) ;
1580
+ $mdKeyboard . currentModel . $validate ( ) ;
1581
+ $mdKeyboard . currentModel . $render ( ) ;
1534
1582
1535
1583
break ;
1536
1584
@@ -1552,9 +1600,9 @@ function useKeyboardDirective($mdKeyboard, $injector, $log, $rootScope) {
1552
1600
scope . $broadcast ( '$submit' ) ;
1553
1601
scope . $root . $broadcast ( '$submit' ) ;
1554
1602
} else {
1555
- ngModelCtrl . $setViewValue ( ( ngModelCtrl . $viewValue || '' ) + "\n" ) ;
1556
- ngModelCtrl . $validate ( ) ;
1557
- ngModelCtrl . $render ( ) ;
1603
+ $mdKeyboard . currentModel . $setViewValue ( ( $mdKeyboard . currentModel . $viewValue || '' ) + "\n" ) ;
1604
+ $mdKeyboard . currentModel . $validate ( ) ;
1605
+ $mdKeyboard . currentModel . $render ( ) ;
1558
1606
}
1559
1607
1560
1608
break ;
@@ -1568,17 +1616,22 @@ function useKeyboardDirective($mdKeyboard, $injector, $log, $rootScope) {
1568
1616
//});
1569
1617
//element[0].dispatchEvent(event);
1570
1618
1571
- ngModelCtrl . $setViewValue ( ( ngModelCtrl . $viewValue || '' ) + key [ 0 ] ) ;
1572
- ngModelCtrl . $validate ( ) ;
1573
- ngModelCtrl . $render ( ) ;
1619
+ $mdKeyboard . currentModel . $setViewValue ( ( $mdKeyboard . currentModel . $viewValue || '' ) + key [ 0 ] ) ;
1620
+ $mdKeyboard . currentModel . $validate ( ) ;
1621
+ $mdKeyboard . currentModel . $render ( ) ;
1622
+
1623
+ scope . caps = false ;
1574
1624
}
1575
1625
}
1576
1626
1577
1627
function hideKeyboard ( ) {
1578
- if ( keyboard ) {
1579
- $mdKeyboard . hide ( ) ;
1580
- keyboard = undefined ;
1628
+ if ( $rootScope . keyboardTimeout ) {
1629
+ $timeout . cancel ( $rootScope . keyboardTimeout ) ;
1581
1630
}
1631
+ //keyboard.hide();
1632
+ $rootScope . keyboardTimeout = $timeout ( function ( ) {
1633
+ $mdKeyboard . hide ( ) ;
1634
+ } , 500 ) ;
1582
1635
}
1583
1636
}
1584
1637
}
0 commit comments