/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
AUI.add('liferay-staging', function (A) {
  var StagingBar = {
    init: function init(config) {
      var instance = this;
      var namespace = config.namespace;
      instance.markAsReadyForPublicationURL = config.markAsReadyForPublicationURL;
      instance.layoutRevisionStatusURL = config.layoutRevisionStatusURL;
      instance._namespace = namespace;
      instance.viewHistoryURL = config.viewHistoryURL;
      Liferay.publish({
        fireOnce: true
      });
      Liferay.after('initStagingBar', function () {
        var body = A.getBody();

        if (body.hasClass('has-staging-bar')) {
          var stagingLevel3 = A.one('.staging-bar-level-3-message');
          body.addClass(stagingLevel3 === null ? 'staging-ready' : 'staging-ready-level-3');
        }
      });
      Liferay.fire('initStagingBar', config);
    }
  };
  Liferay.StagingBar = StagingBar;
}, '', {
  requires: ['aui-io-plugin-deprecated', 'aui-modal']
});
//# sourceMappingURL=staging.js.map
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
AUI.add('liferay-staging-branch', function (A) {
  var Lang = A.Lang;
  var StagingBar = Liferay.StagingBar;
  A.mix(StagingBar, {
    _getBranchDialog: function _getBranchDialog() {
      var instance = this;
      var branchDialog = instance._branchDialog;

      if (!branchDialog) {
        var namespace = instance._namespace;
        branchDialog = Liferay.Util.Window.getWindow({
          dialog: {
            bodyContent: A.one('#' + namespace + 'addBranch').show()
          },
          title: 'Branch'
        });
        instance._branchDialog = branchDialog;
      }

      return branchDialog;
    },
    addBranch: function addBranch(dialogTitle) {
      var instance = this;

      var branchDialog = instance._getBranchDialog();

      if (Lang.isValue(dialogTitle)) {
        branchDialog.set('title', dialogTitle);
      }

      branchDialog.show();
    }
  });
}, '', {
  requires: ['liferay-staging']
});
//# sourceMappingURL=staging_branch.js.map
/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */
AUI.add('liferay-staging-version', function (A) {
  var StagingBar = Liferay.StagingBar;
  var MAP_CMD_REVISION = {
    redo: 'redo_layout_revision',
    undo: 'undo_layout_revision'
  };
  var MAP_TEXT_REVISION = {
    redo: 'Are\x20you\x20sure\x20you\x20want\x20to\x20redo\x20your\x20last\x20changes\x3f',
    undo: 'Are\x20you\x20sure\x20you\x20want\x20to\x20undo\x20your\x20last\x20changes\x3f'
  };
  A.mix(StagingBar, {
    _cleanup: function _cleanup() {
      var instance = this;

      if (instance._eventHandles) {
        A.Array.invoke(instance._eventHandles, 'detach');
      }
    },
    _getNotification: function _getNotification() {
      var instance = this;
      var notification = instance._notification;

      if (!notification) {
        notification = new Liferay.Notice({
          closeText: false,
          content: 'There\x20was\x20an\x20unexpected\x20error\x2e\x20Please\x20refresh\x20the\x20current\x20page\x2e',
          noticeClass: 'hide',
          timeout: 10000,
          toggleText: false,
          type: 'warning',
          useAnimation: true
        });
        instance._notification = notification;
      }

      return notification;
    },
    _onInit: function _onInit() {
      var instance = this;

      instance._cleanup();

      var namespace = instance._namespace;
      var eventHandles = [Liferay.on(namespace + 'redo', instance._onRevisionChange, instance, 'redo'), Liferay.on(namespace + 'submit', instance._onSubmit, instance), Liferay.on(namespace + 'undo', instance._onRevisionChange, instance, 'undo'), Liferay.on(namespace + 'viewHistory', instance._onViewHistory, instance)];
      var layoutRevisionDetails = A.one('#' + namespace + 'layoutRevisionDetails');
      var layoutRevisionStatus = A.one('#' + namespace + 'layoutRevisionStatus');

      if (layoutRevisionDetails) {
        eventHandles.push(Liferay.after('updatedLayout', function () {
          Liferay.Util.fetch(instance.markAsReadyForPublicationURL).then(function (response) {
            return response.text();
          }).then(function (response) {
            layoutRevisionDetails.plug(A.Plugin.ParseContent);
            layoutRevisionDetails.setContent(response);
            Liferay.fire('updatedStatus');
          })["catch"](function () {
            layoutRevisionDetails.setContent('There\x20was\x20an\x20unexpected\x20error\x2e\x20Please\x20refresh\x20the\x20current\x20page\x2e');
          });
        }));
      }

      if (layoutRevisionStatus) {
        Liferay.after('updatedStatus', function () {
          Liferay.Util.fetch(instance.layoutRevisionStatusURL).then(function (response) {
            return response.text();
          }).then(function (response) {
            layoutRevisionStatus.plug(A.Plugin.ParseContent);
            layoutRevisionStatus.setContent(response);
          })["catch"](function () {
            layoutRevisionStatus.setContent('There\x20was\x20an\x20unexpected\x20error\x2e\x20Please\x20refresh\x20the\x20current\x20page\x2e');
          });
        });
      }

      instance._eventHandles = eventHandles;
    },
    _onRevisionChange: function _onRevisionChange(event, type) {
      var instance = this;
      var cmd = MAP_CMD_REVISION[type];
      var confirmText = MAP_TEXT_REVISION[type];

      if (confirm(confirmText)) {
        instance._updateRevision(cmd, event.layoutRevisionId, event.layoutSetBranchId);
      }
    },
    _onSubmit: function _onSubmit(event) {
      var instance = this;
      var namespace = instance._namespace;
      var layoutRevisionDetails = A.one('#' + namespace + 'layoutRevisionDetails');
      var layoutRevisionInfo = layoutRevisionDetails.one('.layout-revision-info');

      if (layoutRevisionInfo) {
        layoutRevisionInfo.addClass('loading');
      }

      var submitLink = A.one('#' + namespace + 'submitLink');

      if (submitLink) {
        submitLink.html('Loading' + '...');
      }

      Liferay.Util.fetch(event.publishURL).then(function () {
        if (event.incomplete) {
          location.href = event.currentURL;
        } else {
          Liferay.fire('updatedLayout');
        }
      })["catch"](function () {
        layoutRevisionDetails.addClass('alert alert-danger');
        layoutRevisionDetails.setContent('There\x20was\x20an\x20unexpected\x20error\x2e\x20Please\x20refresh\x20the\x20current\x20page\x2e');
      });
    },
    _onViewHistory: function _onViewHistory() {
      Liferay.Util.openWindow({
        dialog: {
          after: {
            destroy: function destroy() {
              window.location.reload();
            }
          },
          destroyOnHide: true
        },
        title: 'History',
        uri: StagingBar.viewHistoryURL
      });
    },
    _updateRevision: function _updateRevision(cmd, layoutRevisionId, layoutSetBranchId) {
      var instance = this;
      var updateLayoutData = {
        cmd: cmd,
        doAsUserId: themeDisplay.getDoAsUserIdEncoded(),
        layoutRevisionId: layoutRevisionId,
        layoutSetBranchId: layoutSetBranchId,
        p_auth: Liferay.authToken,
        p_l_id: themeDisplay.getPlid(),
        p_v_l_s_g_id: themeDisplay.getSiteGroupId()
      };
      Liferay.Util.fetch(themeDisplay.getPathMain() + '/portal/update_layout', {
        body: Liferay.Util.objectToFormData(updateLayoutData),
        method: 'POST'
      }).then(function () {
        window.location.reload();
      })["catch"](function () {
        instance._getNotification().show();
      });
    },
    destructor: function destructor() {
      var instance = this;

      instance._cleanup();
    }
  });
  Liferay.on('initStagingBar', StagingBar._onInit, StagingBar);
}, '', {
  requires: ['aui-button', 'liferay-staging']
});
//# sourceMappingURL=staging_version.js.map
