
/**
 * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
 */

/**
 * @fileOverview Definition for placeholder plugin dialog.
 *
 */

'use strict';

CKEDITOR.dialog.add('placeholder', function (editor) {
    var lang = editor.lang.placeholder,
		generalLabel = editor.lang.common.generalTab,
		validNameRegex = /^[^\[\]<>]+$/;

    return {
        title: lang.title,
        minWidth: 300,
        minHeight: 80,
        onLoad: function () {
            var $select = $(this.parts.contents.find("select").$[0]);
            ajax({
                url: "Office/GetPlaceHolderList",
                success: function (res) {
                    $.each(res, function () {
                        $select.append("<option value=" + this.Value + ">" + this.Text + "</option>");
                    });
                }
            });
        },
        onOk: function () {
            var dialog = this;
            var span = editor.document.createElement("span");
            var body = editor.document.$.getElementsByTagName('body')[0];
            span.setText("{" + dialog.getValueOf('info', 'name') + "}");
            $(body).prepend($(span.$));
            $(span.$).addClass("cke_placeholder");
            $(span.$).attr("data-val", dialog.getValueOf("info", "name"));
            $(span.$).draggable({ containment: $(body), cursor: 'pointer' });
        },
        contents: [
			{
			    id: 'info',
			    label: generalLabel,
			    title: generalLabel,
			    elements: [
					// Dialog window UI elements.
					{
					    id: 'name',
					    type: 'select',
					    items: [],
					    style: 'width: 100%;',
					    label: lang.name,
					    'default': '',
					    required: true,
					    validate: CKEDITOR.dialog.validate.regex(validNameRegex, lang.invalidName),
					    setup: function (widget) {
					        this.setValue(widget.data.name);
					    },
					    commit: function (widget) {
					        debugger;
					        widget.setData('name', this.getValue());
					        var el = $(widget.element.$).parent();
					        el.draggable();
					    }
					}
			    ]
			}
        ]
    };
});
