Merge branch 'master' of git@github.com:fnp/redakcja
[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-regexp'],
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         self.search();
94     };
95
96     SearchPerspective.prototype.onEnter = function(success, failure){
97         var self = this;
98
99         $.wiki.Perspective.prototype.onEnter.call(this);
100         self.$searchCursor = null;
101
102         $('.vsplitbar').not('.active').trigger('click');
103         if ($.wiki.activePerspective() != 'CodeMirrorPerspective')
104             $.wiki.switchToTab('#CodeMirrorPerspective');
105     };
106
107     SearchPerspective.prototype.onExit = function(success, failure) {
108
109     };
110
111     $.wiki.SearchPerspective = SearchPerspective;
112
113 })(jQuery);