source: vlo/branches/vlo-3.3-oeaw/vlo-solr/src/main/webapp/js/scripts/cores.js @ 6753

Last change on this file since 6753 was 6753, checked in by davor.ostojic@oeaw.ac.at, 9 years ago

merged with trunk

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