More data in catalogue
[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.$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         $.wiki.SidebarPerspective.call(this, options);
58     };
59
60     SearchPerspective.prototype = new $.wiki.SidebarPerspective();
61
62     SearchPerspective.prototype.search = function(forward=true){
63         var self = this;
64         var query = self.$searchInput.val();
65
66         if (!self.editor)
67             self.editor = $.wiki.perspectiveForTab('#CodeMirrorPerspective').codemirror
68
69         if (!self.searchCursor) {
70             var options = {};
71             options.caseFold = !self.options['search-case-sensitive'];
72             var start = 0;
73             if (self.options['search-from-cursor']) {
74                 start = self.editor.getCursor();
75             }
76             self.searchCursor = self.editor.getSearchCursor(
77                 self.$searchInput.val(), 
78                 start,
79                 options
80             );
81         }
82         if (forward ? self.searchCursor.findNext() : self.searchCursor.findPrevious()) {
83             self.editor.setSelection(self.searchCursor.from(), self.searchCursor.to());
84             self.editor.scrollIntoView({from: self.searchCursor.from(), to: self.searchCursor.to()}, 20);
85             self.$replaceButton.removeAttr("disabled");
86             return true;
87         }
88         else {
89             self.searchCursor = null;
90             this.$replaceButton.attr("disabled","disabled");
91             return false;
92         }
93     };
94
95     SearchPerspective.prototype.replace = function(){
96         var self = this;
97         var query = self.$replaceInput.val();
98
99         if (!self.searchCursor) {
100             self.search();
101         }
102         else {}
103
104         self.editor.setSelection(self.searchCursor.from(), self.searchCursor.to());
105         self.editor.scrollIntoView({from: self.searchCursor.from(), to: self.searchCursor.to()}, 20);
106
107         self.searchCursor.replace(query);
108         if(self.search() && self.options['replace-all']) {
109             self.replace();
110         }
111     };
112
113     SearchPerspective.prototype.onEnter = function(success, failure){
114         var self = this;
115
116         $.wiki.SidebarPerspective.prototype.onEnter.call(this);
117         self.$searchCursor = null;
118
119         if ($.wiki.activePerspective() != 'CodeMirrorPerspective')
120             $.wiki.switchToTab('#CodeMirrorPerspective');
121     };
122
123     SearchPerspective.prototype.onExit = function(success, failure) {
124
125     };
126
127     $.wiki.SearchPerspective = SearchPerspective;
128
129 })(jQuery);