/**
 * @ngdoc directive
 * @name uiGridSt
 * @restrict A
 * @element any with ui-grid
 * @example
 * <pre>
 *      <div ui-grid="myGrid.options" ui-grid-st ui-grid-edit ui-grid-validate></div>
 * </pre>
 */
export function uiGridStDirective(){
    return {
        require: 'uiGrid',
        scope: false,
        restrict: 'A',
        link: ($scope, $elm, $attr, uiGridCtrl) => {
            let id = `.grid${uiGridCtrl.grid.id}`;
            $attr.$set('tabindex', 0);
            $elm.addClass(`ui-grid-st`);
            if (!angular.element(document.querySelector(`${id}#ui-grid-st-styles`)).length){
                let styles = angular.element(`<style id="ui-grid-st-styles">.grid${id}.ui-grid-st .ui-grid-cell.highlight-error:before{content:"";position:absolute;z-index:10002;width:1px;height:${uiGridCtrl.grid.options.rowHeight / 2}px;margin-top:${uiGridCtrl.grid.options.rowHeight / 2}px;background:red;}</style>`);
                $elm.append(styles);
            }
            function onCtrlS($event){
                if ($event.which === 83 && (navigator.platform.match("Mac") ? $event.metaKey : $event.ctrlKey)) {
                    $event.preventDefault();
                    uiGridCtrl.grid.api.st.saveData();
                }
            }
            $elm.on('keydown', onCtrlS);
            $scope.$on('$destroy', function(){
                $elm.off('keydown', onCtrlS);
            });
        }
    };
}

