15f8e2e09eb61dd7a1ceac8d5d78f5c1c83c5b02
[redakcja.git] / 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
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.Perspective.call(this, options);
52     };
53
54     SearchPerspective.prototype = new $.wiki.Perspective();
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             self.searchCursor = self.editor.getSearchCursor(
65                 self.$searchInput.val(), 
66                 self.options['search-from-cursor'], 
67                 !self.options['search-case-sensitive']
68             );
69         }
70         if (self.searchCursor.findNext()) {
71             self.searchCursor.select();
72             self.$replaceButton.removeAttr("disabled");
73             return true;
74         }
75         else {
76             self.searchCursor = null;
77             this.$replaceButton.attr("disabled","disabled");
78             return false;
79         }
80     };
81
82     SearchPerspective.prototype.replace = function(){
83         var self = this;
84         var query = self.$replaceInput.val();
85
86         if (!self.searchCursor) {
87             self.search();
88         }
89         else {}
90         self.searchCursor.select();
91         self.searchCursor.replace(query);
92         self.search();
93     };
94
95     SearchPerspective.prototype.onEnter = function(success, failure){
96         var self = this;
97
98         $.wiki.Perspective.prototype.onEnter.call(this);
99         self.$searchCursor = null;
100
101         $('.vsplitbar').not('.active').trigger('click');
102         $(".vsplitbar-title").html("↓ ZNAJDŹ I ZAMIEŃ ↓");        
103         
104         if ($.wiki.activePerspective() != 'CodeMirrorPerspective')
105             $.wiki.switchToTab('#CodeMirrorPerspective');
106     };
107
108     SearchPerspective.prototype.onExit = function(success, failure) {
109
110     };
111
112     $.wiki.SearchPerspective = SearchPerspective;
113
114 })(jQuery);