Code coverage report for dist/layout-bin-packer.js

Statements: 96.19% (227 / 236)      Branches: 81.25% (78 / 96)      Functions: 93.33% (28 / 30)      Lines: 97% (226 / 233)      Ignored: none     

All files » dist/ » layout-bin-packer.js
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 4431 1           1 12 12     1 6 4         1                     1       1           1     1   1 2       1 388           1 46 46 46               1           1   1 93     1 93     1 93 93 93     1 6 6     1 1 1 5 1 1   5 5       5 27 27 3       2     1 49 3 3     49 49     49 49 49   49 99   99 49 50 49         50     49     1 9 9 9       1 10         10   10     1 240 240   240 1     239 239 239 239   239 239 239   239 192 47 14 14   33 33 33 33 33 33     47 93 93   93   62 62 62 62   31     93 14     93   93     47     1 10 10 10 10 10   10 8   2     10 10 35 35   35     30 30 5       35 25 10 4 4 4   6       10     1 33   33 1     32 4 4     32     1 24   20 1 1   20   20 20                           20 20 20   20     20     20 20       20 20   20 20   20   20   3 4 4   4 2   2 2 2     3 17   17 17 17 17 17   17             1 2 2   2     1 1   1       1 2     1 16 16   16     1 6 6 6   6 6   6   6 3     6   6     1 54     1 48     1 4 4       4   4 4   4     1 21     21 21   21 21 21 21   21     1 1 1      
(function (global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  typeof define === 'function' && define.amd ? define(['exports'], factory) :
  factory((global.Bin = {}));
}(this, function (exports) { 'use strict';
 
  // I don't think this should have args
  function Bin(content, width) {
    this.width = width || 0;
    this.content = content;
  }
 
  function mustImplement(name) {
    return function() {
      throw new TypeError("MustImplement: " + name );
    };
  }
 
  // abstract
  Bin.prototype.objectAt = function (collection, index) {
    return collection[index];
  };
 
  // abstract: return coordinates of element at index.
  //
  // @param index: index of the element in content
  // @param width: viewport width.
  // @returns {x, y} coordinates of element at index.
  //
  // May reset cached viewport width.
  Bin.prototype.position = mustImplement('position');
 
  // abstract: reset internal state to be anchored at index.
  // @param index: index of the element in content
  Bin.prototype.flush = mustImplement('flush');
 
  // abstract: return total content height given viewport width.
  // @param width: viewport width
  //
  // May reset cached viewport width.
  Bin.prototype.height = mustImplement('height');
 
  // abstract: true if layout places more than one object on a line.
  Bin.prototype.isGrid = mustImplement('isGrid');
 
  function _rangeError(length, index) {
    throw new RangeError("Parameter must be within: [" + 0 + " and " + length + ") but was: " + index);
  }
 
  // abstract: returns number of elements in content.
  Bin.prototype.length = function () {
    return this.content.length;
  };
 
  // maximum offset of content wrt to viewport
  // The amount by which content (after being layed out) is taller than
  // the viewport.
  Bin.prototype.maxContentOffset = function Bin_maxContentOffset(width, height) {
    var contentHeight = this.height(width);
    var maxOffset = Math.max(contentHeight - height, 0);
    return maxOffset;
  }
 
  // abstract: returns index of first visible item.
  // @param topOffset: scroll position
  // @param width: width of viewport
  // @param height: height of viewport
  //
  Bin.prototype.visibleStartingIndex = mustImplement('visibleStartingIndex');
 
  // abstract: returns number of items visible in viewport.
  // @param topOffset: scroll position
  // @param width: width of viewport
  // @param height: height of viewport
  Bin.prototype.numberVisibleWithin = mustImplement('numberVisibleWithin');
 
  Bin.prototype.heightAtIndex = function (index) {
    return this.content[index].height;
  };
 
  Bin.prototype.widthAtIndex = function (index) {
    return this.content[index].width;
  };
 
  function Entry(height, width, x, y) {
    this.height   = height;
    this.width    = width;
    this.position = {x:x, y:y};
  }
 
  function ShelfFirst(content, width) {
    this._super$constructor(content, width);
    this._positionEntries = [];
  }
 
  ShelfFirst.prototype = Object.create(Bin.prototype);
  ShelfFirst.prototype._super$constructor = Bin;
  ShelfFirst.prototype.isGrid = function ShelfFirst_isGrid(width) {
    if (width != null && width !== this.width) {
      this.flush(0);
      this.width = width;
    }
    var length = this.length();
    var entry;
 
    // TODO: cache/memoize
 
    for (var i = 0; i < length; i++) {
      entry = this._entryAt(i);
      if (entry.position.x > 0) {
        return true;
      }
    }
 
    return false;
  };
 
  ShelfFirst.prototype.height = function (width) {
    if (width != null && width !== this.width) {
      this.flush(0);
      this.width = width;
    }
 
    var length = this.length();
    Iif (length === 0) { return 0; }
 
    // find tallest in last row, add to Y
    var tallest  = 0;
    var currentY = 0;
    var entry;
 
    for (var i = length - 1; i >= 0; i--) {
      entry = this._entryAt(i);
 
      if (currentY > entry.position.y) {
        break; // end of last row
      } else if (tallest < entry.height) {
        tallest = entry.height;
      } else {
        // do nothing
      }
 
      currentY = entry.position.y;
    }
 
    return currentY + tallest;
  };
 
  ShelfFirst.prototype.flush = function (position) {
    var positionEntries = this._positionEntries;
    Eif (positionEntries.length > position) {
      positionEntries.length = position;
    }
  };
 
  ShelfFirst.prototype.numberVisibleWithin = function (topOffset, width, height, withPadding) {
    Iif (width !== this.width) {
      this.flush(0);
      this.width = width;
    }
 
    var startingIndex = this.visibleStartingIndex(topOffset, width, height);
 
    return this._numberVisibleWithin(startingIndex, height, withPadding);
  };
 
  ShelfFirst.prototype._entryAt = function _entryAt(index) {
    var length = this.length();
    var width = this.width;
 
    if (index >= length) {
      _rangeError(length, index);
    }
 
    var entry;
    var entries = this._positionEntries;
    var entriesLength = entries.length;
    var startingIndex;
 
    var y, x, i;
    var rowHeight = 0;
    var rowWidth = 0;
 
    if (index < entriesLength) {
      return this._positionEntries[index];
    } else if (entriesLength === 0) {
      startingIndex = 0;
      y = 0;
    } else {
      startingIndex = entriesLength - 1;
      entry = this._positionEntries[startingIndex];
      rowWidth = entry.position.x + entry.width;
      rowHeight = entry.height;
      y = entry.position.y;
      startingIndex++;
    }
 
    for (i = startingIndex; i < index + 1; i++) {
      var currentHeight = this.heightAtIndex(i);
      var currentWidth = this.widthAtIndex(i);
 
      if (entry && (currentWidth + rowWidth) > width) {
        // new row
        y = entry.position.y + entry.height;
        x = 0;
        rowWidth = 0;
        rowHeight = currentHeight;
      } else {
        x = rowWidth;
      }
 
      if (currentHeight > rowHeight) {
        rowHeight = currentHeight;
      }
 
      entry = this._positionEntries[i] = new Entry(rowHeight, currentWidth, x, y);
 
      rowWidth = x + currentWidth;
    }
 
    return entry;
  };
 
  ShelfFirst.prototype._numberVisibleWithin = function (startingIndex, height, withPadding) {
    var count = 0;
    var length = this.length();
    var entry, position;
    var currentY = 0;
    var yOffset = 0;
 
    if (startingIndex > 0 && startingIndex < length) {
      yOffset = this._entryAt(startingIndex).position.y;
    } else {
      yOffset = 0;
    }
 
    var firstRowHeight;
    for (var i = startingIndex; i < length; i++) {
      entry = this._entryAt(i);
      position = entry.position;
 
      if (currentY === position.y) {
        // same row
      } else {
        currentY = position.y - yOffset;
        if (withPadding && !firstRowHeight) {
          firstRowHeight = entry.height;
        }
      }
 
      if (currentY < height) {
        count++;
      } else if (withPadding) {
        withPadding = false;
        height += Math.max(firstRowHeight, entry.height) + 1;
        count++;
      } else {
        break;
      }
    }
 
    return count;
  };
 
  ShelfFirst.prototype.position = function position(index, width) {
    var length = this.length();
 
    if (length === 0 || index > length) {
      _rangeError(length, index);
    }
 
    if (width !== this.width) {
      this.flush(0);
      this.width =  width;
    }
 
    return this._entryAt(index).position;
  };
 
  ShelfFirst.prototype.visibleStartingIndex = function (topOffset, width, visibleHeight) {
    if (topOffset <= 0 ) { return 0; }
 
    if (width != null && width!== this.width) {
      this.flush(0);
      this.width = width;
    }
    topOffset = Math.min(topOffset, this.maxContentOffset(width, visibleHeight));
 
    var height = this.height();
    var length = this.length();
 
    // Start searching using the last item in the list
    // and the bottom of the list for calculating the average height.
 
    // This algorithm is necessary for efficiently finding
    // the starting index of a list with variable heights
    // in less than O(n) time.
 
    // Ideally, the performance will be O(log n).
    // The algorithm implemented assumes that the best case
    // is a list of items with all equal heights.
    // Lists with consistent distributions should arrive
    // at results fairly quickly as well.
    var index = length;
    var bottom = height;
    var previousIndex;
 
    for (;;) {
      // Try to find an item that straddles the top offset
      // or is flush with it
      var averageHeight = bottom / index;
 
      // Guess the index based off the average height
      index = Math.min(Math.floor(topOffset / averageHeight), length - 1);
      Iif (previousIndex === index) {
        return index;
      }
 
      var entry = this._entryAt(index);
      var position = entry.position;
 
      var top = position.y;
      bottom = top + entry.height;
 
      previousIndex = index;
 
      if (bottom > topOffset) {
        // Walk backwards until we find an item that won't be shown
        while (bottom >= topOffset) {
          previousIndex = index;
          index--;
 
          if (index === -1) {
            break;
          }
          entry = this._entryAt(index);
          position = entry.position;
          bottom = position.y + entry.height;
        }
 
        return previousIndex;
      } else Eif (topOffset === bottom) {
        // Walk forwards until we find the next one- it should be close
        while (bottom <= topOffset) {
          index++;
          entry = this._entryAt(index);
          position = entry.position;
          bottom = position.y + entry.height;
        }
        return index;
      }
    }
 
    return -1;
  };
 
  function FixedGrid(content, elementWidth, elementHeight) {
    this._elementWidth =  elementWidth;
    this._elementHeight =  elementHeight;
 
    this._super$constructor(content);
  }
 
  FixedGrid.prototype = Object.create(Bin.prototype);
  FixedGrid.prototype._super$constructor = Bin;
 
  FixedGrid.prototype.flush = function (/* index, to */) {
 
  };
 
  FixedGrid.prototype.isGrid = function (width) {
    return (Math.floor(width / this.widthAtIndex(0)) || 1) > 1;
  };
 
  FixedGrid.prototype.visibleStartingIndex = function (topOffset, width, height) {
    topOffset = Math.min(topOffset, this.maxContentOffset(width, height));
    var columns = Math.floor(width / this.widthAtIndex(0)) || 1;
 
    return Math.floor(topOffset / this.heightAtIndex(0)) * columns;
  };
 
  FixedGrid.prototype.numberVisibleWithin = function (topOffset, width, height, withPadding) {
    var startingIndex = this.visibleStartingIndex(topOffset, width, height);
    var columns = Math.floor(width / this.widthAtIndex(0)) || 1;
    var length = this.length();
 
    var rowHeight = this.heightAtIndex(0);
    var rows = Math.ceil(height / rowHeight);
 
    var maxNeeded = rows * columns;
 
    if (withPadding) {
      maxNeeded += columns;
    }
 
    var potentialVisible = length - startingIndex;
 
    return Math.max(Math.min(maxNeeded, potentialVisible), 0);
  };
 
  FixedGrid.prototype.widthAtIndex = function (/* index */) {
    return this._elementWidth;
  };
 
  FixedGrid.prototype.heightAtIndex = function (/* index */) {
    return this._elementHeight;
  };
 
  FixedGrid.prototype.position = function (index, width) {
    var length = this.length();
    Iif (length === 0 || index > length) {
      rangeError(length, index);
    }
 
    var columns = Math.floor(width / this.widthAtIndex(index)) || 1;
 
    var x = index % columns * this.widthAtIndex(index) | 0;
    var y = Math.floor(index / columns) * this.heightAtIndex(index);
 
    return {x:x, y:y};
  };
 
  FixedGrid.prototype.height = function (visibleWidth) {
    Iif (typeof visibleWidth !== 'number') {
      throw new TypeError('height depends on the first argument of visibleWidth(number)');
    }
    var length = this.length();
    Iif (length === 0) { return 0; }
 
    var columnCount = Math.max(Math.floor(visibleWidth/this.widthAtIndex(0), 1));
    columnCount = columnCount > 0 ? columnCount : 1;
    var rows = Math.ceil(length/columnCount);
    var totalHeight = rows * this.heightAtIndex(0);
 
    return totalHeight;
  };
 
  exports.Bin = Bin;
  exports.ShelfFirst = ShelfFirst;
  exports.FixedGrid = FixedGrid;
 
}));
//# sourceMappingURL=layout-bin-packer.js.map