view app/result-type.ts @ 31:4926885f8a99

selectable result columns. nicer cypher query output.
author casties
date Mon, 01 Feb 2016 20:10:55 +0100
parents 193271b6b9d2
children 7578b21cdf2e
line wrap: on
line source

import {ResultColumn} from './result-column';

export class ResultType {
    public name: string;
    public idAttribute: string;
    public allowedAttributes: string[];
    public deniedAttributes: string[];
    
    constructor (name: string, idAttribute: string, allowedAttributes: string[]=[], deniedAttributes: string[]=[]) {
        this.name = name;
        this.idAttribute = idAttribute;
        this.allowedAttributes = allowedAttributes;
        this.deniedAttributes = deniedAttributes;
    }
    
    getColumns(attributes: string[], allAttributes=true) {
        let atts = attributes.slice();
        let cols = [];
        // allowed attributes
        this.allowedAttributes.forEach(att =>  {
            let idx = atts.indexOf(att);
            if (idx > -1) {
                cols.push(new ResultColumn(att, att, '', true));
                atts[idx] = null;
            }
        });
        // then other attributes
        if (allAttributes) {
            atts.forEach(att => {
                if (att != null && att[0] != '_' && this.deniedAttributes.indexOf(att) < 0) {
                    cols.push(new ResultColumn(att, att, '', false));
                }
            });           
        }        
        return cols;
    }
    
}

export function getResultType(name: string, resultTypes: {[name:string]: ResultType}): ResultType {
    let rt = resultTypes[name];
    if (rt == null) {
        rt = resultTypes['*'];
    }
    return rt;
}