Skip to content

Commit

Permalink
Merge pull request 3lvis#417 from hyperoslo/fix/field-section-positions
Browse files Browse the repository at this point in the history
Fix reset positions for sections and fields
  • Loading branch information
vadymmarkov committed May 26, 2015
2 parents 4a6b979 + c534f30 commit 9ef9d47
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Demos/Basic-ObjC/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ DEPENDENCIES:

EXTERNAL SOURCES:
Form:
:path: ../../
:path: "../../"

SPEC CHECKSUMS:
Form: 2a2f9ed0ee19f7b153b9b5a8bcf19811d028b847
Expand Down
26 changes: 25 additions & 1 deletion Source/FORMData.m
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,16 @@ - (void)sectionWithID:(NSString *)sectionID
}
}

- (void)updateHiddenSectionsPositionsInGroup:(FORMGroup *)group usingOffset:(NSInteger)offset withDelta:(NSInteger)delta {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"group = %@ && position > %ld", group, offset];

NSArray *hiddenSections = [[self.hiddenSections allValues] filteredArrayUsingPredicate:predicate];

[hiddenSections enumerateObjectsUsingBlock:^(FORMSection *section, NSUInteger idx, BOOL *stop) {
section.position = @([section.position integerValue] + delta);
}];
}

#pragma mark - Field

- (FORMField *)fieldWithID:(NSString *)fieldID
Expand Down Expand Up @@ -688,6 +698,10 @@ - (NSArray *)showTargets:(NSArray *)targets {
FORMGroup *group = self.groups[[section.group.position integerValue]];
[group.sections insertObject:section atIndex:sectionIndex];
[group resetSectionPositions];

[self updateHiddenSectionsPositionsInGroup:group
usingOffset:sectionIndex
withDelta:1];
}
}

Expand Down Expand Up @@ -743,6 +757,10 @@ - (NSArray *)hideTargets:(NSArray *)targets {
for (FORMField *field in section.fields) {
[self.values removeObjectForKey:field.fieldID];
}

[self updateHiddenSectionsPositionsInGroup:section.group
usingOffset:[section.position integerValue]
withDelta:-1];
}
}
}
Expand All @@ -767,17 +785,23 @@ - (NSArray *)hideTargets:(NSArray *)targets {
}];
}

NSMutableSet *resetSections = [NSMutableSet new];

for (FORMField *field in deletedFields) {
[self indexForFieldWithID:field.fieldID
inSectionWithID:field.section.sectionID
completion:^(FORMSection *section, NSInteger index) {
if (section) {
[section.fields removeObjectAtIndex:index];
[section resetFieldPositions];
[resetSections addObject:section];
}
}];
}

[resetSections enumerateObjectsUsingBlock:^(FORMSection *section, BOOL *stop) {
[section resetFieldPositions];
}];

