1jQuery(function() {
2    //Verify, if json-plugin is installed. Initialize it, if necessary.
3    if(typeof json_plugin !== 'object') {
4        return;
5    }
6    if(!json_plugin.initialized) {
7        json_plugin.init();
8    }
9
10    jQuery('.jsontable-plugin').each(function() {
11
12        var $tabs = jQuery(this),
13            $table = $tabs.find('.json-table'),
14            options = $tabs.data('options'),
15            saveall = $tabs.data('saveall'),
16            errors = [],
17            dataChanged = false,
18            o = $tabs.data('o'),    //object with interface, which was prepared by json_plugin
19            hot,
20            dataOriginal;
21
22
23        //prepare options and data for handsontable
24        if(typeof options !== 'object') {
25            try {
26                options = JSON.parse(options);
27            }
28            catch(e) {
29                errors.push("'options' attribute: " + String(e));
30                options = {};
31            }
32        }
33        else if (options === null) {
34            options = {};
35        }
36        options.data = JSON.parse(o.data_combined);
37        if(!options.hasOwnProperty('contextMenu')) {
38            options.contextMenu = true;
39        }
40
41
42        //initialize handsontable
43        hot = new Handsontable($table[0], options);
44
45
46        //prepare search field
47        if(options.hasOwnProperty('search')) {
48            var $search = jQuery('<input type="search" placeholder="Search"/>').insertBefore($table);
49            Handsontable.dom.addEvent($search[0], 'keyup', function (event) {
50                hot.getPlugin('search').query(this.value);
51                hot.render();
52            }, hot);
53        }
54
55
56        //display errors
57        if(errors.length) {
58            $table.before('<div class="json-table-error">' + errors.join('<br/>') + '</div>');
59        }
60
61
62        //show 'save' button once after the  first change. Register
63        //event the first time and each time, after json data are saved
64        //(button hides after successfull save).
65        function registerChangeEvent() {
66            Handsontable.hooks.once('afterChange', function() {
67                o.showButton();
68                dataChanged = true;
69            }, hot);
70        }
71        registerChangeEvent();
72        o.$button.on('jsonAfterSave', registerChangeEvent);
73
74
75        //write data into textarea as json string. From there data can be
76        //saved to dokuwiki by json plugin.
77        Handsontable.hooks.add('afterUnlisten', function () {
78            if(dataChanged) {
79                var dataToSave;
80
81                if(saveall) {
82                    dataToSave = options.data;
83                }
84                else {
85                    if(dataOriginal === undefined) {
86                        dataOriginal = JSON.parse(o.data_original);
87                    }
88                    dataToSave = json_plugin.diff(dataOriginal, options.data);
89                }
90                o.data_inline = JSON.stringify(dataToSave);
91            }
92        }, hot);
93    });
94
95});
96