Skip to content

Commit

Permalink
Allow user to preprocess chunks, add .create();
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentin Krepkiy authored and Valentin Krepkiy committed Feb 3, 2017
1 parent 808a267 commit 8cca3b1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
demo
node_modules
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

## Usage
```javascript
var vc = new VC(options);

vc.setText(str).renderTo($container);
VC.create(options).setText(str).renderTo($container);
```

## Options
- append
- length
- append (Bool)
- chunkPreProcessor (Function)
- length (Number)


## API
<table>
<tr>
<th>Method</th><th>Arguments</th><th>Returns</th><th>Description</th>
</tr>
<tr>
<td>create <sup>(static)</sup></td><td>none</td><td>self</td><td>Destroy VC instance</td>
</tr>
<tr>
<td>destroy</td><td>none</td><td>self</td><td>Destroy VC instance</td>
</tr>
Expand All @@ -29,7 +31,6 @@ vc.setText(str).renderTo($container);
<td>setHtml</td><td>String</td><td>self</td><td>Set HTML content</td>
</tr>
<tr>
<td>setTextl</td><td>String</td><td>self</td><td>Set text content</td>
<td>setText</td><td>String</td><td>self</td><td>Set text content</td>
</tr>

</table>
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "virtualcontent",
"version": "0.1.0",
"version": "0.1.1",
"description": "A JS browser tool designed for handling large text/html content",
"main": "src/virtual_content.js",
"scripts": {
Expand All @@ -18,7 +18,7 @@
"virtual",
"scroll"
],
"author": "Valentin Krepkiy",
"author": "Valentin Krepkiy <valkrepkiy@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/vkrepkiy/virtualcontent/issues"
Expand Down
37 changes: 27 additions & 10 deletions src/virtual_content.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

(function () {

const HTML_TYPE = "html";
const TEXT_TYPE = "text";
const REPLACE_MODE = "replace";
Expand All @@ -14,6 +13,8 @@
const NO_WORD_SPLIT = false;
const MAX_WORD_SIZE = 100;

const CHUNK_PREPROCESSOR = null;

/**
* @class VirtualContent
*
Expand All @@ -27,11 +28,12 @@
class VirtualContent {

constructor (options = {}) {
this._chunkLenght = options.length || DEFAULT_CHUNK_SIZE;
this._mode = options.append ? APPEND_MODE : REPLACE_MODE;
this._noWordSplit = options.noWordSplit || NO_WORD_SPLIT;
this._threshold = options.threshold || DEFAULT_THRESHOLD;
this._type = options.type || TEXT_TYPE;
this._chunkLenght = options.length || DEFAULT_CHUNK_SIZE;
this._mode = options.append ? APPEND_MODE : REPLACE_MODE;
this._noWordSplit = options.noWordSplit || NO_WORD_SPLIT;
this._threshold = options.threshold || DEFAULT_THRESHOLD;
this._type = options.type || TEXT_TYPE;
this._chunkPreProcessor = options.chunkPreProcessor || CHUNK_PREPROCESSOR;

this._chunks = [];
this._el = document.createElement("div");
Expand All @@ -53,6 +55,10 @@
}
}

static create (options) {
return new VirtualContent(options);
}

_addFillerTo (parent, height = 0) {
var el = document.createElement("div");

Expand Down Expand Up @@ -186,6 +192,14 @@
this._updateContent(curPointer, prevPointer);
}

_preprocessChunk (chunkContent) {
if (this._chunkPreProcessor) {
chunkContent = this._chunkPreProcessor(chunkContent);
}

return chunkContent;
}

// TODO: write _recalculate method or add some restrictions
_recalculate () {
console.info("Size was changed.");
Expand Down Expand Up @@ -354,7 +368,7 @@
let chunkEl = document.createElement("span");

chunkEl.dataset.chunkIndex = i;
chunkEl.innerHTML = this._chunks[i];
chunkEl.innerHTML = this._preprocessChunk(this._chunks[i]);

this._el.appendChild(chunkEl);

Expand Down Expand Up @@ -386,6 +400,8 @@
var chunkIndex = chunkEl.dataset.chunkIndex = startOffset + i;
var leftPad = (!i && this._getChunkLeftPad(chunkIndex)) || 0;

content = this._preprocessChunk(content);

chunkEl.setAttribute("style", "position:relative;");

chunkEl.innerHTML = `
Expand Down Expand Up @@ -488,12 +504,13 @@
};
}


if (typeof module === "object" && typeof module.exports === "object") {
module.exports = VirtualContent;
module.exports = VirtualContent;
} else if (typeof define === "function" && define.amd) {
define(VirtualContent);
define(VirtualContent);
}
if (typeof window !== "undefined") {
window.VC = VirtualContent;
window.VC = VirtualContent;
}
})();

0 comments on commit 8cca3b1

Please sign in to comment.