text: /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  /* global nf, top */
  
  $(document).ready(function () {
      // initialize the counters page
      nf.Counters.init();
  });
  
  nf.Counters = (function () {
  
      /**
       * Configuration object used to hold a number of configuration items.
       */
      var config = {
          urls: {
              banners: '../nifi-api/flow/banners',
              about: '../nifi-api/flow/about',
              authorities: '../nifi-api/flow/authorities'
          }
      };
  
      /**
       * Loads the current users authorities.
       */
      var loadAuthorities = function () {
          return $.Deferred(function (deferred) {
              $.ajax({
                  type: 'GET',
                  url: config.urls.authorities,
                  dataType: 'json'
              }).done(function (response) {
                  if (nf.Common.isDefinedAndNotNull(response.authorities)) {
                      // record the users authorities
                      nf.Common.setAuthorities(response.authorities);
                      deferred.resolve();
                  } else {
                      deferred.reject();
                  }
              }).fail(function (xhr, status, error) {
                  nf.Common.handleAjaxError(xhr, status, error);
                  deferred.reject();
              });
          }).promise();
      };
  
      /**
       * Initializes the counters page.
       */
      var initializeCountersPage = function () {
          // define mouse over event for the refresh button
          nf.Common.addHoverEffect('#refresh-button', 'button-refresh', 'button-refresh-hover').click(function () {
              nf.CountersTable.loadCountersTable();
          });
  
          // return a deferred for page initialization
          return $.Deferred(function (deferred) {
              // get the banners if we're not in the shell
              if (top === window) {
                  $.ajax({
                      type: 'GET',
                      url: config.urls.banners,
                      dataType: 'json'
                  }).done(function (response) {
                      // ensure the banners response is specified
                      if (nf.Common.isDefinedAndNotNull(response.banners)) {
                          if (nf.Common.isDefinedAndNotNull(response.banners.headerText) && response.banners.headerText !== '') {
                              // update the header text
                              var bannerHeader = $('#banner-header').text(response.banners.headerText).show();
  
                              // show the banner
                              var updateTop = function (elementId) {
                                  var element = $('#' + elementId);
                                  element.css('top', (parseInt(bannerHeader.css('height'), 10) + parseInt(element.css('top'), 10)) + 'px');
                              };
  
                              // update the position of elements affected by top banners
                              updateTop('counters');
                          }
  
                          if (nf.Common.isDefinedAndNotNull(response.banners.footerText) && response.banners.footerText !== '') {
                              // update the footer text and show it
                              var bannerFooter = $('#banner-footer').text(response.banners.footerText).show();
  
                              var updateBottom = function (elementId) {
                                  var element = $('#' + elementId);
                                  element.css('bottom', parseInt(bannerFooter.css('height'), 10) + 'px');
                              };
  
                              // update the position of elements affected by bottom banners
                              updateBottom('counters');
                          }
                      }
  
                      deferred.resolve();
                  }).fail(function (xhr, status, error) {
                      nf.Common.handleAjaxError(xhr, status, error);
                      deferred.reject();
                  });
              } else {
                  deferred.resolve();
              }
          }).promise();
      };
  
      return {
          /**
           * Initializes the counters page.
           */
          init: function () {
              nf.Storage.init();
              
              // load the users authorities
              loadAuthorities().done(function () {
                  // create the counters table
                  nf.CountersTable.init();
  
                  // load the table
                  nf.CountersTable.loadCountersTable().done(function () {
                      // once the table is initialized, finish initializing the page
                      initializeCountersPage().done(function () {
                          // configure the initial grid height
                          nf.CountersTable.resetTableSize();
  
                          // get the about details
                          $.ajax({
                              type: 'GET',
                              url: config.urls.about,
                              dataType: 'json'
                          }).done(function (response) {
                              var aboutDetails = response.about;
                              var countersTitle = aboutDetails.title + ' Counters';
  
                              // set the document title and the about title
                              document.title = countersTitle;
                              $('#counters-header-text').text(countersTitle);
                          }).fail(nf.Common.handleAjaxError);
                      });
                  });
              });
          }
      };
  }());