"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
/**
 * Decorators.
 */
var TotalCore;
(function (TotalCore) {
    var Common;
    (function (Common) {
        /**
         * A small helper to inject dependencies dynamically.
         *
         * @param func
         */
        function annotate(func) {
            var $injector = angular.injector(['ng']);
            func.$inject = $injector.annotate(func).map(function (member) { return member.replace(/^_/, ''); });
        }
        /**
         * Injectable decorator.
         *
         * @returns {(Entity: any) => void}
         * @constructor
         */
        function Injectable() {
            return function (Entity) {
                annotate(Entity);
            };
        }
        Common.Injectable = Injectable;
        /**
         * Service decorator.
         *
         * @param {string} moduleName
         * @returns {(Service: any) => void}
         * @constructor
         */
        function Service(moduleName) {
            return function (Service) {
                var module;
                var name = Service.name;
                var isProvider = Service.hasOwnProperty('$get');
                annotate(Service);
                try {
                    module = angular.module(moduleName);
                }
                catch (exception) {
                    module = angular.module(moduleName, []);
                }
                module[isProvider ? 'provider' : 'service'](name, Service);
            };
        }
        Common.Service = Service;
        /**
         * Factory decorator.
         *
         * @param {string} moduleName
         * @param selector
         * @returns {(Factory: any) => void}
         * @constructor
         */
        function Factory(moduleName, selector) {
            return function (Factory) {
                var module;
                var name = selector || ("" + Factory.name.charAt(0).toLowerCase() + Factory.name.slice(1)).replace('Factory', '');
                annotate(Factory);
                try {
                    module = angular.module(moduleName);
                }
                catch (exception) {
                    module = angular.module(moduleName, []);
                }
                module.factory(name, Factory);
            };
        }
        Common.Factory = Factory;
        /**
         * Controller decorator.
         *
         * @param {string} moduleName
         * @returns {(Controller: any) => void}
         * @constructor
         */
        function Controller(moduleName) {
            return function (Controller) {
                var module;
                var name = Controller.name;
                annotate(Controller);
                try {
                    module = angular.module(moduleName);
                }
                catch (exception) {
                    module = angular.module(moduleName, []);
                }
                module.controller(name, Controller);
            };
        }
        Common.Controller = Controller;
        /**
         * Filter decorator.
         *
         * @param {string} moduleName
         * @param selector
         * @returns {(Filter: any) => void}
         * @constructor
         */
        function Filter(moduleName, selector) {
            return function (Filter) {
                var module;
                var name = selector || ("" + Filter.name.charAt(0).toLowerCase() + Filter.name.slice(1)).replace('Filter', '');
                annotate(Filter);
                try {
                    module = angular.module(moduleName);
                }
                catch (exception) {
                    module = angular.module(moduleName, []);
                }
                module.filter(name, Filter);
            };
        }
        Common.Filter = Filter;
        /**
         * Component decorator.
         *
         * @param moduleName
         * @param {angular.IComponentOptions} options
         * @param {any} selector
         * @returns {(Class: any) => void}
         * @constructor
         */
        function Component(moduleName, options, selector) {
            if (selector === void 0) { selector = null; }
            return function (Class) {
                var module;
                selector = selector || ("" + Class.name.charAt(0).toLowerCase() + Class.name.slice(1)).replace('Component', '');
                options.controller = Class;
                annotate(Class);
                try {
                    module = angular.module(moduleName);
                }
                catch (exception) {
                    module = angular.module(moduleName, []);
                }
                module.component(selector, options);
            };
        }
        Common.Component = Component;
        /**
         * Directive decorator.
         *
         * @param moduleName
         * @param {any} selector
         * @returns {(Class: any) => void}
         * @constructor
         */
        function Directive(moduleName, selector) {
            if (selector === void 0) { selector = null; }
            return function (Class) {
                var module;
                selector = selector || ("" + Class.name.charAt(0).toLowerCase() + Class.name.slice(1)).replace('Directive', '');
                annotate(Class);
                try {
                    module = angular.module(moduleName);
                }
                catch (exception) {
                    module = angular.module(moduleName, []);
                }
                module.directive(selector, Class);
            };
        }
        Common.Directive = Directive;
    })(Common = TotalCore.Common || (TotalCore.Common = {}));
})(TotalCore || (TotalCore = {}));
///<reference path="../../common/decorators.ts"/>
var TotalCore;
(function (TotalCore) {
    var Common;
    (function (Common) {
        var Configs;
        (function (Configs) {
            var HttpConfig = /** @class */ (function () {
                function HttpConfig($resourceProvider, $httpProvider, $compileProvider) {
                    // Don't strip trailing slashes from calculated URLs
                    $resourceProvider.defaults.stripTrailingSlashes = false;
                    $httpProvider.defaults.transformRequest = function (data) {
                        if (data === undefined) {
                            return data;
                        }
                        return HttpConfig_1.serializer(new FormData(), data);
                    };
                    $httpProvider.defaults.headers.post['Content-Type'] = undefined;
                    $compileProvider.debugInfoEnabled(false);
                }
                HttpConfig_1 = HttpConfig;
                HttpConfig.serializer = function (form, fields, parent) {
                    angular.forEach(fields, function (fieldValue, fieldName) {
                        if (parent) {
                            fieldName = parent + "[" + fieldName + "]";
                        }
                        if (fieldValue !== null && typeof fieldValue === 'object' && (fieldValue.__proto__ === Object.prototype || fieldValue.__proto__ === Array.prototype)) {
                            HttpConfig_1.serializer(form, fieldValue, fieldName);
                        }
                        else {
                            if (typeof fieldValue === 'boolean') {
                                fieldValue = Number(fieldValue);
                            }
                            else if (fieldValue === null) {
                                fieldValue = '';
                            }
                            form.append(fieldName, fieldValue);
                        }
                    });
                    return form;
                };
                HttpConfig = HttpConfig_1 = __decorate([
                    Common.Injectable()
                ], HttpConfig);
                return HttpConfig;
                var HttpConfig_1;
            }());
            Configs.HttpConfig = HttpConfig;
        })(Configs = Common.Configs || (Common.Configs = {}));
    })(Common = TotalCore.Common || (TotalCore.Common = {}));
})(TotalCore || (TotalCore = {}));
///<reference path="../../common/decorators.ts"/>
var TotalCore;
(function (TotalCore) {
    var Common;
    (function (Common) {
        var Configs;
        (function (Configs) {
            var GlobalConfig = /** @class */ (function () {
                function GlobalConfig($locationProvider, $compileProvider) {
                    $locationProvider.html5Mode({ enabled: true, requireBase: false, rewriteLinks: false });
                    // $compileProvider.debugInfoEnabled(false);
                    // $compileProvider.commentDirectivesEnabled(false);
                    // $compileProvider.cssClassDirectivesEnabled(false);
                }
                GlobalConfig = __decorate([
                    Common.Injectable()
                ], GlobalConfig);
                return GlobalConfig;
            }());
            Configs.GlobalConfig = GlobalConfig;
        })(Configs = Common.Configs || (Common.Configs = {}));
    })(Common = TotalCore.Common || (TotalCore.Common = {}));
})(TotalCore || (TotalCore = {}));
///<reference path="../../../../../vendor/misqtech/totalsuite-totalcore/assets/scripts/common/decorators.ts" />
var TotalContest;
(function (TotalContest) {
    var Controller = TotalCore.Common.Controller;
    // @ts-ignore
    var SubmissionsListingCtrl = /** @class */ (function () {
        function SubmissionsListingCtrl($scope, $compile, $sce) {
            this.$scope = $scope;
            this.$compile = $compile;
            this.$sce = $sce;
            this.NO_PREVIEW = 'data:image/png;base64, ';
            this.colspan = 8;
            try {
                angular.element(document).ready(function () {
                    var $list = angular.element('#the-list');
                    this.colspan = angular.element('#the-list tr:first-child td, #the-list tr:first-child th').length;
                    $list.find('tr').each(function (index, element) {
                        var $element = angular.element(this);
                        var id = $element.find('input[name="post[]"]').val();
                        $element.attr('ng-class', "{active: $ctrl.isPreviewOf(" + id + ")}");
                        $element.find('.column-inline-preview button').attr('ng-click', "$ctrl.togglePreview(" + id + ")");
                        $element.find('.row-actions .confirm a').attr('ng-click', "$ctrl.approveSubmission(" + id + ", $event)");
                        $element.after("<tr class=\"hidden\" ng-if=\"$ctrl.isPreviewOf(" + id + ")\"></tr><tr ng-if=\"$ctrl.isPreviewOf(" + id + ")\"><td ng-include=\"'preview-submission-template'\" ng-init=\"submission = $ctrl.getSubmission(" + id + ")\" ng-attr-colspan=\"{{$ctrl.colspan}}\" class=\"totalcontest-submission\"></td></tr>");
                    });
                    $compile($list)($scope.$new());
                });
            }
            catch (e) {
                console.debug(e);
            }
        }
        SubmissionsListingCtrl.prototype.approveSubmission = function (submissionId, event) {
            event.preventDefault();
            var $state = jQuery("#post-" + submissionId + " .post-state:first");
            var $link = jQuery(event.currentTarget);
            var $action = $link.parent();
            $state.html('Approving, ');
            $action.hide();
            jQuery.ajax($link.attr('href'))
                .then(function () {
                $state.html('Approved, ');
                $action.remove();
                setTimeout(function () { return $state.remove(); }, 2000);
            })
                .fail(function () {
                $state.html('Error, ');
                $action.show();
            });
        };
        SubmissionsListingCtrl.prototype.getContent = function (submissionId) {
            return this.$sce.trustAsHtml(this.getSubmission(submissionId).content);
        };
        SubmissionsListingCtrl.prototype.getPreview = function (submissionId) {
            var submission = this.getSubmission(submissionId);
            return this.$sce.trustAsHtml(submission.preview || "<img src=\"" + (submission.thumbnail || this.NO_PREVIEW) + "\">");
        };
        SubmissionsListingCtrl.prototype.getSubmission = function (submissionId) {
            return TotalContest.submissions[submissionId];
        };
        SubmissionsListingCtrl.prototype.isPreviewOf = function (submissionId) {
            return this.getSubmission(submissionId) && Boolean(this.getSubmission(submissionId)._preview);
        };
        SubmissionsListingCtrl.prototype.togglePreview = function (submissionId) {
            var submission = this.getSubmission(submissionId);
            submission._preview = !submission._preview;
        };
        SubmissionsListingCtrl = __decorate([
            Controller('controllers.totalcontest')
        ], SubmissionsListingCtrl);
        return SubmissionsListingCtrl;
    }());
    TotalContest.SubmissionsListingCtrl = SubmissionsListingCtrl;
})(TotalContest || (TotalContest = {}));
///<reference path="../../../../build/typings/index.d.ts" />
///<reference path="../../../../vendor/misqtech/totalsuite-totalcore/assets/scripts/common/configs/http.ts" />
///<reference path="../../../../vendor/misqtech/totalsuite-totalcore/assets/scripts/common/configs/global.ts" />
///<reference path="controllers/submissions-listing.ts" />
var TotalContest;
(function (TotalContest) {
    var HttpConfig = TotalCore.Common.Configs.HttpConfig;
    var GlobalConfig = TotalCore.Common.Configs.GlobalConfig;
    TotalContest.submission = angular
        .module('submissions-listing', [
        'ngResource',
        'controllers.totalcontest'
    ])
        .config(GlobalConfig)
        .config(HttpConfig)
        .value('ajaxEndpoint', window['totalcontestAjaxURL'] || window['ajaxurl'] || '/wp-admin/admin-ajax.php')
        .value('namespace', 'TotalContest')
        .value('prefix', 'totalcontest');
    TotalContest.submissions = {};
    angular.element().ready(function () {
        angular.element('#screen-meta').attr('ng-non-bindable', 0);
        angular.element('#posts-filter').attr('ng-non-bindable', 0);
        angular.element('#wpbody-content').attr('ng-controller', 'SubmissionsListingCtrl as $ctrl');
        angular.bootstrap(document.body, ['submissions-listing']);
    });
})(TotalContest || (TotalContest = {}));

//# sourceMappingURL=maps/submissions-listing.js.map
