Skip to content

Commit 1992eea

Browse files
committed
fix(form) Puts conditional display logic into form markup plugin so directives get it
1 parent 1050753 commit 1992eea

File tree

2 files changed

+49
-54
lines changed

2 files changed

+49
-54
lines changed

js/directives/form.js

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,6 @@ formsAngular
5050
var subkeys = [];
5151
var tabsSetup = false;
5252

53-
var generateNgShow = function (showWhen, model) {
54-
55-
function evaluateSide(side) {
56-
var result = side;
57-
if (typeof side === 'string') {
58-
if (side.slice(0, 1) === '$') {
59-
result = (model || 'record') + '.';
60-
var parts = side.slice(1).split('.');
61-
if (parts.length > 1) {
62-
var lastBit = parts.pop();
63-
result += parts.join('.') + '[$index].' + lastBit;
64-
} else {
65-
result += side.slice(1);
66-
}
67-
} else {
68-
result = '\'' + side + '\'';
69-
}
70-
}
71-
return result;
72-
}
73-
74-
var conditionText = ['eq', 'ne', 'gt', 'gte', 'lt', 'lte'],
75-
conditionSymbols = ['===', '!==', '>', '>=', '<', '<='],
76-
conditionPos = conditionText.indexOf(showWhen.comp);
77-
78-
if (conditionPos === -1) { throw new Error('Invalid comparison in showWhen'); }
79-
return evaluateSide(showWhen.lhs) + conditionSymbols[conditionPos] + evaluateSide(showWhen.rhs);
80-
};
81-
8253
var generateInput = function (fieldInfo, modelString, isRequired, idString, options) {
8354
var nameString;
8455
if (!modelString) {
@@ -300,30 +271,7 @@ formsAngular
300271
};
301272

302273
var handleField = function (info, options) {
303-
var includeIndex = false;
304-
var insert = '';
305-
if (options.index) {
306-
try {
307-
parseInt(options.index);
308-
includeIndex = true;
309-
} catch (err) {
310-
// Nothing to do
311-
}
312-
}
313-
if (info.showWhen) {
314-
if (typeof info.showWhen === 'string') {
315-
insert += 'ng-show="' + info.showWhen + '"';
316-
} else {
317-
insert += 'ng-show="' + generateNgShow(info.showWhen, options.model) + '"';
318-
}
319-
}
320-
if (includeIndex) {
321-
insert += ' id="cg_' + info.id.replace('_', '-' + attrs.index + '-') + '"';
322-
} else {
323-
insert += ' id="cg_' + info.id.replace(/\./g, '-') + '"';
324-
}
325-
326-
var fieldChrome = formMarkupHelper.fieldChrome(scope, info, options, insert);
274+
var fieldChrome = formMarkupHelper.fieldChrome(scope, info, options);
327275
var template = fieldChrome.template;
328276

329277
if (info.schema) {
@@ -435,6 +383,10 @@ formsAngular
435383
// var processInstructions = function (instructionsArray, topLevel, groupId) {
436384
// removing groupId as it was only used when called by containerType container, which is removed for now
437385
var processInstructions = function (instructionsArray, topLevel, options) {
386+
if (options.index) {
387+
alert('Found where options index is used'); // This is tested for shen generating field chrome, but cannot see how it is ever generated. Redundant? AM removing 9/2/15
388+
throw new Error('Found where options index is used');
389+
}
438390
var result = '';
439391
if (instructionsArray) {
440392
for (var anInstruction = 0; anInstruction < instructionsArray.length; anInstruction++) {

js/services/form-markup-helper.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,57 @@ formsAngular.factory('formMarkupHelper', [
55
function (cssFrameworkService, inputSizeHelper, addAllService) {
66
var exports = {};
77

8+
function generateNgShow(showWhen, model) {
9+
10+
function evaluateSide(side) {
11+
var result = side;
12+
if (typeof side === 'string') {
13+
if (side.slice(0, 1) === '$') {
14+
result = (model || 'record') + '.';
15+
var parts = side.slice(1).split('.');
16+
if (parts.length > 1) {
17+
var lastBit = parts.pop();
18+
result += parts.join('.') + '[$index].' + lastBit;
19+
} else {
20+
result += side.slice(1);
21+
}
22+
} else {
23+
result = '\'' + side + '\'';
24+
}
25+
}
26+
return result;
27+
}
28+
29+
var conditionText = ['eq', 'ne', 'gt', 'gte', 'lt', 'lte'],
30+
conditionSymbols = ['===', '!==', '>', '>=', '<', '<='],
31+
conditionPos = conditionText.indexOf(showWhen.comp);
32+
33+
if (conditionPos === -1) { throw new Error('Invalid comparison in showWhen'); }
34+
return evaluateSide(showWhen.lhs) + conditionSymbols[conditionPos] + evaluateSide(showWhen.rhs);
35+
}
36+
37+
838
exports.isHorizontalStyle = function (formStyle) {
939
return (!formStyle || formStyle === 'undefined' || ['vertical', 'inline'].indexOf(formStyle) === -1);
1040
};
1141

12-
exports.fieldChrome = function (scope, info, options, insert) {
42+
exports.fieldChrome = function (scope, info, options) {
1343
var classes = '';
1444
var template = '';
1545
var closeTag = '';
46+
var insert = '';
47+
48+
info.showWhen = info.showWhen || info.showwhen; // deal with use within a directive
49+
50+
if (info.showWhen) {
51+
if (typeof info.showWhen === 'string') {
52+
insert += 'ng-show="' + info.showWhen + '"';
53+
} else {
54+
insert += 'ng-show="' + generateNgShow(info.showWhen, options.model) + '"';
55+
}
56+
}
57+
insert += ' id="cg_' + info.id.replace(/\./g, '-') + '"';
58+
1659

1760
if (cssFrameworkService.framework() === 'bs3') {
1861
classes = 'form-group';

0 commit comments

Comments
 (0)