Skip to content

Plugins

cvasseng edited this page Dec 19, 2017 · 10 revisions

Sample plugins can be found here.

Data Import Plugins (for importing data)

Data import plugins can be attached using highed.plugins.import.install(...).

These plugins add functionality to the Web Import system.

Example: Register a basic CSV plugin

highed.plugins.import.install('CSV', {
    description: "Standard CSV Import",
    treatAs: 'csv',
    fetchAs: 'text',
    filter: function (data, options, fn) {
        //Validate CSV here... 
        fn(wasInvalid, data);
    }
});

Example: Register a Socrata plugin

highed.plugins.import.install('Socrata', {
        description: 'Socrata is an open data format commonly used for various government sources. <a href="http://www.opendatanetwork.com/" target="_blank">http://www.opendatanetwork.com/</a>',
        treatAs: 'csv',
        fetchAs: 'json',
        defaultURL: 'https://finances.worldbank.org/resource/czdd-amke.json',
        options: {
            includeFields: {
                type: 'string',
                label: 'Fields to include, separate by whitespace',
                default: 'fiscal_year amount_us_millions_'                        
            }
        },
        filter: function (data, options, fn) {
            var csv = [], header = [];

            options.includeFields = highed.arrToObj(options.includeFields.split(' '));

            if (highed.isArr(data)) {

                //Only include things we're interested in
                data = data.map(function (d) {
                    var r = {};
                    Object.keys(options.includeFields).forEach(function (c) {
                        r[c] = d[c];
                    });
                    return r;
                });

                data.forEach(function (row, i) {
                    var rdata = [];                            
                    
                    Object.keys(row).forEach(function (key) {
                        var col = row[key];

                        if (!options.includeFields[key]) {
                            return;
                        }

                        if (i == 0) {
                            header.push(key);
                        }

                        rdata.push(col);
                        
                    });
                    csv.push(rdata.join(','));
                });
            }

            fn(false, [header.join(',')].concat(csv).join('\n'));
        }
);

To enable import plugins, add its name to the plugins array supplied when creating either the importer or the editor:

highed.DataImporter(.., {plugins: ['NAME']})
OR
highed.Editor(...,{importer: {plugins: ['NAME']}});

Export plugins

To add export plugins, call highed.plugins.export.install(<name>, <definition>).

Example

highed.plugins.export.install('My Plugin', {
    description: 'Description Test',
    //Custom options - will produce a UI
    options: {
        myOption: {
            type: 'string',
            label: 'Custom Option'
        }
    },
    //Function to call when performing an export
    //Chart is an instance of ChartPreview containing the current chart
    export: function (options, chart, fn) {
       //Do stuff, then call fn(). First argument is an error message or false
    }
});

Similarly to import plugins, plugins must be explicitly enabled when creating the editor:

highed.Exporter(.., {plugins: ['NAME']})
OR
highed.Editor(...,{exporter: {plugins: ['NAME']}});