source: vlo/branches/vlo-3.0/vlo-solr/src/main/webapp/js/scripts/cores.js @ 4734

Last change on this file since 4734 was 4734, checked in by teckart@informatik.uni-leipzig.de, 10 years ago

Added files for Solr admin interface (directories: css, img, js, tpl + admin.html), updated web.xml

File size: 22.1 KB
Line 
1/*
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements.  See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License.  You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16*/
17
18sammy.bind
19(
20  'cores_load_data',
21  function( event, params )
22  {
23    $.ajax
24    (
25      {
26        url : app.config.solr_path + app.config.core_admin_path + '?wt=json',
27        dataType : 'json',
28        beforeSend : function( xhr, settings )
29        {
30        },
31        success : function( response, text_status, xhr )
32        {
33          var has_cores = false;
34          for( core in response.status )
35          {
36            has_cores = true; break;
37          }
38
39          app.set_cores_data( response );
40         
41          if( has_cores )
42          {
43            params.success( app.cores_data );
44          }
45          else
46          {
47            params.error();
48          }
49        },
50        error : function( xhr, text_status, error_thrown)
51        {
52        },
53        complete : function( xhr, text_status )
54        {
55        }
56      }
57    );
58  }
59);
60
61sammy.bind
62(
63  'cores_build_navigation',
64  function( event, params )
65  {
66    var navigation_content = ['<ul>'];
67
68    for( var core in params.cores )
69    {
70      var core_name = core;
71      if( !core_name )
72      {
73        core_name = '<em>(empty)</em>';
74      }
75      navigation_content.push( '<li><a href="' + params.basepath + core + '">' + core_name + '</a></li>' );
76    }
77
78    params.navigation_element
79      .html( navigation_content.join( "\n" ) );
80       
81    $( 'a[href="' + params.basepath + params.current_core + '"]', params.navigation_element ).parent()
82      .addClass( 'current' );
83  }
84);
85
86sammy.bind
87(
88  'cores_load_template',
89  function( event, params )
90  {
91    if( app.cores_template )
92    {
93      params.callback();
94      return true;
95    }
96
97    $.get
98    (
99      'tpl/cores.html',
100      function( template )
101      {
102        params.content_element
103          .html( template );
104             
105        app.cores_template = template;   
106        params.callback();
107      }
108    );
109  }
110);
111
112// #/~cores
113sammy.get
114(
115  /^#\/(~cores)$/,
116  function( context )
117  {
118    delete app.cores_template;
119    var content_element = $( '#content' );
120
121    sammy.trigger
122    (
123      'cores_load_data',
124      {
125        success : function( cores )
126        {
127          var first_core = null;
128          for( var key in cores )
129          {
130            if( !first_core )
131            {
132              first_core = key;
133            }
134            continue;
135          }
136          context.redirect( context.path + '/' + first_core );
137        },
138        error : function()
139        {
140          sammy.trigger
141          (
142            'cores_load_template',
143            {
144              content_element : content_element,
145              callback : function()
146              {
147                var cores_element = $( '#cores', content_element );
148                var navigation_element = $( '#navigation', cores_element );
149                var data_element = $( '#data', cores_element );
150                var core_data_element = $( '#core-data', data_element );
151                var index_data_element = $( '#index-data', data_element );
152
153                // layout
154
155                var ui_block = $( '#ui-block' );
156                var actions_element = $( '.actions', cores_element );
157                var div_action = $( 'div.action', actions_element );
158
159                ui_block
160                  .css( 'opacity', 0.7 )
161                  .width( cores_element.width() + 10 )
162                  .height( cores_element.height() );
163
164                if( $( '#cloud.global' ).is( ':visible' ) )
165                {
166                  $( '.cloud', div_action )
167                    .show();
168                }
169
170                $( 'button.action', actions_element )
171                  .die( 'click' )
172                  .live
173                  (
174                    'click',
175                    function( event )
176                    {
177                      var self = $( this );
178
179                      self
180                        .toggleClass( 'open' );
181
182                      $( '.action.' + self.attr( 'id' ), actions_element )
183                        .trigger( 'open' );
184
185                      return false;
186                    }
187                  );
188
189                div_action
190                  .die( 'close' )
191                  .live
192                  (
193                    'close',
194                    function( event )
195                    {
196                      div_action.hide();
197                      ui_block.hide();
198                    }
199                  )
200                  .die( 'open' )
201                  .live
202                  (
203                    'open',
204                    function( event )
205                    {
206                      var self = $( this );
207                      var rel = $( '#' + self.data( 'rel' ) );
208
209                      self
210                        .trigger( 'close' )
211                        .show()
212                        .css( 'left', rel.position().left );
213                     
214                      ui_block
215                        .show();
216                    }
217                  );
218
219                $( 'form button.reset', actions_element )
220                  .die( 'click' )
221                  .live
222                  (
223                    'click',
224                    function( event )
225                    {
226                      $( this ).closest( 'div.action' )
227                        .trigger( 'close' );
228                    }
229                  );
230
231                $( 'form', div_action )
232                  .ajaxForm
233                  (
234                    {
235                      url : app.config.solr_path + app.config.core_admin_path + '?wt=json&indexInfo=false',
236                      dataType : 'json',
237                      beforeSubmit : function( array, form, options )
238                      {
239                        $( 'button[type="submit"] span', form )
240                          .addClass( 'loader' );
241                      },
242                      success : function( response, status_text, xhr, form )
243                      {
244                        delete app.cores_data;
245                        sammy.refresh();
246
247                        $( 'button.reset', form )
248                          .trigger( 'click' );
249                      },
250                      error : function( xhr, text_status, error_thrown )
251                      {
252                        var response = null;
253                        eval( 'response = ' + xhr.responseText + ';' );
254
255                        var error_elem = $( '.error', div_action.filter( ':visible' ) );
256                        error_elem.show();
257                        $( 'span', error_elem ).text( response.error.msg );
258                      },
259                      complete : function()
260                      {
261                        $( 'button span.loader', actions_element )
262                          .removeClass( 'loader' );
263                      }
264                    }
265                  );
266
267                // --
268
269                $( '#add', content_element )
270                  .trigger( 'click' );
271
272                $( '[data-rel="add"] input[type="text"]:first', content_element )
273                  .focus();
274              }
275            }
276          );
277        }
278      }
279    );
280  }
281);
282
283// #/~cores
284sammy.get
285(
286  /^#\/(~cores)\//,
287  function( context )
288  {
289    var content_element = $( '#content' );
290
291    var path_parts = this.path.match( /^(.+\/~cores\/)(.*)$/ );
292    var current_core = path_parts[2];
293
294    sammy.trigger
295    (
296      'cores_load_data',
297      {
298        error : function()
299        {
300          context.redirect( '#/' + context.params.splat[0] );
301        },
302        success : function( cores )
303        {
304          sammy.trigger
305          (
306            'cores_load_template',
307            {
308              content_element : content_element,
309              callback : function()
310              {
311                var cores_element = $( '#cores', content_element );
312                var navigation_element = $( '#navigation', cores_element );
313                var data_element = $( '#data', cores_element );
314                var core_data_element = $( '#core-data', data_element );
315                var index_data_element = $( '#index-data', data_element );
316
317                cores_element
318                  .removeClass( 'empty' );
319
320                sammy.trigger
321                (
322                  'cores_build_navigation',
323                  {
324                    cores : cores,
325                    basepath : path_parts[1],
326                    current_core : current_core,
327                    navigation_element : navigation_element
328                  }
329                );
330
331                var core_data = cores[current_core];
332                var core_basepath = $( '#' + current_core, app.menu_element ).attr( 'data-basepath' );
333
334                // core-data
335
336                $( '.startTime dd', core_data_element )
337                  .html( core_data.startTime );
338
339                $( '.instanceDir dd', core_data_element )
340                  .html( core_data.instanceDir );
341
342                $( '.dataDir dd', core_data_element )
343                  .html( core_data.dataDir );
344
345                // index-data
346
347                $( '.lastModified dd', index_data_element )
348                  .html( core_data.index.lastModified || '-' );
349
350                $( '.version dd', index_data_element )
351                  .html( core_data.index.version );
352
353                $( '.numDocs dd', index_data_element )
354                  .html( core_data.index.numDocs );
355
356                $( '.maxDoc dd', index_data_element )
357                  .html( core_data.index.maxDoc );
358               
359                $( '.deletedDocs dd', index_data_element )
360                  .html( core_data.index.deletedDocs || '-' );
361
362                $( '.optimized dd', index_data_element )
363                  .addClass( !core_data.index.hasDeletions ? 'ico-1' : 'ico-0' );
364
365                $( '#actions #optimize', cores_element )
366                  .show();
367
368                $( '.optimized dd span', index_data_element )
369                  .html( !core_data.index.hasDeletions ? 'yes' : 'no' );
370
371                $( '.current dd', index_data_element )
372                  .addClass( core_data.index.current ? 'ico-1' : 'ico-0' );
373
374                $( '.current dd span', index_data_element )
375                  .html( core_data.index.current ? 'yes' : 'no' );
376
377                $( '.directory dd', index_data_element )
378                  .html
379                  (
380                    core_data.index.directory
381                      .replace( /:/g, ':&#8203;' )
382                      .replace( /@/g, '@&#8203;' )
383                  );
384
385                var core_names = [];
386                var core_selects = $( '#actions select', cores_element );
387
388                for( var key in cores )
389                {
390                  core_names.push( '<option value="' + key + '">' + key + '</option>' )
391                }
392
393                core_selects
394                  .html( core_names.join( "\n") );
395
396                $( 'option[value="' + current_core + '"]', core_selects.filter( '.other' ) )
397                  .remove();
398               
399                $( 'input[data-core="current"]', cores_element )
400                  .val( current_core );
401
402                // layout
403
404                var ui_block = $( '#ui-block' );
405                var actions_element = $( '.actions', cores_element );
406                var div_action = $( 'div.action', actions_element );
407
408                ui_block
409                  .css( 'opacity', 0.7 )
410                  .width( cores_element.width() + 10 )
411                  .height( cores_element.height() );
412
413                if( $( '#cloud.global' ).is( ':visible' ) )
414                {
415                  $( '.cloud', div_action )
416                    .show();
417                }
418
419                $( 'button.action', actions_element )
420                  .die( 'click' )
421                  .live
422                  (
423                    'click',
424                    function( event )
425                    {
426                      var self = $( this );
427
428                      self
429                        .toggleClass( 'open' );
430
431                      $( '.action.' + self.attr( 'id' ), actions_element )
432                        .trigger( 'open' );
433
434                      return false;
435                    }
436                  );
437
438                div_action
439                  .die( 'close' )
440                  .live
441                  (
442                    'close',
443                    function( event )
444                    {
445                      div_action.hide();
446                      ui_block.hide();
447                    }
448                  )
449                  .die( 'open' )
450                  .live
451                  (
452                    'open',
453                    function( event )
454                    {
455                      var self = $( this );
456                      var rel = $( '#' + self.data( 'rel' ) );
457
458                      self
459                        .trigger( 'close' )
460                        .show()
461                        .css( 'left', rel.position().left );
462                     
463                      ui_block
464                        .show();
465                    }
466                  );
467
468                $( 'form button.reset', actions_element )
469                  .die( 'click' )
470                  .live
471                  (
472                    'click',
473                    function( event )
474                    {
475                      $( this ).closest( 'div.action' )
476                        .trigger( 'close' );
477                    }
478                  );
479
480                var form_callback = {
481
482                  rename : function( form, response )
483                  {
484                    var url = path_parts[1] + $( 'input[name="other"]', form ).val();
485                    context.redirect( url );
486                  }
487
488                };
489
490                $( 'form', div_action )
491                  .ajaxForm
492                  (
493                    {
494                      url : app.config.solr_path + app.config.core_admin_path + '?wt=json&indexInfo=false',
495                      dataType : 'json',
496                      beforeSubmit : function( array, form, options )
497                      {
498                        $( 'button[type="submit"] span', form )
499                          .addClass( 'loader' );
500                      },
501                      success : function( response, status_text, xhr, form )
502                      {
503                        var action = $( 'input[name="action"]', form ).val().toLowerCase();
504
505                        delete app.cores_data;
506
507                        if( form_callback[action] )
508                        {
509                         form_callback[action]( form, response ); 
510                        }
511                        else
512                        {
513                          sammy.refresh();
514                        }
515
516                        $( 'button.reset', form )
517                          .trigger( 'click' );
518                      },
519                      error : function( xhr, text_status, error_thrown )
520                      {
521                        var response = null;
522                        eval( 'response = ' + xhr.responseText + ';' );
523
524                        var error_elem = $( '.error', div_action.filter( ':visible' ) );
525                        error_elem.show();
526                        $( 'span', error_elem ).text( response.error.msg );
527                      },
528                      complete : function()
529                      {
530                        $( 'button span.loader', actions_element )
531                          .removeClass( 'loader' );
532                      }
533                    }
534                  );
535
536                var reload_button = $( '#actions #reload', cores_element );
537                reload_button
538                  .die( 'click' )
539                  .live
540                  (
541                    'click',
542                    function( event )
543                    {
544                      $.ajax
545                      (
546                        {
547                          url : app.config.solr_path + app.config.core_admin_path + '?wt=json&action=RELOAD&core=' + current_core,
548                          dataType : 'json',
549                          context : $( this ),
550                          beforeSend : function( xhr, settings )
551                          {
552                            $( 'span', this )
553                              .addClass( 'loader' );
554                          },
555                          success : function( response, text_status, xhr )
556                          {
557                            this
558                              .addClass( 'success' );
559
560                            delete app.cores_data;
561                            sammy.refresh();
562
563                            window.setTimeout
564                            (
565                              function()
566                              {
567                                reload_button
568                                  .removeClass( 'success' );
569                              },
570                              1000
571                            );
572                          },
573                          error : function( xhr, text_status, error_thrown )
574                          {
575                          },
576                          complete : function( xhr, text_status )
577                          {
578                            $( 'span', this )
579                              .removeClass( 'loader' );
580                          }
581                        }
582                      );
583                    }
584                  );
585                               
586                $( '#actions #unload', cores_element )
587                  .die( 'click' )
588                  .live
589                  (
590                    'click',
591                    function( event )
592                    {
593                      var ret = confirm( 'Do you really want to unload Core "' + current_core + '"?' );
594                      if( !ret )
595                      {
596                        return false;
597                      }
598
599                      $.ajax
600                      (
601                        {
602                          url : app.config.solr_path + app.config.core_admin_path + '?wt=json&action=UNLOAD&core=' + current_core,
603                          dataType : 'json',
604                          context : $( this ),
605                          beforeSend : function( xhr, settings )
606                          {
607                            $( 'span', this )
608                              .addClass( 'loader' );
609                          },
610                          success : function( response, text_status, xhr )
611                          {
612                            delete app.cores_data;
613                            context.redirect( path_parts[1].substr( 0, path_parts[1].length - 1 ) );
614                          },
615                          error : function( xhr, text_status, error_thrown )
616                          {
617                          },
618                          complete : function( xhr, text_status )
619                          {
620                            $( 'span', this )
621                              .removeClass( 'loader' );
622                          }
623                        }
624                      );
625                    }
626                  );
627
628                var optimize_button = $( '#actions #optimize', cores_element );
629                optimize_button
630                  .die( 'click' )
631                  .live
632                  (
633                    'click',
634                    function( event )
635                    {
636                      $.ajax
637                      (
638                        {
639                          url : core_basepath + '/update?optimize=true&waitFlush=true&wt=json',
640                          dataType : 'json',
641                          context : $( this ),
642                          beforeSend : function( xhr, settings )
643                          {
644                            $( 'span', this )
645                              .addClass( 'loader' );
646                          },
647                          success : function( response, text_status, xhr )
648                          {
649                            this
650                              .addClass( 'success' );
651
652                            window.setTimeout
653                            (
654                              function()
655                              {
656                                optimize_button
657                                  .removeClass( 'success' );
658                              },
659                              1000
660                            );
661                                                       
662                            $( '.optimized dd.ico-0', index_data_element )
663                              .removeClass( 'ico-0' )
664                              .addClass( 'ico-1' );
665                          },
666                          error : function( xhr, text_status, error_thrown)
667                          {
668                            console.warn( 'd0h, optimize broken!' );
669                          },
670                          complete : function( xhr, text_status )
671                          {
672                            $( 'span', this )
673                              .removeClass( 'loader' );
674                          }
675                        }
676                      );
677                    }
678                  );
679
680                $( '.timeago', data_element )
681                  .timeago();
682
683                $( 'ul', data_element )
684                  .each
685                  (
686                    function( i, element )
687                    {
688                      $( 'li:odd', element )
689                        .addClass( 'odd' );
690                    }
691                  )
692              }
693            }
694          );
695        }
696      }
697    );
698  }
699);
Note: See TracBrowser for help on using the repository browser.