1/**
2 * DokuWiki Plugin workflow browser script
3 *
4 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
5 * @author  Ryan Boder <ryan.boder@suretycam.com>
6 */
7
8var wfstate = null;
9var wfrows = null;
10
11function wfCount(obj) {
12    var i = 0;
13    for (var j in obj)
14	i++;
15    return i;
16}
17
18function readSessionObject(name) {
19    var str = jQuery.cookie(name);
20    if (str)
21	return jQuery.parseJSON(str);
22    else
23	return {};
24}
25
26function writeSessionObject(name, obj) {
27    jQuery.cookie(name, JSON.stringify(obj));
28}
29
30function wfRemember(name, value) {
31    wfstate[name] = value;
32    writeSessionObject('wfstate', wfstate);
33}
34
35function wfForget(name) {
36    delete wfstate[name];
37    writeSessionObject('wfstate', wfstate);
38}
39
40function wfForgetAll() {
41    wfstate = {};
42    writeSessionObject('wfstate', wfstate);
43}
44
45function wfRenderLink(target, script, text) {
46    return '<a class="wikilink1" href="doku.php?id=' + wfnamespace + ':' + target + '" onclick="' + script + '">' + text + '</a>';
47}
48
49function wfFilterRowsByState() {
50    wfrows.filter(function() {
51	var data = jQuery(this).attr('data-criteria');
52	if (data != null && data != '') {
53	    var criteria = data.split(':');
54	    for (var i in criteria) {
55		var crit = criteria[i].split('=');
56		var values = crit[1].split(',');
57		for (var j in wfstate) {
58		    // if we know state for this criteria and it does not match any valid values...
59		    if (crit[0] == j && values.indexOf(wfstate[j]) == -1) {
60			return true;
61		    }
62		}
63	    }
64	}
65        return false;
66    }).hide();
67}
68
69function wfFocusSearch() {
70    setTimeout(function (){
71	jQuery('#wfsearch').focus();
72    }, 400);
73}
74
75
76jQuery(document).ready(function() {
77    if (typeof wfnamespace == 'undefined')
78	return; // don't do anything if this isn't a workflow page
79
80    if (wfpageid == 'start') { // if this is the start page, store the state definitions for other pages to use
81	writeSessionObject('wfstatedefs', wfstatedefs);
82    } else { // otherwise, read the state definitions that were defined in the start page
83	wfstatedefs = readSessionObject('wfstatedefs');
84    }
85
86    // recall the current state in this session
87    wfstate = readSessionObject('wfstate');
88
89    // render the forget state links at the bottom of the decision link table
90    for (var name in wfstate) {
91	var link = wfRenderLink(wfpageid, 'wfForget(\'' + name + '\');', name + ' = ' + wfstate[name]);
92	jQuery('#wftable tr:last').after('<tr><td>Forget ' + link  + '</td></tr>');
93    }
94    if (wfCount(wfstate) > 0)
95	jQuery('#wftable tr:last').after('<tr><td>' + wfRenderLink('start', 'wfForgetAll();', 'Forget everything and start over.') + '</td></tr>');
96    jQuery('#wftable').append('<tr><td>' + wfRenderLink('start', '', 'Start over.') + '</td></tr>');
97
98    // render the remember state links at the bottom of the decision link table
99    for (var name in wfstatedefs) {
100	if (wfstate.hasOwnProperty(name))
101	    continue; // don't render a link if this state is already remembered
102	var values = wfstatedefs[name].split(',');
103	var links = '';
104	for (var i in values) {
105	    links += wfRenderLink(wfpageid, 'wfRemember(\'' + name + '\',\'' + values[i] + '\');', values[i]) + ' | ';
106	}
107	if (links.length > 0)
108	    links = links.slice(0, -3); // remove the extraneous vertical bar after the last value
109	jQuery('#wftable tr:last').after('<tr><td>Remember ' + name + ' = ' + links + '</td></tr>');
110    }
111
112    // select the rows after we've rendered the forget state links
113    wfrows = jQuery('#wftable tr');
114
115    // filter the decision table every time the user types in the search box
116    jQuery('#wfsearch').keyup(function() {
117	var val = '^(?=.*\\b' + jQuery.trim(jQuery(this).val()).split(/\s+/).join('\\b)(?=.*\\b') + ').*$', reg = RegExp(val, 'i'), text;
118	wfrows.show().filter(function() {
119            text = jQuery(this).text().replace(/\s+/g, ' ');
120            return !reg.test(text);
121	}).hide();
122	wfFilterRowsByState();
123    });
124
125    // filter out any decision links due to state criteria when the page first loads
126    wfFilterRowsByState();
127});
128