<?php
    class DataTable
    {
        public static function sortIcon()
        {
            ?>
                <i class="text-secondary fa fa-fw fa-sort sort ordered"></i>
                <i class="text-secondary fa fa-fw fa-arrow-down-short-wide sort asc d-none"></i>
                <i class="text-secondary fa fa-fw fa-arrow-up-wide-short sort desc d-none"></i>
            <?php
        }

        public static function js($strTableSelector)
        {
            ?>
            <!-- This portion was generated by the common lib DataTable.php -->
            <script>
                $(function() {
                    fnSetupDataTable = function()
                    {
                        if (typeof(globalDataTable) !== "undefined")
                            globalDataTable.destroy();

                        globalDataTable = $("<?= $strTableSelector; ?>").DataTable({
                            paging:     false,
                            searching:  false,
                            info:       false,
                            order:      []
                        });

                        $("<?= $strTableSelector; ?>").on("order.dt", function() {
                            $("th.sorting").each(function(i, x) {
                                x = $(x);
                                x.find(".sort").addClass("d-none");

                                if (x.hasClass("dt-ordering-desc"))
                                    x.find(".sort.desc").removeClass("d-none");

                                else
                                if (x.hasClass("dt-ordering-asc"))
                                    x.find(".sort.asc").removeClass("d-none");

                                else
                                x.find(".sort.ordered").removeClass("d-none");
                            });
                        });
                    };

                    fnSetupDataTable();
                });
            </script>
            <?php
        }
    }
?>
