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 status page
      nf.History.init();
  });
  
  nf.History = (function () {
  
      /**
       * Configuration object used to hold a number of configuration items.
       */
      var config = {
          urls: {
              banners: '../nifi-api/flow/banners',
              about: '../nifi-api/flow/about',
              currentUser: '../nifi-api/flow/current-user',
              clusterSummary: '../nifi-api/flow/cluster/summary'
          }
      };
  
      /**
       * Loads the current user.
       */
      var loadCurrentUser = function () {
          return $.ajax({
              type: 'GET',
              url: config.urls.currentUser,
              dataType: 'json'
          }).done(function (currentUser) {
              nf.Common.setCurrentUser(currentUser);
          }).fail(nf.Common.handleAjaxError);
      };
  
      /**
       * Loads the flow configuration and updated the cluster state.
       *
       * @returns xhr
       */
      var loadClusterSummary = function () {
          return $.ajax({
              type: 'GET',
              url: config.urls.clusterSummary,
              dataType: 'json'
          }).done(function (response) {
              var clusterSummary = response.clusterSummary;
  
              // if clustered, show message to indicate location of actions
              if (clusterSummary.clustered === true) {
                  $('#cluster-history-message').show();
              }
          });
      };
  
      /**
       * Initializes the history page.
       */
      var initializeHistoryPage = function () {
          // define mouse over event for the refresh button
          $('#refresh-button').click(function () {
              nf.HistoryTable.loadHistoryTable();
          });
  
          // 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('history');
                          }
  
                          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('history');
                          }
                      }
  
                      deferred.resolve();
                  }).fail(function (xhr, status, error) {
                      nf.Common.handleAjaxError(xhr, status, error);
                      deferred.reject();
                  });
              } else {
                  deferred.resolve();
              }
          }).promise();
      };
  
      return {
          /**
           * Initializes the status page.
           */
          init: function () {
              nf.Storage.init();
              
              // load the current user
              loadCurrentUser().done(function () {
                  loadClusterSummary();
  
                  // create the history table
                  nf.HistoryTable.init();
  
                  // load the history table
                  nf.HistoryTable.loadHistoryTable();
  
                  // once the table is initialized, finish initializing the page
                  initializeHistoryPage().done(function () {
                      var setBodySize = function () {
                          //alter styles if we're not in the shell
                          if (top === window) {
                              $('body').css({
                                  'height': $(window).height() + 'px',
                                  'width': $(window).width() + 'px'
                              });
                              
                              $('#history').css('margin', 40);
                              $('#history-refresh-container').css('margin', 40);
                          }
  
                          // configure the initial grid height
                          nf.HistoryTable.resetTableSize();
                      };
  
                      // get the about details
                      $.ajax({
                          type: 'GET',
                          url: config.urls.about,
                          dataType: 'json'
                      }).done(function (response) {
                          var aboutDetails = response.about;
                          var historyTitle = aboutDetails.title + ' History';
  
                          // set the document title and the about title
                          document.title = historyTitle;
                          $('#history-header-text').text(historyTitle);
  
                          // set the initial size
                          setBodySize();
                      }).fail(nf.Common.handleAjaxError);
  
                      $(window).on('resize', function (e) {
                          setBodySize();
                          // resize dialogs when appropriate
                          var dialogs = $('.dialog');
                          for (var i = 0, len = dialogs.length; i < len; i++) {
                              if ($(dialogs[i]).is(':visible')){
                                  setTimeout(function(dialog){
                                      dialog.modal('resize');
                                  }, 50, $(dialogs[i]));
                              }
                          }
  
                          // resize grids when appropriate
                          var gridElements = $('*[class*="slickgrid_"]');
                          for (var j = 0, len = gridElements.length; j < len; j++) {
                              if ($(gridElements[j]).is(':visible')){
                                  setTimeout(function(gridElement){
                                      gridElement.data('gridInstance').resizeCanvas();
                                  }, 50, $(gridElements[j]));
                              }
                          }
  
                          // toggle tabs .scrollable when appropriate
                          var tabsContainers = $('.tab-container');
                          var tabsContents = [];
                          for (var k = 0, len = tabsContainers.length; k < len; k++) {
                              if ($(tabsContainers[k]).is(':visible')){
                                  tabsContents.push($('#' + $(tabsContainers[k]).attr('id') + '-content'));
                              }
                          }
                          $.each(tabsContents, function (index, tabsContent) {
                              nf.Common.toggleScrollable(tabsContent.get(0));
                          });
                      });
                  });
              });
          }
      };
  }());