Skip to content

Commit 73d3e45

Browse files
committed
Add ObjectPath as a provider
1 parent 266f386 commit 73d3e45

File tree

9 files changed

+48
-223
lines changed

9 files changed

+48
-223
lines changed

bower.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313
"moduleType": [
1414
"globals"
1515
],
16-
"keywords": ["angular","angularjs","form","json","json-schema","schema"],
16+
"keywords": [
17+
"angular",
18+
"angularjs",
19+
"form",
20+
"json",
21+
"json-schema",
22+
"schema"
23+
],
1724
"license": "MIT",
1825
"ignore": [
1926
"**/.*",
@@ -27,7 +34,8 @@
2734
"tv4": "~1.0.15",
2835
"pickadate": "~3.5.2",
2936
"angular-sanitize": "~1.2.18",
30-
"bootstrap-vertical-tabs": "~1.1.0"
37+
"bootstrap-vertical-tabs": "~1.1.0",
38+
"objectpath": "~1.0.2"
3139
},
3240
"devDependencies": {
3341
"angular-ui-ace": "bower"

dist/bootstrap-datepicker.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/schema-form.min.js

Lines changed: 16 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var deps = [];
1+
var deps = ['ObjectPath'];
22
try {
33
//This throws an expection if module does not exist.
44
angular.module('ngSanitize');
@@ -13,98 +13,6 @@ try {
1313

1414
angular.module('schemaForm',deps);
1515

16-
17-
'use strict';
18-
19-
;!function(undefined) {
20-
21-
var ObjectPath = {
22-
parse: function(str){
23-
if(typeof str !== 'string'){
24-
throw new TypeError('ObjectPath.parse must be passed a string');
25-
}
26-
27-
var i = 0;
28-
var parts = [];
29-
var d, b, q, c;
30-
while (i < str.length){
31-
d = str.indexOf('.', i);
32-
b = str.indexOf('[', i);
33-
34-
// we've reached the end
35-
if (d === -1 && b === -1){
36-
parts.push(str.slice(i, str.length));
37-
i = str.length;
38-
}
39-
40-
// dots
41-
else if (b === -1 || (d !== -1 && d < b)) {
42-
parts.push(str.slice(i, d));
43-
i = d + 1;
44-
}
45-
46-
// brackets
47-
else {
48-
if (b > i){
49-
parts.push(str.slice(i, b));
50-
i = b;
51-
}
52-
q = str.slice(b+1, b+2);
53-
if (q !== '"' && q !=='\'') {
54-
c = str.indexOf(']', b);
55-
if (c === -1) c = str.length;
56-
parts.push(str.slice(i + 1, c));
57-
i = (str.slice(c + 1, c + 2) === '.') ? c + 2 : c + 1;
58-
} else {
59-
c = str.indexOf(q+']', b);
60-
if (c === -1) c = str.length;
61-
while (str.slice(c - 1, c) === '\\' && b < str.length){
62-
b++;
63-
c = str.indexOf(q+']', b);
64-
}
65-
parts.push(str.slice(i + 2, c).replace(new RegExp('\\'+q,'g'), q));
66-
i = (str.slice(c + 2, c + 3) === '.') ? c + 3 : c + 2;
67-
}
68-
}
69-
}
70-
return parts;
71-
},
72-
73-
// root === true : auto calculate root; must be dot-notation friendly
74-
// root String : the string to use as root
75-
stringify: function(arr, quote){
76-
77-
if(Array.isArray(arr) !== true)
78-
arr = [arr.toString()];
79-
80-
quote = quote === '"' ? '"' : '\'';
81-
82-
return arr.slice().map(function(n){ return '[' + quote + (n.toString()).replace(new RegExp(quote, 'g'), '\\' + quote) + quote + ']'; }).join('');
83-
},
84-
85-
normalize: function(str){
86-
return this.stringify(this.parse(str));
87-
}
88-
};
89-
90-
// AMD
91-
if (typeof define === 'function' && define.amd) {
92-
define(function() {
93-
return ObjectPath;
94-
});
95-
}
96-
97-
// CommonJS
98-
else if (typeof exports === 'object') {
99-
exports.ObjectPath = ObjectPath;
100-
}
101-
102-
// Browser global.
103-
else {
104-
window.ObjectPath = ObjectPath;
105-
}
106-
}();
107-
10816
/**
10917
* @ngdoc service
11018
* @name sfSelect
@@ -125,7 +33,7 @@ angular.module('schemaForm',deps);
12533
* @returns {Any|undefined} returns the value at the end of the projection path
12634
* or undefined if there is none.
12735
*/
128-
angular.module('schemaForm').factory('sfSelect', [function () {
36+
angular.module('schemaForm').factory('sfSelect', ['ObjectPath', function (ObjectPath) {
12937
var numRe = /^\d+$/;
13038

13139
return function(projection, obj, valueToSet) {
@@ -178,7 +86,7 @@ angular.module('schemaForm').factory('sfSelect', [function () {
17886
};
17987
}]);
18088

181-
angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider',function($compileProvider){
89+
angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider','ObjectPathProvider',function($compileProvider, ObjectPathProvider){
18290
var defaultDecorator = '';
18391
var directives = {};
18492

@@ -231,7 +139,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
231139
//for fieldsets to recurse properly.
232140
var url = templateUrl(name,form);
233141
$http.get(url,{ cache: $templateCache }).then(function(res){
234-
var key = form.key ? ObjectPath.stringify(form.key).replace(/"/g, '&quot;') : '';
142+
var key = form.key ? ObjectPathProvider.stringify(form.key).replace(/"/g, '&quot;') : '';
235143
var template = res.data.replace(/\$\$value\$\$/g,'model'+key);
236144
$compile(template)(scope,function(clone){
237145
element.replaceWith(clone);
@@ -484,7 +392,7 @@ angular.module('schemaForm').provider('schemaFormDecorators',['$compileProvider'
484392
* This service is not that useful outside of schema form directive
485393
* but makes the code more testable.
486394
*/
487-
angular.module('schemaForm').provider('schemaForm',[function(){
395+
angular.module('schemaForm').provider('schemaForm',['ObjectPathProvider', function(ObjectPathProvider){
488396

489397
var defaultFormDefinition = function(name,schema,options){
490398
var rules = defaults[schema.type];
@@ -526,7 +434,7 @@ angular.module('schemaForm').provider('schemaForm',[function(){
526434
var f = stdFormObj(schema,options);
527435
f.key = options.path;
528436
f.type = 'text';
529-
options.lookup[ObjectPath.stringify(options.path)] = f;
437+
options.lookup[ObjectPathProvider.stringify(options.path)] = f;
530438
return f;
531439
}
532440
};
@@ -538,7 +446,7 @@ angular.module('schemaForm').provider('schemaForm',[function(){
538446
var f = stdFormObj(schema,options);
539447
f.key = options.path;
540448
f.type = 'number';
541-
options.lookup[ObjectPath.stringify(options.path)] = f;
449+
options.lookup[ObjectPathProvider.stringify(options.path)] = f;
542450
return f;
543451
}
544452
};
@@ -548,7 +456,7 @@ angular.module('schemaForm').provider('schemaForm',[function(){
548456
var f = stdFormObj(schema,options);
549457
f.key = options.path;
550458
f.type = 'number';
551-
options.lookup[ObjectPath.stringify(options.path)] = f;
459+
options.lookup[ObjectPathProvider.stringify(options.path)] = f;
552460
return f;
553461
}
554462
};
@@ -558,7 +466,7 @@ angular.module('schemaForm').provider('schemaForm',[function(){
558466
var f = stdFormObj(schema,options);
559467
f.key = options.path;
560468
f.type = 'checkbox';
561-
options.lookup[ObjectPath.stringify(options.path)] = f;
469+
options.lookup[ObjectPathProvider.stringify(options.path)] = f;
562470
return f;
563471
}
564472
};
@@ -575,7 +483,7 @@ angular.module('schemaForm').provider('schemaForm',[function(){
575483
f.titleMap[name] = name;
576484
});
577485
}
578-
options.lookup[ObjectPath.stringify(options.path)] = f;
486+
options.lookup[ObjectPathProvider.stringify(options.path)] = f;
579487
return f;
580488
}
581489
};
@@ -591,7 +499,7 @@ angular.module('schemaForm').provider('schemaForm',[function(){
591499
f.titleMap[name] = name;
592500
});
593501
}
594-
options.lookup[ObjectPath.stringify(options.path)] = f;
502+
options.lookup[ObjectPathProvider.stringify(options.path)] = f;
595503
return f;
596504
}
597505
};
@@ -604,13 +512,13 @@ angular.module('schemaForm').provider('schemaForm',[function(){
604512
var f = stdFormObj(schema,options);
605513
f.type = 'fieldset';
606514
f.items = [];
607-
options.lookup[ObjectPath.stringify(options.path)] = f;
515+
options.lookup[ObjectPathProvider.stringify(options.path)] = f;
608516

609517
//recurse down into properties
610518
angular.forEach(schema.properties,function(v,k){
611519
var path = options.path.slice();
612520
path.push(k);
613-
if (options.ignore[ObjectPath.stringify(path)] !== true) {
521+
if (options.ignore[ObjectPathProvider.stringify(path)] !== true) {
614522
var required = schema.required && schema.required.indexOf(k) !== -1;
615523

616524
var def = defaultFormDefinition(k,v,{
@@ -636,7 +544,7 @@ angular.module('schemaForm').provider('schemaForm',[function(){
636544
var f = stdFormObj(schema,options);
637545
f.type = 'array';
638546
f.key = options.path;
639-
options.lookup[ObjectPath.stringify(options.path)] = f;
547+
options.lookup[ObjectPathProvider.stringify(options.path)] = f;
640548

641549
var required = schema.required && schema.required.indexOf(options.path(options.path.length - 1)) !== -1;
642550

@@ -772,9 +680,9 @@ angular.module('schemaForm').provider('schemaForm',[function(){
772680
//extend with std form from schema.
773681
if (obj.key) {
774682
if(typeof obj.key == 'string') {
775-
obj.key = ObjectPath.parse(obj.key);
683+
obj.key = ObjectPathProvider.parse(obj.key);
776684
}
777-
var str = ObjectPath.stringify(obj.key);
685+
var str = ObjectPathProvider.stringify(obj.key);
778686
if(lookup[str]){
779687
return angular.extend(lookup[str],obj);
780688
}

examples/bootstrap-example.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ <h3>Schema</h3>
6565
<script type="text/javascript" src="../bower_components/angular-sanitize/angular-sanitize.min.js"></script>
6666
<script type="text/javascript" src="../bower_components/angular-ui-sortable/sortable.js"></script>
6767
<script type="text/javascript" src="../bower_components/angular-ui-ace/ui-ace.js"></script>
68+
<script type="text/javascript" src="../bower_components/objectpath/lib/ObjectPath.js"></script>
6869
<script type="text/javascript" src="../bower_components/pickadate/lib/picker.js"></script>
6970
<script type="text/javascript" src="../bower_components/pickadate/lib/picker.date.js"></script>
7071
<script type="text/javascript" src="../bower_components/pickadate/lib/translations/sv_SE.js"></script>
@@ -73,7 +74,7 @@ <h3>Schema</h3>
7374
<script type="text/javascript" src="../dist/bootstrap-datepicker.min.js"></script>
7475
<script type="text/javascript">
7576

76-
angular.module('test',['schemaForm','ui.ace']);
77+
angular.module('test',['schemaForm','ui.ace','ObjectPath']);
7778

7879
function TestCtrl($scope,$http){
7980

src/directives/decorators/bootstrap/datepicker/bootstrap-datepicker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
angular.module('schemaForm').config(
2-
['schemaFormProvider','schemaFormDecoratorsProvider',
3-
function(schemaFormProvider, schemaFormDecoratorsProvider){
2+
['schemaFormProvider','schemaFormDecoratorsProvider','ObjectPathProvider',
3+
function(schemaFormProvider, schemaFormDecoratorsProvider, ObjectPathProvider){
44

55
var datepicker = function(name,schema,options) {
66
if (schema.type === 'string' && schema.format == "date") {
77
var f = schemaFormProvider.stdFormObj(schema,options);
88
f.key = options.path;
99
f.type = 'datepicker';
10-
options.lookup[ObjectPath.stringify(options.path)] = f;
10+
options.lookup[ObjectPathProvider.stringify(options.path)] = f;
1111
return f;
1212
}
1313
};

src/module.js

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var deps = [];
1+
var deps = ['ObjectPath'];
22
try {
33
//This throws an expection if module does not exist.
44
angular.module('ngSanitize');
@@ -12,95 +12,3 @@ try {
1212
} catch (e) {}
1313

1414
angular.module('schemaForm',deps);
15-
16-
17-
'use strict';
18-
19-
;!function(undefined) {
20-
21-
var ObjectPath = {
22-
parse: function(str){
23-
if(typeof str !== 'string'){
24-
throw new TypeError('ObjectPath.parse must be passed a string');
25-
}
26-
27-
var i = 0;
28-
var parts = [];
29-
var d, b, q, c;
30-
while (i < str.length){
31-
d = str.indexOf('.', i);
32-
b = str.indexOf('[', i);
33-
34-
// we've reached the end
35-
if (d === -1 && b === -1){
36-
parts.push(str.slice(i, str.length));
37-
i = str.length;
38-
}
39-
40-
// dots
41-
else if (b === -1 || (d !== -1 && d < b)) {
42-
parts.push(str.slice(i, d));
43-
i = d + 1;
44-
}
45-
46-
// brackets
47-
else {
48-
if (b > i){
49-
parts.push(str.slice(i, b));
50-
i = b;
51-
}
52-
q = str.slice(b+1, b+2);
53-
if (q !== '"' && q !=='\'') {
54-
c = str.indexOf(']', b);
55-
if (c === -1) c = str.length;
56-
parts.push(str.slice(i + 1, c));
57-
i = (str.slice(c + 1, c + 2) === '.') ? c + 2 : c + 1;
58-
} else {
59-
c = str.indexOf(q+']', b);
60-
if (c === -1) c = str.length;
61-
while (str.slice(c - 1, c) === '\\' && b < str.length){
62-
b++;
63-
c = str.indexOf(q+']', b);
64-
}
65-
parts.push(str.slice(i + 2, c).replace(new RegExp('\\'+q,'g'), q));
66-
i = (str.slice(c + 2, c + 3) === '.') ? c + 3 : c + 2;
67-
}
68-
}
69-
}
70-
return parts;
71-
},
72-
73-
// root === true : auto calculate root; must be dot-notation friendly
74-
// root String : the string to use as root
75-
stringify: function(arr, quote){
76-
77-
if(Array.isArray(arr) !== true)
78-
arr = [arr.toString()];
79-
80-
quote = quote === '"' ? '"' : '\'';
81-
82-
return arr.slice().map(function(n){ return '[' + quote + (n.toString()).replace(new RegExp(quote, 'g'), '\\' + quote) + quote + ']'; }).join('');
83-
},
84-
85-
normalize: function(str){
86-
return this.stringify(this.parse(str));
87-
}
88-
};
89-
90-
// AMD
91-
if (typeof define === 'function' && define.amd) {
92-
define(function() {
93-
return ObjectPath;
94-
});
95-
}
96-
97-
// CommonJS
98-
else if (typeof exports === 'object') {
99-
exports.ObjectPath = ObjectPath;
100-
}
101-
102-
// Browser global.
103-
else {
104-
window.ObjectPath = ObjectPath;
105-
}
106-
}();

0 commit comments

Comments
 (0)