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