|
1 | 1 | /* jshint loopfunc: true */ |
2 | 2 | /* |
3 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * Licensed under the Apache License, Version 2.0 (the 'License'); |
4 | 4 | * you may not use this file except in compliance with the License. |
5 | 5 | * You may obtain a copy of the License at |
6 | 6 | * |
7 | 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | 8 | * |
9 | 9 | * Unless required by applicable law or agreed to in writing, software |
10 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * distributed under the License is distributed on an 'AS IS' BASIS, |
11 | 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | * See the License for the specific language governing permissions and |
13 | 13 | * limitations under the License. |
14 | 14 | */ |
15 | 15 | 'use strict'; |
16 | 16 |
|
17 | | -angular.module('zeppelinWebApp').controller('SearchResultCtrl', function($scope, $routeParams, searchService) { |
| 17 | +angular |
| 18 | + .module('zeppelinWebApp') |
| 19 | + .controller('SearchResultCtrl', function($scope, $routeParams, searchService) { |
18 | 20 |
|
19 | 21 | var results = searchService.search({'q': $routeParams.searchTerm}).query(); |
20 | 22 |
|
21 | | - console.log("Found: %o", results); |
| 23 | + console.log('Found: %o', results); |
22 | 24 | results.$promise.then(function(result) { |
23 | 25 | $scope.notes = result.body; |
24 | 26 | }); |
25 | | - console.log("Found body: %o", $scope.notes); |
| 27 | + console.log('Found body: %o', $scope.notes); |
26 | 28 |
|
27 | 29 | $scope.page = 0; |
28 | | - $scope.allResults = false; |
| 30 | + $scope.allResults = false; |
| 31 | + |
| 32 | + |
| 33 | + $scope.highlightSearchResults = function(note) { |
| 34 | + return function(_editor) { |
| 35 | + function getEditorMode(text) { |
| 36 | + var editorModes = { |
| 37 | + 'ace/mode/scala': /^%spark/, |
| 38 | + 'ace/mode/sql': /^%(\w*\.)?\wql/, |
| 39 | + 'ace/mode/markdown': /^%md/, |
| 40 | + 'ace/mode/sh': /^%sh/ |
| 41 | + } |
| 42 | + |
| 43 | + return Object.keys(editorModes).reduce(function(res, mode) { |
| 44 | + return editorModes[mode].test(text)? mode : res |
| 45 | + }, 'ace/mode/scala') |
| 46 | + } |
| 47 | + |
| 48 | + var Range = ace.require('ace/range').Range; |
| 49 | + |
| 50 | + _editor.setOption('highlightActiveLine', false); |
| 51 | + _editor.$blockScrolling = Infinity; |
| 52 | + _editor.setReadOnly(true); |
| 53 | + _editor.renderer.setShowGutter(false); |
| 54 | + _editor.setTheme('ace/theme/chrome'); |
| 55 | + _editor.getSession().setMode(getEditorMode(note.text)); |
| 56 | + |
| 57 | + function getIndeces(term) { |
| 58 | + return function(str) { |
| 59 | + var indeces = []; |
| 60 | + var i = -1; |
| 61 | + while((i = str.indexOf(term, i + 1)) >= 0) { |
| 62 | + indeces.push(i); |
| 63 | + } |
| 64 | + return indeces; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + var lines = note.fragment |
| 69 | + .split('\n') |
| 70 | + .map(function(line, row) { |
| 71 | + var match = line.match(/<B>(.+?)<\/B>/) |
| 72 | + |
| 73 | + // return early if nothing to highlight |
| 74 | + if (!match) { |
| 75 | + return line |
| 76 | + } |
| 77 | + |
| 78 | + var term = match[1] |
| 79 | + var __line = line |
| 80 | + .replace(/<B>/g, '') |
| 81 | + .replace(/<\/B>/g, '') |
| 82 | + |
| 83 | + var indeces = getIndeces(term)(__line) |
| 84 | + |
| 85 | + indeces.forEach(function(start) { |
| 86 | + var end = start + term.length |
| 87 | + _editor |
| 88 | + .getSession() |
| 89 | + .addMarker( |
| 90 | + new Range(row, start, row, end), |
| 91 | + 'search-results-highlight', |
| 92 | + 'line' |
| 93 | + ); |
| 94 | + }) |
| 95 | + |
| 96 | + return __line |
| 97 | + }) |
| 98 | + |
| 99 | + // resize editor based on content length |
| 100 | + _editor.setOption( |
| 101 | + 'maxLines', |
| 102 | + lines.reduce(function(len, line) {return len + line.length}, 0) |
| 103 | + ) |
| 104 | + |
| 105 | + _editor.getSession().setValue(lines.join('\n')) |
| 106 | + |
| 107 | + } |
| 108 | + } |
| 109 | + |
29 | 110 | }); |
0 commit comments