You will find only what you bring in.
[redakcja.git] / src / wiki / static / wiki / js / themes.js
1 (function($) {
2
3     $.themes = {};
4
5     // Wykonuje block z zaƂadowanymi kanonicznymi motywami
6     $.themes.withCanon = function(code_block, onError) {
7         if (typeof $.themes.canon == 'undefined') {
8             $.ajax({
9                 url: '/editor/themes',
10                 dataType: 'text',
11                 success: function(data) {
12                     $.themes.canon = data.split('\n');
13                     code_block($.themes.canon);
14                 },
15                 error: function() {
16                     $.themes.canon = null;
17                     code_block($.themes.canon);
18                 }
19             })
20         }
21         else {
22             code_block($.themes.canon);
23         }
24     };
25
26     function split( val ) {
27         return val.split( /,\s*/ );
28     }
29     function extractLast( term ) {
30         return split( term ).pop();
31     }
32  
33     $.themes.autocomplete = function(elem) {
34         elem.autocomplete({
35             source: function(request, response) {
36                 var query = extractLast(request.term).toLowerCase();
37                 $.themes.withCanon(function(canonThemes) {
38                     var candidates = [];
39                     $.each(canonThemes, function(i, theme) {
40                         if (theme.toLowerCase().startsWith(query)) {
41                             candidates.push(theme);
42                         }
43                         if (candidates.length == 10) {
44                             return false;
45                         }
46                     });
47                     response(candidates);
48                 });
49             },
50             search: function() {
51                 var term = extractLast( this.value );
52                 if ( term.length < 1 ) {
53                     return false;
54                 }
55             },
56             focus: function() {
57                 // prevent value inserted on focus
58                 return false;
59             },
60             select: function( event, ui ) {
61                 var terms = split( this.value );
62                 terms.pop();
63                 terms.push( ui.item.value );
64                 terms.push( "" );
65                 this.value = terms.join( ", " );
66                 return false;
67             },
68             appendTo: elem.parent()
69         });
70     };
71
72     
73 })(jQuery);