All files / app/model template.ts

15% Statements 3/20
0% Branches 0/14
25% Functions 1/4
15.78% Lines 3/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 102117x 127x                                                                                               127x                                                                                                        
import { Schema } from 'jtd';
import { DateTime } from 'luxon';
import { toJS } from 'mobx';
import { Ext } from './ext';
import { Config, TagSort } from './tag';
import { Roles } from './user';
 
export interface Template extends Config {
  config?: Config['config'] & {
    /**
     * Do not render forms from inherited Templates. If unset forms
     * will stack from abstract to specific inheritance.
     */
    overrideForm?: boolean,
    /**
     * Show built-in custom view.
     */
    view?: string,
    /**
     * Override the view text used in a tab.
     */
    tab?: string,
    /**
     * Always use fully qualified tag when creating web links.
     */
    local?: boolean,
    /**
     * This view is available by default, no tagging required.
     */
    global?: boolean;
    /**
     * Submit text instead of links by default.
     */
    submitText?: boolean,
    /**
     * Add to the sort dropdown.
     */
    sorts?: SortConfig[],
  };
  // Client-only
  type?: 'template';
}
 
export interface SortConfig {
  sort: TagSort;
  label: string;
  title?: string;
}
 
export const templateSchema: Schema = {
  optionalProperties: {
    tag: { type: 'string' },
    name: { type: 'string' },
    config: {},
    defaults: {},
    schema: {},
  }
};
 
export function mapTemplate(obj: any): Template {
  obj.type = 'template';
  obj.tag ||= '';
  obj.origin ||= '';
  obj.modifiedString = obj.modified;
  obj.modified = obj.modified && DateTime.fromISO(obj.modified);
  return obj;
}
 
export function maybeTemplate(obj: any): Template | undefined {
  if (!obj) return undefined;
  return mapTemplate(obj);
}
 
export function writeTemplate(template: Template): Template {
  const result = { ...template };
  if (result.modifiedString) result.modified = result.modifiedString as any;
  delete result.type;
  delete result.upload;
  delete result.exists;
  delete result.outdated;
  delete result.modifiedString;
  delete result.config?._cache;
  return result;
}
 
export interface TemplateScope {
  [record: string]: any,
  ext: Ext;
  template: Template;
}
 
export function getTemplateScope(account: Roles, template: Template, ext: Ext, el?: Element, actions?: any): TemplateScope {
  return {
    el,
    ...actions || {},
    account: toJS(account),
    ext: toJS(ext),
    template: toJS(template),
    ...(ext.config || {}),
  };
}