all files / common/models/drag-position/ drag-position.js

70% Statements 35/50
64% Branches 16/25
75% Functions 9/12
70% Lines 35/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88  27× 27× 27×     41×                                                             23×           24× 24× 12× 24× 12× 24×             34×                  
"use strict";
var immutable_class_1 = require('immutable-class');
var general_1 = require('../../utils/general/general');
var check;
var DragPosition = (function () {
    function DragPosition(parameters) {
        this.insert = general_1.hasOwnProperty(parameters, 'insert') ? parameters.insert : null;
        this.replace = general_1.hasOwnProperty(parameters, 'replace') ? parameters.replace : null;
        Iif (this.insert == null && this.replace == null)
            throw new Error('invalid drag position');
    }
    DragPosition.isDragPosition = function (candidate) {
        return immutable_class_1.isInstanceOf(candidate, DragPosition);
    };
    DragPosition.calculateFromOffset = function (offset, numItems, itemWidth, itemGap) {
        if (!numItems) {
            return new DragPosition({
                replace: 0
            });
        }
        if (offset < 0) {
            return new DragPosition({
                insert: 0
            });
        }
        var sectionWidth = itemWidth + itemGap;
        var sectionNumber = Math.floor(offset / sectionWidth);
        if (numItems <= sectionNumber) {
            return new DragPosition({
                replace: numItems
            });
        }
        var offsetWithinSection = offset - sectionWidth * sectionNumber;
        if (offsetWithinSection < itemWidth) {
            return new DragPosition({
                replace: sectionNumber
            });
        }
        else {
            return new DragPosition({
                insert: sectionNumber + 1
            });
        }
    };
    DragPosition.fromJS = function (parameters) {
        return new DragPosition(parameters);
    };
    DragPosition.prototype.valueOf = function () {
        return {
            insert: this.insert,
            replace: this.replace
        };
    };
    DragPosition.prototype.toJS = function () {
        var js = {};
        if (this.insert != null)
            js.insert = this.insert;
        if (this.replace != null)
            js.replace = this.replace;
        return js;
    };
    DragPosition.prototype.toJSON = function () {
        return this.toJS();
    };
    DragPosition.prototype.toString = function () {
        if (this.insert != null) {
            return "[insert " + this.insert + "]";
        }
        else {
            return "[replace " + this.replace + "]";
        }
    };
    DragPosition.prototype.equals = function (other) {
        return DragPosition.isDragPosition(other) &&
            this.insert === other.insert &&
            this.replace === other.replace;
    };
    DragPosition.prototype.isInsert = function () {
        return this.insert !== null;
    };
    DragPosition.prototype.isReplace = function () {
        return this.replace !== null;
    };
    return DragPosition;
}());
exports.DragPosition = DragPosition;
check = DragPosition;