passbooker
Version:
Generates Apple Passboks
196 lines (137 loc) • 3.63 kB
JavaScript
;
/**
* Module dependencies
*/
var Hoek = require('hoek');
/**
* Config
*/
var defaultOptions = {
imagePath: './',
keysPath: './',
passCertName: 'pass.pem',
passKeytName: 'pass-key.pem',
wwdrCertName: 'wwdr.pem',
};
var defaultPass = {
formatVersion: 1,
passTypeIdentifier: 'pass.example.foo',
teamIdentifier: 'XXXXXXXXXX',
organizationName: 'Example Corp',
logoText: 'Example Corp',
description: 'Example Corp Pass',
labelColor: 'rgb(255, 255, 255)',
foregroundColor: 'rgb(255, 255, 255)',
backgroundColor: 'rgb( 0, 171, 237 )',
barcode: {}
};
var types = [
'boardingPass',
'coupon',
'eventTicket',
'storeCard',
'generic'
];
/**
* Template
*/
var Template = function fnTemplate( type, options ) {
if ( !type ) {
throw new Error('No `type` supplied!');
}
if ( types.indexOf(type) === -1 ) {
throw new Error('Unsupported `type` supplied!');
}
var that = this;
that.type = type;
that.options = Hoek.applyToDefaults( defaultOptions, options || {} );
that.headerFields = [];
that.primaryFields = [];
that.secondaryFields = [];
that.auxiliaryFields = [];
that.backFields = [];
that.barcode = {};
};
/**
* Pass object
*/
Template.prototype.getPass = function fn_getPass() {
var that = this;
var passObject = {
passTypeIdentifier: that.options.passTypeIdentifier,
teamIdentifier: that.options.teamIdentifier,
organizationName: that.options.organizationName,
logoText: that.options.logoText,
description: that.options.description,
barcode: that.barcode,
};
passObject[that.type] = {
headerFields: that.headerFields,
primaryFields: that.primaryFields,
secondaryFields: that.secondaryFields,
auxiliaryFields: that.auxiliaryFields,
backFields: that.backFields,
};
return Hoek.applyToDefaults( defaultPass, passObject );
};
/**
* Getter / Setter
*/
Template.prototype._setContentField = function fn_setContentField( fieldType, input ) {
var that = this;
if ( !input.value ) {
input.value = input.label;
input.label = null;
}
var fields = {};
that[fieldType].forEach(function( field ) {
fields[field.key] = field;
});
fields[input.key] = fields[input.key] || {};
fields[input.key].key = input.key;
fields[input.key].value = input.value;
fields[input.key].label = input.label;
if ( !input.label ) {
delete fields[input.key].label;
}
that[fieldType] = Object.keys( fields ).map(function( aKey ) {
return fields[aKey];
});
return that;
};
Template.prototype.setPrimaryField = function fnSetPrimaryField( key, label, value ) {
var that = this;
return that._setContentField( 'primaryFields', {
key: key, label: label, value: value
});
};
Template.prototype.setSecondaryField = function fnSetSecondaryField( key, label, value ) {
var that = this;
return that._setContentField( 'secondaryFields', {
key: key, label: label, value: value
});
};
Template.prototype.setAuxiliaryField = function fnSetAuxiliaryField( key, label, value ) {
var that = this;
return that._setContentField( 'auxiliaryFields', {
key: key, label: label, value: value
});
};
Template.prototype.setBackField = function fnSetBackField( key, label, value ) {
var that = this;
return that._setContentField( 'backFields', {
key: key, label: label, value: value
});
};
Template.prototype.setBarcode = function fn_setBarcode( options ) {
var that = this;
if ( !options ) {
return that;
}
that.barcode = options;
return that;
};
/**
* Export
*/
module.exports = Template;