Desktop markdown wiki app. Built with node, Electron Framework and AngularJS.

image-view-ctrl.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @ngdoc function
  3. * @name domainManagerApp.controller:AboutCtrl
  4. * @description
  5. * # AboutCtrl
  6. * Controller of the domainManagerApp
  7. */
  8. angular.module('codexApp.imageView', [])
  9. .controller('ImageViewCtrl',['$scope', '$rootScope', '$state', 'FileService', function ($scope, $rootScope, $state, FileService) {
  10. var filesystem = require("fs");
  11. console.log('-> Image View opened!')
  12. $scope.note = FileService.getCurrentNote();
  13. $scope.container = "note-container";
  14. $scope.image_path = $scope.note.path;
  15. $scope.fixRelativeURL = function(current_url, relative_url) {
  16. console.log("-> Fixing URL")
  17. console.log(" * Relative URL: " + relative_url)
  18. console.log(" * Note URL: " + current_url)
  19. // split urls and create arrays
  20. var current_path = current_url.split('/');
  21. var relative_path = relative_url.split('/');
  22. // remove the current note's filename from the url
  23. current_path.pop();
  24. // count how many folders the relative path goes back and erase '..'
  25. var count = 0;
  26. for (var i = 0; i < relative_path.length; i++) {
  27. if(relative_path[i] == ".."){
  28. count = count + 1;
  29. relative_path[i] = "";
  30. }
  31. }
  32. // make the relative path a string again
  33. relative_path = relative_path.join('/');
  34. // remove the same count of folders from the end of the current notes url
  35. for (var i = 0; i < count; i++) {
  36. current_path.pop();
  37. }
  38. // make the current note's url a string again
  39. current_path = current_path.join('/');
  40. // add a '/' if the relative url pointed to a file or folder above the current notes root
  41. if(count == 0){
  42. var fixed_url = current_path + "/" + relative_path;
  43. } else {
  44. var fixed_url = current_path + relative_path;
  45. }
  46. // return the fixed relative url
  47. console.log(" * Fixed URL: " + fixed_url)
  48. return fixed_url;
  49. }
  50. $scope.absoluteToRelativeURL = function(current_url, absolute_url) {
  51. console.log("-> Converting absolute URL to relative")
  52. console.log(" * Absolute URL: " + absolute_url)
  53. console.log(" * Note URL: " + current_url)
  54. // split urls and create arrays
  55. var current_path = current_url.split('/');
  56. var absolute_path = $scope.getUrlParts(absolute_url).pathname.split('/');
  57. // remove the current note's filename from the url and the image filename from the url
  58. current_path.pop();
  59. current_path.shift();
  60. absolute_path.shift();
  61. // count how many folders the current path has
  62. var current_path_count = 0;
  63. for (var i = 0; i < current_path.length; i++) {
  64. current_path_count = current_path_count + 1;
  65. }
  66. // count how many folders the absolute path has
  67. var absolute_path_count = 0;
  68. for (var i = 0; i < absolute_path.length; i++) {
  69. absolute_path_count = absolute_path_count + 1;
  70. }
  71. absolute_path_count = absolute_path_count - 1;
  72. console.log(" * Cleaned current URL (" + current_path_count + " parts): " + current_path.join('/'))
  73. console.log(" * Cleaned absolute URL (" + absolute_path_count + " parts): " + absolute_path.join('/'))
  74. dif = current_path_count - (absolute_path_count -1);
  75. for (var i = 0; i < absolute_path_count; i++) {
  76. absolute_path.shift();
  77. }
  78. console.log(" * Modified current URL (" + current_path_count + " parts): " + current_path.join('/'))
  79. console.log(" * Modified absolute URL (" + absolute_path_count + " parts): " + absolute_path.join('/'))
  80. // make the relative path a string again
  81. var relative_path = absolute_path.join('/');
  82. console.log(" * Converted relative URL: " + relative_path)
  83. return relative_path;
  84. }
  85. $scope.getUrlParts = function(url) {
  86. var a = document.createElement('a');
  87. a.href = url;
  88. return {
  89. href: a.href,
  90. host: a.host,
  91. hostname: a.hostname,
  92. port: a.port,
  93. pathname: a.pathname,
  94. protocol: a.protocol,
  95. hash: a.hash,
  96. search: a.search
  97. };
  98. }
  99. }]);