This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit.js
executable file
·116 lines (105 loc) · 4.27 KB
/
init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*jshint browser:true*/
/*
* Copyright (c) Codiad & Andr3as, distributed
* as-is and without warranty under the MIT License.
* See http://opensource.org/licenses/MIT for more information.
* This information must remain intact.
*/
(function(global, $){
var codiad = global.codiad,
scripts = document.getElementsByTagName('script'),
path = scripts[scripts.length-1].src.split('?')[0],
curpath = path.split('/').slice(0, -1).join('/')+'/';
$(function() {
codiad.MiniMap.init();
});
codiad.MiniMap = {
path : curpath,
worker : true,
changeNumber: null,
scrollNumber: null,
template : "",
init: function() {
var _this = this;
$.get(this.path+"template.html", function(data){
_this.template = data;
$('#editor-top-bar').before(data);
});
//Get worker
this.worker = new Worker(this.path+'worker.js');
this.worker.addEventListener('message', this.getWorkerResult.bind(this));
//Render canvas
amplify.subscribe("active.onFocus", function(path){
_this.updateMap();
});
//document on change listener
amplify.subscribe("active.onOpen", function(path){
var session = codiad.editor.getActive().getSession();
session.on('changeScrollTop', function(scrollTop){
//Changed scrolling
if (_this.scrollNumber === null) {
_this.scrollNumber = setTimeout(function(){
_this.scrollNumber = null;
_this.colorLines();
}, 50);
}
_this.colorLines();
});
session.on('change', function(e){
if (_this.changeNumber === null) {
_this.changeNumber = setTimeout(function(){
_this.changeNumber = null;
}, 2000);
//Update canvas
_this.updateMap();
}
});
});
//Reset Canvas
amplify.subscribe("active.onClose", function(path){
_this.resetMap();
});
amplify.subscribe("active.onRemoveAll", function(){
_this.resetMap();
});
//Click listener
$('.minimap pre').live('click', function(e){
var y = e.pageY;
var offset = $('.minimap pre').offset().top;
var height = $('.minimap pre').height();
var length = codiad.editor.getActive().getSession().getLength();
var line = Math.floor((y-offset) / (height / length));
codiad.editor.gotoLine(line);
_this.updateMap();
});
},
updateMap: function() {
var mode = $('#current-mode').text();
var code = codiad.editor.getContent();
$('.minimap .code').removeClass().addClass('code language-' + mode);
this.worker.postMessage({code: code, mode: mode});
},
getWorkerResult: function(e) {
$('.minimap .code').html(e.data.code);
this.colorLines();
},
colorLines: function() {
var first = codiad.editor.getActive().renderer.getFirstFullyVisibleRow() + 1;
var last = codiad.editor.getActive().renderer.getLastFullyVisibleRow() + 1;
var lines = last - first;
var height = $('.minimap pre').height();
var length = codiad.editor.getActive().getSession().getLength();
var size = height / length * lines;
var offset = height / length * first;
$('.minimap .background').css('height', size + "px");
$('.minimap .background').css('margin-top', offset + "px");
},
resetMap: function(){
$('.minimap').replaceWith(this.template);
$('.minimap .background').css('height', 0);
},
getExtension: function(path) {
return path.substring(path.lastIndexOf(".")+1);
}
};
})(this, jQuery);