﻿Type.registerNamespace('SimplePageComponent');
// RibbonApp Page Component
SimplePageComponent.PageComponent = function (data) {
    SimplePageComponent.PageComponent.initializeBase(this);

    if (typeof (data) == 'string')
        data = eval(data);

    this.commands = new Object();
    for (i in data) {
        this.commands[data[i].Command] = data[i];
    }
}

SimplePageComponent.PageComponent.prototype = {
    commands: null,

    init: function () {
    },
    getGlobalCommands: function () {
        var v = [];
        for (i in this.commands) {
            v.push(this.commands[i].Command)
        }
        return v;
    },
    handleCommand: function (commandId, properties, sequence) {
        if (this.commands[commandId] && this.canHandleCommand(commandId)) {
            var command = this.commands[commandId];
            return this.handleCommandInternal(command, commandId, properties);
        }
        return false;
    },
    // 2 Functions below are close to the ones used by CommandUIExtension Page Component
    handleCommandInternal: function (command, $p1, $p2) {
        var $v_0 = command.ClickScript;
        if (command.ElementClientId) {
            var btn = document.getElementById(command.ElementClientId);
            if (btn && btn.onclick) {
                var ret = true;
                if ($v_0) {
                    var strFunc = '(function func() {' + $v_0 + '}) ();';
                    strFunc = this.replaceTokens(strFunc);
                    ret = eval(strFunc);
                }
                if (ret != false)
                    btn.onclick();
                return true;
            }
        } else if ($v_0) {
            if ($v_0.toLowerCase().startsWith('javascript:')) {
                eval(this.replaceTokens($v_0, true));
            } else {
                STSNavigate(this.replaceTokens($v_0, true));
            }
            return true;
        }
        return false;
    },
    replaceTokens: function ($p0, $p1) {
        var $v_0 = null;
        var $v_1 = null;
        var $v_2 = SP.ListOperation.Selection.getSelectedItems();
        $v_1 = SP.ListOperation.Selection.getSelectedList();
        if (!SP.Ribbon.SU.$0($v_1) && $p1) {
            $v_1 = $v_1.replace('{', '%7B');
            $v_1 = $v_1.replace('-', '%2D');
            $v_1 = $v_1.replace('}', '%7D');
        }
        if (!SP.Ribbon.SU.$0($v_2)) {
            var $v_3 = 0;
            var $v_4 = null;
            var $$dict_2_0 = $v_2;
            for (var $$key_2_1 in $$dict_2_0) {
                var $v_5 = { key: $$key_2_1, value: $$dict_2_0[$$key_2_1] };
                $v_4 = $v_5.value.id.toString();
                $v_3++;
            }
            if ($v_3 === 1) {
                $v_0 = $v_4;
            }
        }
        $p0 = $p0.replace(new RegExp('{SelectedListId}', 'g'), $v_1);
        $p0 = $p0.replace(new RegExp('{SelectedItemId}', 'g'), $v_0);
        return $p0;
    },
    canHandleCommand: function (commandId) {
        if (this.commands[commandId]) {
            return eval(this.commands[commandId].EnabledScript);
        }
    },
    getFocusedCommands: function () {
        return [];
    },
    isFocusable: function () {
        return false;
    },
    //receiveFocus: function () { return true; },
    //yieldFocus: function () { return true; },
    getId: function () { return 'SimplePageComponent'; }
};

function initSimplePageComponent() {
    SimplePageComponent.PageComponent.registerClass('SimplePageComponent.PageComponent', CUI.Page.PageComponent);
}

ExecuteOrDelayUntilScriptLoaded(initSimplePageComponent, 'SP.Ribbon.js');
