imExportNode.ctrl.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. angular.module('kityminderEditor')
  2. .controller('imExportNode.ctrl', function ($scope, $modalInstance, title, defaultValue, type) {
  3. $scope.title = title;
  4. $scope.value = defaultValue;
  5. $scope.type = type;
  6. $scope.ok = function () {
  7. if ($scope.value == '') {
  8. return;
  9. }
  10. $modalInstance.close($scope.value);
  11. editor.receiver.selectAll();
  12. };
  13. $scope.cancel = function () {
  14. $modalInstance.dismiss('cancel');
  15. editor.receiver.selectAll();
  16. };
  17. setTimeout(function() {
  18. $('.single-input').focus();
  19. $('.single-input')[0].setSelectionRange(0, defaultValue.length);
  20. }, 30);
  21. $scope.shortCut = function(e) {
  22. e.stopPropagation();
  23. //if (e.keyCode == 13 && e.shiftKey == false) {
  24. // $scope.ok();
  25. //}
  26. if (e.keyCode == 27) {
  27. $scope.cancel();
  28. }
  29. // tab 键屏蔽默认事件 和 backspace 键屏蔽默认事件
  30. if (e.keyCode == 8 && type == 'export') {
  31. e.preventDefault();
  32. }
  33. if (e.keyCode == 9) {
  34. e.preventDefault();
  35. var $textarea = e.target;
  36. var pos = getCursortPosition($textarea);
  37. var str = $textarea.value;
  38. $textarea.value = str.substr(0, pos) + '\t' + str.substr(pos);
  39. setCaretPosition($textarea, pos + 1);
  40. }
  41. };
  42. /*
  43. * 获取 textarea 的光标位置
  44. * @Author: Naixor
  45. * @date: 2015.09.23
  46. * */
  47. function getCursortPosition (ctrl) {
  48. var CaretPos = 0; // IE Support
  49. if (document.selection) {
  50. ctrl.focus ();
  51. var Sel = document.selection.createRange ();
  52. Sel.moveStart ('character', -ctrl.value.length);
  53. CaretPos = Sel.text.length;
  54. }
  55. // Firefox support
  56. else if (ctrl.selectionStart || ctrl.selectionStart == '0') {
  57. CaretPos = ctrl.selectionStart;
  58. }
  59. return (CaretPos);
  60. }
  61. /*
  62. * 设置 textarea 的光标位置
  63. * @Author: Naixor
  64. * @date: 2015.09.23
  65. * */
  66. function setCaretPosition(ctrl, pos){
  67. if(ctrl.setSelectionRange) {
  68. ctrl.focus();
  69. ctrl.setSelectionRange(pos,pos);
  70. } else if (ctrl.createTextRange) {
  71. var range = ctrl.createTextRange();
  72. range.collapse(true);
  73. range.moveEnd('character', pos);
  74. range.moveStart('character', pos);
  75. range.select();
  76. }
  77. }
  78. });