b49671c68a45efb9490cc39eb903b0c32791764c
[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.noupdate_hash_onenter = true;
10         this.vsplitbar = 'ZNAJDŹ I ZAMIEŃ';
11
12         options.callback = function(){
13             var self = this;
14
15             this.editor = null;
16             this.$element = $("#side-search");
17             this.$searchInput = $('#search-input', this.$element);
18             this.$replaceInput = $('#replace-input', this.$element);
19             this.$searchButton = $('#search-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.$replaceButton.click(function(){
46                 self.replace();
47             });
48
49             old_callback.call(this);
50         };
51
52         $.wiki.Perspective.call(this, options);
53     };
54
55     SearchPerspective.prototype = new $.wiki.Perspective();
56
57     SearchPerspective.prototype.search = function(){
58         var self = this;
59         var query = self.$searchInput.val();
60
61         if (!self.editor)
62             self.editor = $.wiki.perspectiveForTab('#CodeMirrorPerspective').codemirror
63
64         if (!self.searchCursor) {
65             self.searchCursor = self.editor.getSearchCursor(
66                 self.$searchInput.val(), 
67                 self.options['search-from-cursor'], 
68                 !self.options['search-case-sensitive']
69             );
70         }
71         if (self.searchCursor.findNext()) {
72             self.searchCursor.select();
73             self.$replaceButton.removeAttr("disabled");
74             return true;
75         }
76         else {
77             self.searchCursor = null;
78             this.$replaceButton.attr("disabled","disabled");
79             return false;
80         }
81     };
82
83     SearchPerspective.prototype.replace = function(){
84         var self = this;
85         var query = self.$replaceInput.val();
86
87         if (!self.searchCursor) {
88             self.search();
89         }
90         else {}
91         self.searchCursor.select();
92         self.searchCursor.replace(query);
93         if(self.search() && self.options['replace-all']) {
94             self.replace();
95         }
96     };
97
98     SearchPerspective.prototype.onEnter = function(success, failure){
99         var self = this;
100
101         $.wiki.Perspective.prototype.onEnter.call(this);
102         self.$searchCursor = null;
103
104         $('.vsplitbar').not('.active').trigger('click');
105         $(".vsplitbar-title").html("↓ ZNAJDŹ I ZAMIEŃ ↓");        
106         
107         if ($.wiki.activePerspective() != 'CodeMirrorPerspective')
108             $.wiki.switchToTab('#CodeMirrorPerspective');
109     };
110
111     SearchPerspective.prototype.onExit = function(success, failure) {
112
113     };
114
115     $.wiki.SearchPerspective = SearchPerspective;
116
117 })(jQuery);