diff --git a/hugvey/story.py b/hugvey/story.py index e3fceab..c4fa31a 100644 --- a/hugvey/story.py +++ b/hugvey/story.py @@ -264,7 +264,9 @@ class Condition(object): condition.method = condition._hasMetTimeout if data['type'] == "variable": condition.method = condition._hasVariable - + if data['type'] == "diversion": + condition.method = condition._hasDiverged + if 'vars' in data: condition.vars = data['vars'] @@ -316,6 +318,24 @@ class Condition(object): r = not r return r + + def _hasDiverged(self, story) -> bool: + if not story.lastMsgFinishTime: + return False + + d = story.get(self.vars['diversionId']) + if not d: + story.logger.critical(f"Condition on non-existing diversion: {self.vars['diversionId']}") + + r = d.hasHit + if r: + story.logger.debug(f"Diversion {self.vars['diversionId']} has been hit.") + + if 'inverseMatch' in self.vars and self.vars['inverseMatch']: + # inverse: + r = not r + + return r def _hasMetReplyContains(self, story) -> bool: """ diff --git a/www/js/crel.min.js b/www/js/crel.min.js index 31e009d..739d68c 100644 --- a/www/js/crel.min.js +++ b/www/js/crel.min.js @@ -4,4 +4,20 @@ crel.attrMap['on'] = function(element, value) { for (var eventName in value) { element.addEventListener(eventName, value[eventName]); } +}; +crel.attrMap['options'] = function(element, values) { + if(element.tagName != "SELECT") { + return; + } + console.log(values); + if(Array.isArray(values)) { + for (let option of values) { + element.appendChild(crel('option', option)); + } + } else { + for (let option in values) { + element.appendChild(crel('option', {'value': option}, values[option])); + } + } + }; \ No newline at end of file diff --git a/www/js/hugvey_console.js b/www/js/hugvey_console.js index f9d3134..3cbaff5 100644 --- a/www/js/hugvey_console.js +++ b/www/js/hugvey_console.js @@ -1109,6 +1109,10 @@ class Graph { 'variable': { 'variable': { 'value': '','placeholder': "Variable name (without $)" }, 'notSet': { "label": "Not set", 'value': '', 'title': "Match if the variable is _not_ set.", 'type':'checkbox' }, + }, + 'diversion': { + 'diversionId': { 'tag': 'select', 'value': '','placeholder': "Variable name (without $)", 'options': this.diversions.map((d) => d['@id']) }, + 'inverseMatch': { "label": "Match if not done", 'value': '', 'title': "Match if the diversion has _not_ been done.", 'type':'checkbox' }, } }; } @@ -1118,6 +1122,8 @@ class Graph { let vars = this.getConditionTypes()[type]; for ( let v in vars ) { let attr = vars[v]; + let inputType = attr.hasOwnProperty('tag') ? attr['tag'] : 'input'; + attr['name'] = typeof conditionId == 'undefined' ? v : `${conditionId}-vars.${v}`; if(typeof values != 'undefined') { let value = this._getValueForPath(v, values); @@ -1138,7 +1144,7 @@ class Graph { crel( 'span', { 'title': attr.hasOwnProperty('title') ? attr['title'] : "" }, attr.hasOwnProperty('label') ? attr['label'] : v ), - crel( 'input', attr ) + crel( inputType, attr ) // crel('span', {'class': 'label-unit'}, attr.hasOwnProperty('unit') ? attr['unit'] : "" ) ) );