for (FORMSection *section in deletedSections) {
FORMGroup *group = self.groups[[section.group.position integerValue]];

Expand Down
2 changes: 1 addition & 1 deletion Tests/Tests/FORMDataSourceTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ - (void)testIndexInForms {
[dataSource processTargets:@[[FORMTarget showFieldTargetWithID:@"username"]]];
field = [dataSource fieldWithID:@"username" includingHiddenFields:YES];
index = [field indexInSectionUsingGroups:dataSource.groups];
XCTAssertEqual(index, 1);
XCTAssertEqual(index, 2);
[dataSource processTargets:[FORMTarget showFieldTargetsWithIDs:@[@"first_name",
@"address"]]];

Expand Down
89 changes: 84 additions & 5 deletions Tests/Tests/FORMDataTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ - (void)testFormGenerationSectionPositionsWithHiddenTargets {
XCTAssertEqualObjects(field.position, @2);
}

- (void)testSectionPositionForHideAndShowTargets {
- (void)testSectionPositionForHideTargets {
NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"section-field-position.json"
inBundle:[NSBundle bundleForClass:[self class]]];

Expand All @@ -127,11 +127,90 @@ - (void)testSectionPositionForHideAndShowTargets {
[formData hideTargets:@[target]];
section = [formData sectionWithID:@"section-2"];
XCTAssertEqualObjects(section.position, @1);
}

target = [FORMTarget showSectionTargetWithID:@"section-1"];
[formData showTargets:@[target]];
section = [formData sectionWithID:@"section-2"];
XCTAssertEqualObjects(section.position, @2);
- (void)testSectionPositionForShowTargets {
NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"section-field-position.json"
inBundle:[NSBundle bundleForClass:[self class]]];

FORMData *formData = [[FORMData alloc] initWithJSON:JSON
initialValues:nil
disabledFieldIDs:nil
disabled:NO];

FORMSection *section = [formData sectionWithID:@"section-2"];
FORMTarget *target = [FORMTarget hideSectionTargetWithID:@"section-2"];

XCTAssertEqualObjects(section.position, @2);
[formData hideTargets:@[target]];
[formData showTargets:@[target]];
XCTAssertEqualObjects(section.position, @2);
}

- (void)testFieldPositionConsistency {
NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"forms.json"
inBundle:[NSBundle bundleForClass:[self class]]];
FORMData *formData = [[FORMData alloc] initWithJSON:JSON
initialValues:nil
disabledFieldIDs:nil
disabled:NO];

NSArray *hideFields = [FORMTarget hideFieldTargetsWithIDs:@[@"end_date", @"end_time"]];
NSArray *showFields = [FORMTarget showFieldTargetsWithIDs:@[@"end_date", @"end_time"]];
FORMField *endDate = [formData fieldWithID:@"end_date" includingHiddenFields:YES];
FORMField *endTime = [formData fieldWithID:@"end_time" includingHiddenFields:YES];
NSNumber *expectedEndDatePosition = endDate.position;
NSNumber *expectedEndTimePosition = endTime.position;

[formData hideTargets:hideFields];
XCTAssertEqualObjects(endDate.position, expectedEndDatePosition);
XCTAssertEqualObjects(endTime.position, expectedEndTimePosition);

[formData showTargets:showFields];
XCTAssertEqualObjects(endDate.position, expectedEndDatePosition);
XCTAssertEqualObjects(endTime.position, expectedEndTimePosition);

[formData hideTargets:hideFields];
XCTAssertEqualObjects(endDate.position, expectedEndDatePosition);
XCTAssertEqualObjects(endTime.position, expectedEndTimePosition);

[formData showTargets:showFields];
XCTAssertEqualObjects(endDate.position, expectedEndDatePosition);
XCTAssertEqualObjects(endTime.position, expectedEndTimePosition);
}

- (void)testSectionPositionConsistency {
NSArray *JSON = [NSJSONSerialization JSONObjectWithContentsOfFile:@"forms.json"
inBundle:[NSBundle bundleForClass:[self class]]];
FORMData *formData = [[FORMData alloc] initWithJSON:JSON
initialValues:nil
disabledFieldIDs:nil
disabled:NO];

NSArray *hideSections = [FORMTarget hideFieldTargetsWithIDs:@[@"personal-details-0", @"personal-details-1"]];
NSArray *showSections = [FORMTarget showFieldTargetsWithIDs:@[@"personal-details-0", @"personal-details-1"]];

FORMSection *firstSection = [formData sectionWithID:@"personal-details-0"];
FORMSection *secondSection = [formData sectionWithID:@"personal-details-1"];

NSNumber *expectedPersonalDetailsPosition = firstSection.position;
NSNumber *expectedSectiondPersonalDetailsPosition = secondSection.position;

[formData hideTargets:hideSections];
XCTAssertEqualObjects(firstSection.position, expectedPersonalDetailsPosition);
XCTAssertEqualObjects(secondSection.position, expectedSectiondPersonalDetailsPosition);

[formData showTargets:showSections];
XCTAssertEqualObjects(firstSection.position, expectedPersonalDetailsPosition);
XCTAssertEqualObjects(secondSection.position, expectedSectiondPersonalDetailsPosition);

[formData hideTargets:hideSections];
XCTAssertEqualObjects(firstSection.position, expectedPersonalDetailsPosition);
XCTAssertEqualObjects(secondSection.position, expectedSectiondPersonalDetailsPosition);

[formData showTargets:showSections];
XCTAssertEqualObjects(firstSection.position, expectedPersonalDetailsPosition);
XCTAssertEqualObjects(secondSection.position, expectedSectiondPersonalDetailsPosition);
}

- (void)testRequiredFields {
Expand Down
39 changes: 39 additions & 0 deletions Tests/Tests/JSONs/forms.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,24 +257,52 @@
{
"id":"start_date",
"title":"Start date",
"info":"The day of employment",
"type":"date",
"size":{
"width":25,
"height":1
}
},
{
"id":"start_time",
"title":"Start time",
"type":"time",
"size":{
"width":25,
"height":1
}
},
{
"id":"end_date",
"title":"End date",
"type":"date",
"validations":{
"compare_to":"start_date",
"compare_rule":">"
},
"size":{
"width":25,
"height":1
}
},
{
"id":"end_time",
"title":"End time",
"type":"time",
"size":{
"width":25,
"height":1
},
"validations":{
"required":true,
"min_length":2
}
},
{
"id":"contract_type",
"title":"Contract type",
"info":"Type of contract\n Permanent or temporary",
"type":"select",
"size":{
"width":50,
Expand All @@ -284,13 +312,19 @@
{
"id":0,
"title":"Permanent",
"info":"Regular employee",
"default":true,
"targets":[
{
"id":"end_date",
"type":"field",
"action":"show"
},
{
"id":"end_time",
"type":"field",
"action":"show"
},
{
"id":"employment-1",
"type":"section",
Expand All @@ -307,6 +341,11 @@
"type":"field",
"action":"hide"
},
{
"id":"end_time",
"type":"field",
"action":"hide"
},
{
"id":"employment-1",
"type":"section",
Expand Down

0 comments on commit 9ef9d47

Please sign in to comment.