123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- * @ngdoc function
- * @name domainManagerApp.controller:AboutCtrl
- * @description
- * # AboutCtrl
- * Controller of the domainManagerApp
- */
- angular.module('codexApp.noteView', [])
- .controller('NoteViewCtrl',['$scope', '$rootScope', '$state', 'FileService', function ($scope, $rootScope, $state, FileService) {
- var marked = require('marked');
- marked.setOptions({
- renderer: new marked.Renderer(),
- gfm: true,
- tables: true,
- breaks: true,
- pedantic: true,
- sanitize: false,
- smartLists: true,
- smartypants: true
- });
- var filesystem = require("fs");
- console.log('-> Note View opened!')
- $scope.note = FileService.getCurrentNote();
- $scope.container = "note-container";
- $scope.html_data = "";
- $scope.loadNoteView = function() {
- filesystem.readFile($scope.note.path, function(err, data) {
-
- $scope.note.data = new Buffer(data).toString('utf8')
- if(!$scope.$$phase) {
- $scope.$apply(function(){
- $scope.html_data = marked($scope.note.data);
- });
- } else {
- $scope.html_data = marked($scope.note.data);
- }
-
- $scope.fixImgURLs($scope.note.path, $scope.html_data);
- var a = document.getElementsByTagName('a'), ajax;
- for (var i=0; i<a.length; ++i) {
- a[i].addEventListener('click', handleAnchor, false);
- var parts = $scope.getUrlParts(a[i].toString())
- if(parts.protocol == "file:"){
- a[i].className += "internal-link";
- }
- }
-
- code = document.getElementsByTagName("code");
- for (var i = 0; i < code.length; i++) {
- hljs.highlightBlock(code[i]);
- }
- function handleAnchor(e){
- e.preventDefault();
- var r = new RegExp('^(?:[a-z]+:)?//', 'i');
- if(e.srcElement.protocol == "http:" || e.srcElement.protocol == "https:"){
- console.log("-> Prevented link from opening: " + e.srcElement.outerHTML.match(/href="([^"]*)/)[1]);
- var open = require("open");
- open(e.srcElement.outerHTML.match(/href="([^"]*)/)[1]);
- }
- if(e.srcElement.protocol == "file:"){
- var url = e.srcElement.outerHTML.match(/href="([^"]*)/)[1];
-
- url = $scope.fixRelativeURL($scope.note.path, url)
- var note = FileService.getNote(url);
- FileService.setCurrentNote(note);
- $scope.note = note;
- console.log("-> Opening Link: " + note.path)
- $scope.loadNoteView();
- }
- }
- function updateContent() {
-
- }
- });
- }
- $scope.loadNoteView();
- $rootScope.$on('note-view:reload', function() {
- $scope.note = FileService.getCurrentNote();
- $scope.html_data = "";
- $scope.loadNoteView();
- });
- $scope.fixImgURLs = function(current_note_path, html){
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- var imgs = angular.element(html).find("img");
- var img_urls = []
- for (var i = 0; i < imgs.length; i++) {
- img_urls.push($scope.fixRelativeURL(current_note_path, $scope.absoluteToRelativeURL(current_note_path, imgs[i].src)));
- }
- var page_images = document.getElementsByTagName('img');
- console.log("-> Changing "+ img_urls.length + " images")
- for(var i = 0; i < img_urls.length; i++) {
- console.log(page_images[i]);
- page_images[i].src = img_urls[i];
- }
- }
- $scope.fixRelativeURL = function(current_url, relative_url) {
- console.log("-> Fixing URL")
- console.log(" * Relative URL: " + relative_url)
- console.log(" * Note URL: " + current_url)
-
- var current_path = current_url.split('/');
- var relative_path = relative_url.split('/');
-
- current_path.pop();
-
- var count = 0;
- for (var i = 0; i < relative_path.length; i++) {
- if(relative_path[i] == ".."){
- count = count + 1;
- relative_path[i] = "";
- }
- }
-
- relative_path = relative_path.join('/');
-
- for (var i = 0; i < count; i++) {
- current_path.pop();
- }
-
- current_path = current_path.join('/');
-
- if(count == 0){
- var fixed_url = current_path + "/" + relative_path;
- } else {
- var fixed_url = current_path + relative_path;
- }
-
- console.log(" * Fixed URL: " + fixed_url)
- return fixed_url;
- }
- $scope.absoluteToRelativeURL = function(current_url, absolute_url) {
- console.log("-> Converting absolute URL to relative")
- console.log(" * Absolute URL: " + absolute_url)
- console.log(" * Note URL: " + current_url)
-
- var current_path = current_url.split('/');
- var absolute_path = $scope.getUrlParts(absolute_url).pathname.split('/');
-
- current_path.pop();
- current_path.shift();
- absolute_path.shift();
-
- var current_path_count = 0;
- for (var i = 0; i < current_path.length; i++) {
- current_path_count = current_path_count + 1;
- }
-
- var absolute_path_count = 0;
- for (var i = 0; i < absolute_path.length; i++) {
- absolute_path_count = absolute_path_count + 1;
- }
- absolute_path_count = absolute_path_count - 1;
- console.log(" * Cleaned current URL (" + current_path_count + " parts): " + current_path.join('/'))
- console.log(" * Cleaned absolute URL (" + absolute_path_count + " parts): " + absolute_path.join('/'))
- dif = current_path_count - (absolute_path_count -1);
- for (var i = 0; i < absolute_path_count; i++) {
- absolute_path.shift();
- }
- console.log(" * Modified current URL (" + current_path_count + " parts): " + current_path.join('/'))
- console.log(" * Modified absolute URL (" + absolute_path_count + " parts): " + absolute_path.join('/'))
-
- var relative_path = absolute_path.join('/');
- console.log(" * Converted relative URL: " + relative_path)
- return relative_path;
- }
- $scope.getUrlParts = function(url) {
- var a = document.createElement('a');
- a.href = url;
- return {
- href: a.href,
- host: a.host,
- hostname: a.hostname,
- port: a.port,
- pathname: a.pathname,
- protocol: a.protocol,
- hash: a.hash,
- search: a.search
- };
- }
- }]);
|