From 2ca9fab372488b1c39cfa96c0708a1a914efa8c3 Mon Sep 17 00:00:00 2001 From: Alexey Ivantsov Date: Tue, 28 Jul 2015 00:10:59 +0300 Subject: [PATCH] Implement evaluation of readonly field --- allcount-base.js | 21 +++++++++++++++++---- bower.json | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/allcount-base.js b/allcount-base.js index 2cdd1ae..438fc72 100644 --- a/allcount-base.js +++ b/allcount-base.js @@ -571,7 +571,16 @@ function fieldDirective(directiveName) { }) } - function renderField(fieldDescription, isEditor) { + function callReadOnly(fieldDescription, entity, isEditor) { + var expression = fieldDescription.isReadOnlyExpression; + if (expression && entity && isEditor) { + var entityCopy = JSON.parse(JSON.stringify(entity)); + return !eval("(" + expression+ ")(entityCopy)"); + } + return undefined; + } + + function renderField(fieldDescription, isEditor, entity) { if (!fieldDescription || fieldRenderingService.readOnlyFieldRenderer(fieldDescription) === false && fieldScope) { if (fieldScope) { fieldScope.isEditor = isEditor; @@ -580,7 +589,9 @@ function fieldDirective(directiveName) { } if (fieldScope) fieldScope.$destroy(); if (!fieldDescription) return; - isEditor = fieldDescription.isReadOnly ? false : isEditor; + var readOnlyExp = callReadOnly(fieldDescription, entity, isEditor); + isEditor = readOnlyExp != undefined ? readOnlyExp : (fieldDescription.isReadOnly ? false : isEditor); + fieldScope = scope.$new(); if (isEditor || fieldRenderingService.readOnlyFieldRenderer(fieldDescription) === false) { fieldScope.isEditor = isEditor; @@ -599,10 +610,12 @@ function fieldDirective(directiveName) { } scope.$watch(attrs.isEditor, function (isEditor) { - renderField(scope.$eval(attrs[directiveName]), isEditor); + var entity = scope.entity || scope.$eval("item"); + renderField(scope.$eval(attrs[directiveName]), isEditor, entity); }); scope.$watch(attrs[directiveName], function (fd) { - renderField(fd, scope.$eval(attrs.isEditor)); + var entity = scope.entity; + renderField(fd, scope.$eval(attrs.isEditor), entity); }) } } diff --git a/bower.json b/bower.json index 29cb2e6..7bc01ae 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "allcountjs-angular-base", - "version": "1.0.0", + "version": "1.0.1", "homepage": "http://allcountjs.com", "authors": [ "Pavel Tiunov " @@ -28,6 +28,6 @@ }, "repository": { "type": "git", - "url": "git://github.com/allcount/allcountjs-angular-base.git" + "url": "file:///Users/alexx/projects/allcount-base-fork/.git" } }