hyperlink.ctrl.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. angular.module('kityminderEditor')
  2. .controller('hyperlink.ctrl', function ($scope, $modalInstance, link) {
  3. var urlRegex = '^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$';
  4. $scope.R_URL = new RegExp(urlRegex, 'i');
  5. $scope.url = link.url || '';
  6. $scope.title = link.title || '';
  7. setTimeout(function() {
  8. var $linkUrl = $('#link-url');
  9. $linkUrl.focus();
  10. $linkUrl[0].setSelectionRange(0, $scope.url.length);
  11. }, 30);
  12. $scope.shortCut = function(e) {
  13. e.stopPropagation();
  14. if (e.keyCode == 13) {
  15. $scope.ok();
  16. } else if (e.keyCode == 27) {
  17. $scope.cancel();
  18. }
  19. };
  20. $scope.ok = function () {
  21. if($scope.R_URL.test($scope.url)) {
  22. $modalInstance.close({
  23. url: $scope.url,
  24. title: $scope.title
  25. });
  26. } else {
  27. $scope.urlPassed = false;
  28. var $linkUrl = $('#link-url');
  29. $linkUrl.focus();
  30. $linkUrl[0].setSelectionRange(0, $scope.url.length);
  31. }
  32. editor.receiver.selectAll();
  33. };
  34. $scope.cancel = function () {
  35. $modalInstance.dismiss('cancel');
  36. editor.receiver.selectAll();
  37. };
  38. });