Some modernizations.
[redakcja.git] / src / redakcja / static / js / wiki / view_search.js
1 (function($){
2
3     /*
4      * Perspective
5      */
6     class SearchPerspective extends $.wiki.SidebarPerspective {
7         constructor(options) {
8             var old_callback = options.callback || function() { };
9
10             options.callback = function(){
11                 var self = this;
12
13                 this.vsplitbar = 'ZNAJDŹ I ZAMIEŃ';
14                 this.editor = null;
15                 this.$element = $("#side-search");
16                 this.$searchInput = $('#search-input', this.$element);
17                 this.$replaceInput = $('#replace-input', this.$element);
18                 this.$searchButton = $('#search-button', this.$element);
19                 this.$searchPrevButton = $('#search-prev-button', this.$element);
20                 this.$replaceButton = $('#replace-button', this.$element);
21
22                 this.$replaceButton.attr("disabled","disabled");
23                 this.options = Array();
24
25                 // handlers
26                 this.$searchInput.change(function(event){
27                     self.searchCursor = null;
28                 });
29                 this.$replaceInput.change(function(event){
30                     self.searchCursor = null;
31                 });
32
33                 $("#side-search input:checkbox").each(function() {
34                     self.options[this.id] = this.checked;
35                 }).change(function(){
36                     self.options[this.id] = this.checked;
37                     self.searchCursor = null;
38                 });
39
40                 this.$searchButton.click(function(){
41                     if (!self.search())
42                         alert('Brak wyników.');
43                 });
44
45                 this.$searchPrevButton.click(function(){
46                     if (!self.search(false))
47                         alert('Brak wyników.');
48                 });
49
50                 this.$replaceButton.click(function(){
51                     self.replace();
52                 });
53
54                 old_callback.call(this);
55             };
56
57             super(options);
58         }
59
60         search(forward=true) {
61             var self = this;
62             var query = self.$searchInput.val();
63
64             if (!self.editor)
65                 self.editor = $.wiki.perspectiveForTab('#CodeMirrorPerspective').codemirror
66
67             if (!self.searchCursor) {
68                 var options = {};
69                 options.caseFold = !self.options['search-case-sensitive'];
70                 var start = 0;
71                 if (self.options['search-from-cursor']) {
72                     start = self.editor.getCursor();
73                 }
74                 self.searchCursor = self.editor.getSearchCursor(
75                     self.$searchInput.val(),
76                     start,
77                     options
78                 );
79             }
80             if (forward ? self.searchCursor.findNext() : self.searchCursor.findPrevious()) {
81                 self.editor.setSelection(self.searchCursor.from(), self.searchCursor.to());
82                 self.editor.scrollIntoView({from: self.searchCursor.from(), to: self.searchCursor.to()}, 20);
83                 self.$replaceButton.removeAttr("disabled");
84                 return true;
85             }
86             else {
87                 self.searchCursor = null;
88                 this.$replaceButton.attr("disabled","disabled");
89                 return false;
90             }
91         }
92
93         replace() {
94             var self = this;
95             var query = self.$replaceInput.val();
96
97             if (!self.searchCursor) {
98                 self.search();
99             }
100             else {}
101
102             self.editor.setSelection(self.searchCursor.from(), self.searchCursor.to());
103             self.editor.scrollIntoView({from: self.searchCursor.from(), to: self.searchCursor.to()}, 20);
104
105             self.searchCursor.replace(query);
106             if(self.search() && self.options['replace-all']) {
107                 self.replace();
108             }
109         }
110
111         onEnter(success, failure) {
112             var self = this;
113
114             super.onEnter();
115             self.$searchCursor = null;
116
117             if ($.wiki.activePerspective() != 'CodeMirrorPerspective')
118                 $.wiki.switchToTab('#CodeMirrorPerspective');
119         }
120
121         onExit(success, failure) {
122         }
123     }
124
125     $.wiki.SearchPerspective = SearchPerspective;
126
127 })(jQuery);