'use strict';

/**
 * Licensed Materials - Property of IBM
 * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2014, 2018
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */
/**
 * Simple Dialog to Create a New Board
 */
define(['../../../lib/@waca/core-client/js/core-client/ui/dialogs/BaseDialog', 'jquery', 'underscore', '../../nls/StringResources'], function (BaseDialog, $, _, stringResources) {

	var Dialog = null;
	Dialog = BaseDialog.extend({

		/**
   * Rename dialog init function.
   * @param item object to rename
   * @param okCallback function that performs the rename
   */
		init: function init(item, okCallback) {
			Dialog.inherited('init', this, arguments);
			this._okCallback = okCallback;
			if (item) {
				this._currentName = _.unescape(item.name);
				this._type = item.type ? item.type : 'Board';
				this._allowEmpty = item.allowEmpty ? true : false;
			} else {
				this._type = 'Board';
				this._allowEmpty = false;
			}

			this.$input = $('<input>', {
				id: 'boardName',
				name: 'boardName',
				value: this._currentName,
				type: 'text',
				maxlength: '100',
				OnFocus: function OnFocus() {
					this.select();
				},
				on: { 'focus': function focus() {
						// 'this' is the jquery instance object, we don't want to bind(this)
						this.setSelectionRange(0, 9999);
					} }
			});
			this.$input.on('input', this.toggleOk.bind(this));
		},

		renderContent: function renderContent(n) {
			this.$form = $('<form>', {
				id: 'renameBoardForm',
				action: 'board',
				method: 'post',
				'class': 'boardActionDlgForm'
			});
			this.$form.append($('<label>', {
				'for': 'boardName',
				form: this.$form[0].id,
				text: stringResources.get('dlg_NewNameLabel')
			}));
			this.$form.append(this.$input);
			n.append(this.$form);
			n.addClass('boardActionDlgContent');

			return n;
		},

		open: function open() {
			Dialog.inherited('open', this, arguments);
			this.$input.focus();
		},

		renderTitle: function renderTitle(n) {
			var resName = 'dlg_rename';
			resName += this._type;
			resName += 'Title';
			n.text(stringResources.get(resName));
			return n;
		},

		ok: function ok() {
			var boardName = this.$input.val();
			if (boardName.trim().length !== 0 || this._allowEmpty) {
				Dialog.inherited('ok', this, arguments);
				if (boardName !== this._currentName) {
					this._okCallback(boardName);
				}
			}
		},

		toggleOk: function toggleOk() /* evt */{
			var boardName = this.$input.val().trim();
			if (boardName.length === 0 && !this._allowEmpty) {
				Dialog.inherited('disableOk', this, arguments);
			} else {
				Dialog.inherited('enableOk', this, arguments);
			}
		},

		//@overrides
		onKey: function onKey(evt) {
			var keyCode = evt.keyCode;
			// Prevent default auto submit on the input control
			if (keyCode === 13 && evt.target.id === this.$input[0].id) {
				return false;
			}

			return Dialog.inherited('onKey', this, arguments);
		}
	});

	return Dialog;
});
//# sourceMappingURL=RenameDialog.js.map