﻿// EDITORS
/* THESE FORMATTERS & EDITORS ARE JUST SAMPLES! */

(function ($) {

    var baseUrl = '/content/script/images/';

    var liliumEditor = {

        SelectOptionsFormatter: function (row, cell, value, columnDef, dataContext) {
            return (value === null || value === undefined || value.toString() === "") ? "" : htmlEncode(value + " - " + selectOptionValue(columnDef.selectItems, value));
        },

        CleanSelectOptionsFormatter: function (row, cell, value, columnDef, dataContext) {
            return (value === null || value === undefined || value.toString() === "") ? "" : htmlEncode(selectOptionValue(columnDef.selectItems, value));
        },

        SelectOptionsWithTooltipFormatter: function (row, cell, value, columnDef, dataContext) {
            return (value === null || value === undefined || value.toString() === "") ? "" : $("<div />").append($("<acronym />").attr("title", selectOptionValue(columnDef.selectItems, value)).text(value)).html();
        },

        FlagOptionsFormatter: function (row, cell, value, columnDef, dataContext) {
            if (value === null || value === undefined || value.toString() === "") {
                return "<img src='" + baseUrl + "bullet_red.png'>";
            }

            var text = '';
            var val = parseInt(value.toString(), 10);

            var key;
            for (key in columnDef.selectItems) {
                if ((val & (parseInt(columnDef.selectItems[key].Value, 10))) > 0) {
                    text += columnDef.selectItems[key].Text + " | ";
                }
            }

            return (text === '') ? "<img src='" + baseUrl + "bullet_red.png'>" : htmlEncode(text);
        },

        StateFormatter: function (row, cell, value, columnDef, dataContext) {
            return $("<div />").append(messageStateFormatter(columnDef.selectItems, value)).html();
        },

        DlrStateFormatter: function (row, cell, value, columnDef, dataContext) {
            return $("<div />").append(dlrStateFormatter(columnDef.selectItems, value)).html();
        },

        LongTextFormatter: function (row, cell, value, columnDef, dataContext) {
            //return (value === null || value === undefined || value === "") ? "" : "<span title='" + htmlEncode(value) + "'>" + htmlEncode(value) + "</span>";
            return (value === null || value === undefined || value === "") ? "" : $("<div />").append($("<span />").attr("title", value).text(value)).html();
        },

        TextLengthFormatter: function (row, cell, value, columnDef, dataContext) {
            return (value === null || value === undefined || value === "") ? 0 : value.length;
        },

        PercentCompleteCellFormatter: function (row, cell, value, columnDef, dataContext) {
            if (value === null || value === undefined || value === "") {
                return "-";
            }
            else if (value < 50) {
                return "<span style='color:red;font-weight:bold;'>" + value + "%</span>";
            }
            else {
                return "<span style='color:green'>" + value + "%</span>";
            }
        },

        GraphicalPercentCompleteCellFormatter: function (row, cell, value, columnDef, dataContext) {
            if (value === null || value === undefined || value === "") {
                return "";
            }

            var total = dataContext[columnDef.totalValueField];
            var undeliveredCount = dataContext[columnDef.undeliveredCountValueField];
            var pending = total - value - undeliveredCount;

            var percentValue = parseInt(value * 100 / total, 10);
            var percentUndeliveredValue = parseInt(undeliveredCount * 100 / total, 10);
            var percentPendingValue = 100 - percentValue - percentUndeliveredValue;

            var title = LILIUM.deliveredCountText + ':' + value + ' | ' + LILIUM.undeliveredCountText + ':' + undeliveredCount + ' | ' + LILIUM.inProgressText + ':' + pending;

            var content = "<div title='" + title + "'><span class='percent-complete-bar' style='background:#50B432;width:" + percentValue + "%'></span>";
            content += "<span class='percent-complete-bar' style='background:#ED561B;width:" + percentUndeliveredValue + "%'></span>";
            content += "<span class='percent-complete-bar' style='background:silver;width:" + percentPendingValue + "%'></span>";
            content += "<div class='percent-complete-label'>" + percentValue + "%</div></div>";

            return content;
        },

        CompleteCountCellFormatter: function (row, cell, value, columnDef, dataContext) {

            var completeCount = dataContext[columnDef.completeCountValueField];
            var remaining = value - completeCount;

            return (remaining === 0) ? $.formatNumber(value, LILIUM.formatOptions) : "<span>" + $.formatNumber(value, LILIUM.formatOptions) + "/" + $.formatNumber(remaining, LILIUM.formatOptions) + "</span>";
        },

        NumberCellFormatter: function (row, cell, value, columnDef, dataContext) {
            return (value === null || value === undefined || value === "") ? 0 : $.formatNumber(value, LILIUM.formatOptions);
        },

        DecimalCellFormatter: function (row, cell, value, columnDef, dataContext) {
            if (value === null || value === undefined || value === "") {
                return 0;
            }

            if (value > 100000000) {
                return "&infin;";
            }

            return $.formatNumber(value, LILIUM.formatDecimalOptions);
        },

        LinkCellFormatter: function (row, cell, value, columnDef, dataContext) {
            return (value === null || value === undefined || value === "") ? "" : "<a href='" + columnDef.link + value + "'>" + value + "</a>";
        },

        BoolCellFormatter: function (row, cell, value, columnDef, dataContext) {
            return value ? "<img src='" + baseUrl + "tick.png'>" : "<img src='" + baseUrl + "bullet_red.png'>";
        },

        UseValueCellFormatter: function (row, cell, value, columnDef, dataContext) {
            return "<a href='#'><img src='" + baseUrl + "arrow_undo.png'></a>";
        },

        ViewCellFormatter: function (row, cell, value, columnDef, dataContext) {
            return (value === null || value === undefined || value === "") ? "" : "<a href='" + columnDef.link + value + "'><img src='" + baseUrl + "edit.png'></a>";
        },

        EditCellFormatter: function (row, cell, value, columnDef, dataContext) {
            return value ? "<img src='" + baseUrl + "edit.png'>" : "";
        },

        DeleteCellFormatter: function (row, cell, value, columnDef, dataContext) {
            return value ? "<img src='" + baseUrl + "delete.png'>" : "";
        },

        ValueFormatter: function (row, cell, value, columnDef, dataContext) {
            return htmlEncode(dataContext[columnDef.formatedValueField]);
        },

        PopulateSelectList: function ($container, id, url, copyTo) {

            $.ajax({ type: 'POST', url: url, data: { id: id }, async: false,
                success: function (data) {
                    $.busyDialog.hide();

                    $container.empty();

                    if (copyTo !== undefined) {
                        copyTo.selectItems = data;
                    }

                    $.each(data, function (index, item) {
                        buildSelectOption(item.Value, item.Text).appendTo($container);
                    });

                    return data;
                }
            });
        }
    };

    $.extend(window, liliumEditor);
})(jQuery);
