webstaff: add PredictionPattern.compile() method
authorGalen Charlton <gmc@equinoxinitiative.org>
Tue, 2 May 2017 22:00:13 +0000 (18:00 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Tue, 30 May 2017 16:06:40 +0000 (12:06 -0400)
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/web/js/ui/default/staff/serials/directives/prediction_wizard.js

index 8cb0546..01e8e3b 100644 (file)
@@ -250,6 +250,116 @@ function($scope , $q , egSerialsCoreSvc , egCore , egGridDataProvider) {
             }
         }
 
+        // return current pattern compiled to subfield list
+        this.compile = function() {
+            var patternCode = [];
+            patternCode.push(self.compress_expand);
+            patternCode.push(self.caption_evaluation);
+            patternCode.push('8');
+            patternCode.push(self.link);
+            if (self.use_enum) {
+                for (var i = 0; i < self.enum_levels.length; i++) {
+                    patternCode.push(['a', 'b', 'c', 'd', 'e', 'f'][i]);
+                    patternCode.push(self.enum_levels[i].caption);
+                    if (i > 0 && self.enum_levels[i].units_per_next_higher) {
+                        patternCode.push('u');
+                        if (self.enum_levels[i].units_per_next_higher.type == 'number') {
+                            patternCode.push(self.enum_levels[i].units_per_next_higher.value);
+                        } else {
+                            patternCode.push(self.enum_levels[i].units_per_next_higher.type);
+                        }
+                    }
+                    if (i > 0 && self.enum_levels[i].restart != null) {
+                        patternCode.push('v');
+                        patternCode.push(self.enum_levels[i].restart ? 'r' : 'c');
+                    }
+                }
+            }
+            if (self.use_alt_enum) {
+                for (var i = 0; i < self.alt_enum_levels.length; i++) {
+                    patternCode.push(['g','h'][i]);
+                    patternCode.push(self.alt_enum_levels[i].caption);
+                    if (i > 0 && self.alt_enum_levels[i].units_per_next_higher) {
+                        patternCode.push('u');
+                        if (self.alt_enum_levels[i].units_per_next_higher.type == 'number') {
+                            patternCode.push(self.alt_enum_levels[i].units_per_next_higher.value);
+                        } else {
+                            patternCode.push(self.alt_enum_levels[i].units_per_next_higher.type);
+                        }
+                    }
+                    if (i > 0 && self.alt_enum_levels[i].restart != null) {
+                        patternCode.push('v');
+                        patternCode.push(self.alt_enum_levels[i].restart ? 'r' : 'c');
+                    }
+                }
+            }
+            var chron_sfs = (self.use_enum) ? ['i', 'j', 'k', 'l'] : ['a', 'b', 'c', 'd'];
+            if (self.use_chron) {
+                for (var i = 0; i < self.chron_levels.length; i++) {
+                    patternCode.push(chron_sfs[i],
+                        self.chron_levels[i].display_caption ?
+                           self.chron_levels[i].caption :
+                           '(' + self.chron_levels[i].caption + ')'
+                    );
+                }
+            }
+            var alt_chron_sf = (self.use_enum) ? 'm' : 'g';
+            if (self.use_alt_chron) {
+                patternCode.push(alt_chron_sf,
+                    self.alt_chron_levels[0].display_caption ?
+                       self.alt_chron_levels[0].caption :
+                       '(' + self.alt_chron_levels[0].caption + ')'
+                );
+            }
+            // frequency
+            patternCode.push('w',
+                self.frequency_type == 'numeric' ?
+                    self.frequency_numeric :
+                    self.frequency_preset
+            );
+            // calendar change
+            if (self.use_calendar_change) {
+                patternCode.push('x');
+                patternCode.push(self.calendar_change.map(function(chg) {
+                    if (chg.type == 'season') {
+                        return chg.season;
+                    } else if (chg.type == 'month') {
+                        return chg.month;
+                    } else if (chg.type == 'date') {
+                        return chg.month + chg.day;
+                    }
+                }).join(','));
+            }
+            // regularity
+            if (self.use_regularity) {
+                self.regularity.forEach(function(reg) {
+                    patternCode.push('y');
+                    var val = reg.regularity_type + reg.chron_type;
+                    val += reg.parts.map(function(part) {
+                        if (reg.chron_type == 'd') {
+                            return part[part.sub_type];
+                        } else if (reg.chron_type == 'm') {
+                            return part.month;
+                        } else if (reg.chron_type == 'w') {
+                            if (part.sub_type == 'week_in_month') {
+                                return part.week + part.month;
+                            } else if (part.sub_type == 'week_day') {
+                                return part.week + part.day;
+                            } else if (part.sub_type == 'week_day_in_month') {
+                                return part.month + part.week + part.day;
+                            }
+                        } else if (reg.chron_type == 's') {
+                            return part.season;
+                        } else if (reg.chron_type == 'y') {
+                            return part.year;
+                        }
+                    }).join(',');
+                    patternCode.push(val);
+                });
+            }
+            return JSON.stringify(patternCode);
+        }
+
         this.add_enum_level = function() {
             if (self.enum_levels.length < 6) {
                 self.enum_levels.push({