Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Json field #4855

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion admin/public/styles/keystone/field-types.less
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@



// Code
// Code, JSON
// ------------------------------

.CodeMirror-container {
Expand Down
1 change: 1 addition & 0 deletions fields/types/json/JsonColumn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../text/TextColumn');
58 changes: 58 additions & 0 deletions fields/types/json/JsonField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Field from '../Field';
import React from 'react';
import { FormInput } from '../../../admin/client/App/elemental';


// See CodeMirror docs for API:
// http://codemirror.net/doc/manual.html

module.exports = Field.create({
displayName: 'JsonField',
statics: {
type: 'Json',
},
onJsonChanged (event) {
let jsonObj = JSON.parse(event.target.value);
this.props.valueChanged(jsonObj);
},
getJsonValue () {
if (this.props.value != null) {
return JSON.stringify(this.props.value, null, '\t');
}
return this.props.value;
},
renderValue () {
const { height } = this.props;

const styles = {
height: height,
whiteSpace: 'pre-wrap',
overflowY: 'auto',
};

return (
<FormInput multiline noedit style={styles}>{this.getJsonValue()}</FormInput>
);
},
renderField () {
const { height, path, style } = this.props;

const styles = {
height: height,
...style,
};

return (
<FormInput
autoComplete="off"
multiline
name={this.getInputName(path)}
onChange={this.onJsonChanged}
ref="focusTarget"
style={styles}
value={this.getJsonValue()}
/>
);
},

});
1 change: 1 addition & 0 deletions fields/types/json/JsonFilter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../text/TextFilter');
56 changes: 56 additions & 0 deletions fields/types/json/JsonType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var assign = require('object-assign');
var FieldType = require('../Type');
var TextType = require('../text/TextType');
var util = require('util');


/**
* HTML FieldType Constructor
* @extends Field
* @api public
*/
function json (list, path, options) {
this._nativeType = Object;
this._defaultSize = 'full';
this.height = options.height || 180;
this._properties = ['editor', 'height'];
this.codemirror = options.codemirror || {};
this.editor = assign({ mode: json }, this.codemirror);
json.super_.call(this, list, path, options);
}

json.properName = 'Json';
util.inherits(json, FieldType);

/* Inherit from TextType prototype */
json.prototype.addFilterToQuery = TextType.prototype.addFilterToQuery;


json.prototype.validateInput = function (data, required, item) {
var value = this.getValueFromData(data);

if (value === undefined && item && (item.get(this.path) || item.get(this.path) === 0)) {
return true;
}

if (value == null && required) {
return false;
} else if (value == null && !required) {
return true;
} else {
try {
value = this.getValueFromData(data, true);

if (typeof value !== 'object') {
return false;
} else {
return true;
}
} catch (ex) {
return false;
}
}
};

/* Export Field Type */
module.exports = json;
25 changes: 25 additions & 0 deletions fields/types/json/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Json Field
Stores a `Json string` in the model.

## Example

```js
{ type: Types.Json, height: 180}
```

## Options

`height` `Number`
The height of the field (in pixels). Default: 180


## Methods

### Inherits from [`Text`](../text)

* `addFilterToQuery`
* `validateInput`
* `validateRequiredInput`

## Filtering
Uses the same logic and filter UI as the [`Text`](../text) field type.
11 changes: 11 additions & 0 deletions fields/types/json/test/explorer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
Field: require('../JsonField'),
Filter: require('../JsonFilter'),
readme: require('fs').readFileSync('./fields/types/code/Readme.md', 'utf8'),
section: 'Text',
spec: {
label: 'Json',
path: 'json',
value: '{ name: \'hello world\'}',
},
};
66 changes: 66 additions & 0 deletions fields/types/json/test/type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const demand = require('must');
const JsonType = require('../JsonType');

exports.initList = function (List) {
List.add({
json: { type: JsonType },
nested: {
json: { type: JsonType },
},
});
};

exports.createData = function (List) { // eslint-disable-line no-unused-vars

};

exports.testFilters = function (List) { // eslint-disable-line no-unused-vars

};


exports.testFieldType = function (List) {
describe('updateItem', function () {
it('should update top level fields', function (done) {
var testItem = new List.model();

List.fields.json.updateItem(testItem, {
json: '{foo:bar}',
},
function () {
demand(testItem.json).be('{foo:bar}');
done();
});
});


it('should update nested fields', function (done) {
var testItem = new List.model();
List.fields['nested.json'].updateItem(testItem, {
nested: {
json: '{foo:bar}',
},
},
function () {
demand(testItem.nested.json).be('{foo:bar}');
done();
});
});


it('should update nested fields with flat paths', function (done) {
var testItem = new List.model();
List.fields['nested.json'].updateItem(testItem, {
'nested.json': '{foo:bar}',
},
function () {
demand(testItem.nested.json).be('{foo:bar}');
done();
});
});


});


};
1 change: 1 addition & 0 deletions lib/fieldTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var fields = {
get File () { return require('../fields/types/file/FileType'); },
get GeoPoint () { return require('../fields/types/geopoint/GeoPointType'); },
get Html () { return require('../fields/types/html/HtmlType'); },
get Json () { return require('../fields/types/json/JsonType'); },
get Key () { return require('../fields/types/key/KeyType'); },
get LocalFile () { return require('../fields/types/localfile/LocalFileType'); },
get LocalFiles () { return require('../fields/types/localfiles/LocalFilesType'); },
Expand Down