feat: tablerowloop object

See: https://www.rubydoc.info/gems/liquid/Liquid/TablerowloopDrop
This commit is contained in:
Jun Yang
2019-03-02 06:00:30 +08:00
parent cca0306f5b
commit 3647305dcf
8 changed files with 121 additions and 129 deletions
+9 -26
View File
@@ -1,4 +1,3 @@
import { mapSeries } from '../../util/promise'
import { isString, isObject, isArray } from '../../util/underscore'
import { evalExp } from '../../render/syntax'
import assert from '../../util/assert'
@@ -10,6 +9,7 @@ import Hash from '../../template/tag/hash'
import ITemplate from '../../template/itemplate'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'
import { ForloopDrop } from '../../drop/forloop-drop'
const re = new RegExp(`^(${identifier.source})\\s+in\\s+` +
`(${value.source})` +
@@ -61,39 +61,22 @@ export default <ITagImplOptions>{
collection = collection.slice(offset, offset + limit)
if (this.reversed) collection.reverse()
const contexts = collection.map((item: string, i: number) => {
const ctx = {}
ctx[this.variable] = item
ctx['forloop'] = {
first: i === 0,
index: i + 1,
index0: i,
last: i === collection.length - 1,
length: collection.length,
rindex: collection.length - i,
rindex0: collection.length - i - 1
}
return ctx
})
const context = { forloop: new ForloopDrop(collection.length) }
scope.push(context)
let html = ''
let finished = false
await mapSeries(contexts, async context => {
if (finished) return
scope.push(context)
for (const item of collection) {
context[this.variable] = item
try {
html += await this.liquid.renderer.renderTemplates(this.templates, scope)
} catch (e) {
if (e.name === 'RenderBreakError') {
html += e.resolvedHTML
if (e.message === 'break') {
finished = true
}
if (e.message === 'break') break
} else throw e
}
scope.pop(context)
})
context.forloop.next()
}
scope.pop()
return html
}
}
+13 -23
View File
@@ -1,4 +1,3 @@
import { mapSeries } from '../../util/promise'
import assert from '../../util/assert'
import { evalExp } from '../../render/syntax'
import { identifier, value, hash } from '../../parser/lexical'
@@ -9,6 +8,7 @@ import Scope from '../../scope/scope'
import Hash from '../../template/tag/hash'
import ITagImplOptions from '../../template/tag/itag-impl-options'
import ParseStream from '../../parser/parse-stream'
import { TablerowloopDrop } from '../../drop/tablerowloop-drop'
const re = new RegExp(`^(${identifier.source})\\s+in\\s+` +
`(${value.source})` +
@@ -42,34 +42,24 @@ export default {
collection = collection.slice(offset, offset + limit)
const cols = hash.cols || collection.length
const contexts = collection.map((item: any) => {
const ctx = {}
ctx[this.variable] = item
return ctx
})
let row: number = 0
const tablerowloop = new TablerowloopDrop(collection.length, cols)
const ctx = { tablerowloop }
scope.push(ctx)
let html = ''
await mapSeries(contexts, async (context, idx) => {
row = Math.floor(idx / cols) + 1
const col = (idx % cols) + 1
if (col === 1) {
if (row !== 1) {
html += '</tr>'
}
html += `<tr class="row${row}">`
for (let idx = 0; idx < collection.length; idx++, tablerowloop.next()) {
ctx[this.variable] = collection[idx]
if (tablerowloop.col0() === 0) {
if (tablerowloop.row() !== 1) html += '</tr>'
html += `<tr class="row${tablerowloop.row()}">`
}
html += `<td class="col${col}">`
scope.push(context)
html += `<td class="col${tablerowloop.col()}">`
html += await this.liquid.renderer.renderTemplates(this.templates, scope)
html += '</td>'
scope.pop(context)
return html
})
if (row > 0) {
html += '</tr>'
}
if (collection.length) html += '</tr>'
scope.pop(ctx)
return html
}
} as ITagImplOptions
+34
View File
@@ -0,0 +1,34 @@
import { Drop } from './drop'
export class ForloopDrop extends Drop {
protected i: number = 0
length: number
constructor (length: number) {
super()
this.length = length
}
next () {
this.i++
}
index0 () {
return this.i
}
index () {
return this.i + 1
}
first () {
return this.i === 0
}
last () {
return this.i === this.length - 1
}
rindex () {
return this.length - this.i
}
rindex0 () {
return this.length - this.i - 1
}
valueOf () {
return JSON.stringify(this)
}
}
+25
View File
@@ -0,0 +1,25 @@
import { ForloopDrop } from './forloop-drop'
export class TablerowloopDrop extends ForloopDrop {
private cols: number
constructor (length: number, cols: number) {
super(length)
this.length = length
this.cols = cols
}
row () {
return Math.floor(this.i / this.cols) + 1
}
col0 () {
return (this.i % this.cols)
}
col () {
return this.col0() + 1
}
col_first () { // eslint-disable-line
return this.col0() === 0
}
col_last () { // eslint-disable-line
return this.col() === this.cols
}
}
-19
View File
@@ -1,19 +0,0 @@
/*
* Call functions in serial until someone rejected.
* @param {Array} iterable the array to iterate with.
* @param {Array} iteratee returns a new promise.
* The iteratee is invoked with three arguments: (value, index, iterable).
*/
export function mapSeries<T1, T2> (
iterable: T1[],
iteratee: (item: T1, idx: number, iterable: T1[]) => Promise<T2> | T2
): Promise<T2[]> {
let ret = Promise.resolve(0)
const result: T2[] = []
iterable.forEach(function (item, idx) {
ret = ret
.then(() => iteratee(item, idx, iterable))
.then(x => result.push(x))
})
return ret.then(() => result)
}