Send values to TM1 by sliding mouse left/right

The directive below will pull value from TM1 just like DBR, but it allows input by clicking on a number and moving the mouse left or right like if it is a slider. Elements in tm1-elements attribute must be inside single quotes.

HTML:

<tm1-drag-input
     tm1-instance="dev"
     tm1-cube="System Info" 
     tm1-elements="'Environment','Numeric'"
     tm1-max="100"
     tm1-min="-100"
     ng-model="sliderValue">         
</tm1-drag-input>

JavaScript:

app.directive('tm1DragInput', ['$document', '$tm1Ui', function ($document, $tm1Ui) {
var template = '<span class="text-center drag-input" ' +
    'ng-model="value" ' +
    'style="cursor:w-resize;" > ' +
    '{{value}} ' +
    '</span>';

return {
    template: template,
    scope: {
        tm1Instance: '@',
        tm1Cube: '@',
        tm1Elements: '@',
        tm1Max: '@',
        tm1Min: '@',
        value: '=ngModel'
    },

    link: function (scope, element, attrs) {
        var getCubeValue = function () {
            var f = '$tm1Ui.cellGet(scope.tm1Instance, scope.tm1Cube,' + scope.tm1Elements + ');';
            var getValue = eval(f);
            getValue.then(function (v) {
                if (v.Status == 'Data') {
                    scope.value = v.Value;
                } else if (v.Status == 'Null') {
                    scope.value = 0;
                }
            });
        };

        attrs.$observe('tm1Elements', function (string) {
            if (string.indexOf("''") === -1) {
                getCubeValue();
            }
        });

        var value;
        var original;

        element.on('mousedown', function (mouseDownEvent) {
            var startX = mouseDownEvent.pageX;

            original = isNaN(scope.value) ? 0 : scope.value;

            $document.on('mousemove', function (mouseMoveEvent) {
                value = original + mouseMoveEvent.pageX - startX;

                if (!isNaN(scope.tm1Max) && !isNaN(scope.tm1Min)) {
                    if (value >= scope.tm1Min && value <= scope.tm1Max) {
                        scope.value = Math.round(value);
                    }
                } else if (!isNaN(scope.tm1Min)) {
                    if (value >= scope.tm1Min) {
                        scope.value = Math.round(value);
                    }
                } else if (!isNaN(scope.tm1Min)) {
                    if (value <= scope.tm1Max) {
                        scope.value = Math.round(value);
                    }
                } else {
                    scope.value = Math.round(value);
                }

            });

            $document.on('mouseup', function () {
                $document.unbind('mousemove');
                $document.unbind('mouseup');
                if (scope.value != original) {
                    var f = '';
                    f = '$tm1Ui.cellPut(scope.value, scope.tm1Instance, scope.tm1Cube, ' + scope.tm1Elements + ');';
                    var sendValue = eval(f);
                    sendValue.then(function () {
                        $tm1Ui.dataRefresh();
                    });
                }
            });
        });
    }
};
}]);
4 Likes

@aeremenko do you have a screenshot of the end result?

There isn’t much to show as it is more about behavior. On a page you will see just a number. It can be styled any way you want with css by targeting .drag-input class

1 Like

1 Like

NICE ONE !! looks very cool