You will find only what you bring in.
[redakcja.git] / src / redakcja / static / js / wiki / view_search.js
1 (function($){
2
3     /*
4      * Perspective
5      */
6     function SearchPerspective(options){
7         var old_callback = options.callback || function() { };
8
9         this.vsplitbar = 'ZNAJDŹ I ZAMIEŃ';
10
11         options.callback = function(){
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.$replaceButton = $('#replace-button', this.$element);
20
21             this.$replaceButton.attr("disabled","disabled");
22             this.options = Array();
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.$replaceButton.click(function(){
45                 self.replace();
46             });
47
48             old_callback.call(this);
49         };
50
51         $.wiki.SidebarPerspective.call(this, options);
52     };
53
54     SearchPerspective.prototype = new $.wiki.SidebarPerspective();
55
56     SearchPerspective.prototype.search = function(){
57         var self = this;
58         var query = self.$searchInput.val();
59
60         if (!self.editor)
61             self.editor = $.wiki.perspectiveForTab('#CodeMirrorPerspective').codemirror
62
63         if (!self.searchCursor) {
64             var options = {};
65             options.caseFold = !self.options['search-case-sensitive'];
66             var start = 0;
67             if (self.options['search-from-cursor']) {
68                 start = self.editor.getCursor();
69             }
70             self.searchCursor = self.editor.getSearchCursor(
71                 self.$searchInput.val(), 
72                 start,
73                 options
74             );
75         }
76         if (self.searchCursor.findNext()) {
77             self.editor.setSelection(self.searchCursor.from(), self.searchCursor.to());
78             self.editor.scrollIntoView({from: self.searchCursor.from(), to: self.searchCursor.to()}, 20);
79             self.$replaceButton.removeAttr("disabled");
80             return true;
81         }
82         else {
83             self.searchCursor = null;
84             this.$replaceButton.attr("disabled","disabled");
85             return false;
86         }
87     };
88
89     SearchPerspective.prototype.replace = function(){
90         var self = this;
91         var query = self.$replaceInput.val();
92
93         if (!self.searchCursor) {
94             self.search();
95         }
96         else {}
97
98         self.editor.setSelection(self.searchCursor.from(), self.searchCursor.to());
99         self.editor.scrollIntoView({from: self.searchCursor.from(), to: self.searchCursor.to()}, 20);
100
101         self.searchCursor.replace(query);
102         if(self.search() && self.options['replace-all']) {
103             self.replace();
104         }
105     };
106
107     SearchPerspective.prototype.onEnter = function(success, failure){
108         var self = this;
109
110         $.wiki.SidebarPerspective.prototype.onEnter.call(this);
111         self.$searchCursor = null;
112
113         if ($.wiki.activePerspective() != 'CodeMirrorPerspective')
114             $.wiki.switchToTab('#CodeMirrorPerspective');
115     };
116
117     SearchPerspective.prototype.onExit = function(success, failure) {
118
119     };
120
121     $.wiki.SearchPerspective = SearchPerspective;
122
123 })(jQuery);