griffin.yo icon indicating copy to clipboard operation
griffin.yo copied to clipboard

Add check for unless and if in renderElement

Open jgauffin opened this issue 6 years ago • 0 comments

The util method:

            ViewRenderer.prototype.getIfUnlessValue = function (name, data, directives) {
                if (directives === void 0) { directives = {}; }

                // control statements for arrays can't be anything else than if the array is empty or not
                if (data instanceof Array) {
                    return data;
                }
                // when we are on an unknown node (as data-unless/data-if is not counted when traversing structure)
                if (directives.hasOwnProperty(name)) {
                    directives = directives[name];
                }
                if (directives.hasOwnProperty("value")) {
                    data = directives["value"].apply(element, [data, this.dtoStack[this.dtoStack.length - 2]]);
                }
                
                if (data == null) {
                    return null;
                }
                if (data.hasOwnProperty(name)) {
                    data = data[name];
                }
                if (data instanceof Array && data.length == 0)
                    return null;

                return data;
            }

The control:

                    var unless = element.getAttribute('data-unless');
                    var self = this;
                    if (unless != null) {
                        var names = unless.split(',');
                        var gotValue = false;
                        names.forEach(function (name) {
                            if (self.getIfUnlessValue(name, data, directives))
                                gotValue = true;
                        });
                        element.style.display = gotValue ? "none" : "";
                    }

                    var ifValue = element.getAttribute('data-if');
                    if (ifValue != null) {
                        var names = ifValue.split(',');
                        var gotValue = false;
                        names.forEach(function (name) {
                            if (self.getIfUnlessValue(name, data, directives))
                                gotValue = true;
                        });
                        element.style.display = !gotValue ? "none" : "";
                    }

jgauffin avatar Sep 16 '17 18:09 jgauffin