Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

alaska-field-relationship

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

alaska-field-relationship - npm Package Compare versions

Comparing version 0.11.24 to 0.12.0

114

index.js

@@ -1,36 +0,27 @@

// @flow
'use strict';
/* eslint global-require:0 */
/* eslint import/no-dynamic-require:0 */
Object.defineProperty(exports, "__esModule", {
value: true
});
import { Field } from 'alaska';
import mongoose from 'mongoose';
var _alaska = require('alaska');
const TypeObjectId = mongoose.Schema.Types.ObjectId;
const ObjectId = mongoose.Types.ObjectId;
var _alaska2 = _interopRequireDefault(_alaska);
export default class RelationshipField extends Field {
static plain = TypeObjectId;
static viewOptions = ['filters', 'service', 'model', 'multi', 'checkbox', 'switch', function (options, field) {
let Model = field.ref;
if (Model) {
options.ref = Model.path;
options.title = Model.title;
if (!options.service && Model.service) {
options.service = Model.service.id;
}
}
}];
static defaultOptions = {
cell: 'RelationshipFieldCell',
view: 'RelationshipFieldView',
filter: 'RelationshipFieldFilter',
defaultValue: ''
};
var _mongoose = require('mongoose');
service: string;
model: string;
ref: void | Class < Alaska$Model >;
optional: boolean;
var _mongoose2 = _interopRequireDefault(_mongoose);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const TypeObjectId = _mongoose2.default.Schema.Types.ObjectId;
const { ObjectId } = _mongoose2.default.Types;
class RelationshipField extends _alaska.Field {
/**

@@ -42,14 +33,17 @@ * 初始化Schema

let model = this._model;
let dataType = this.dataType;
let { dataType, ref } = this;
if (dataType === 'ObjectId' || dataType === ObjectId) {
dataType = TypeObjectId;
}
let ref = this.ref;
if (typeof ref === 'string') {
//查找引用模型
//ref 当this.optional为true时,ref有可能为null
ref = model.service.model(ref, this.optional);
if (this.optional && !model.service.hasModel(ref)) {
ref = null;
} else {
ref = model.service.getModel(ref);
}
}
let options: Indexed = {};
let options = {};
let type;

@@ -63,7 +57,10 @@ if (ref) {

if (type.type) {
// eslint-disable-next-line
type = type.type;
}
if (typeof type === 'string') {
// $Flow
type = require('alaska-field-' + type).default;
type = _alaska2.default.modules.fields['alaska-field-' + type];
if (!type) {
_alaska2.default.panic(`Field type 'alaska-field-${type}' not found!`);
}
}

@@ -79,3 +76,3 @@ if (type.plain) {

type,
ref: ref.name
ref: ref.modelName
};

@@ -87,3 +84,3 @@

}
this.model = ref.name;
this.model = ref.modelName;
} else {

@@ -96,3 +93,5 @@ //如果没有找到引用,说明是可选引用

let arr = this.ref.split('.');
// eslint-disable-next-line prefer-destructuring
this.model = arr[1];
// eslint-disable-next-line prefer-destructuring
this.service = arr[0];

@@ -102,20 +101,11 @@ }

options.set = (value) => {
options.set = value => {
if (value === '' && type === TypeObjectId) {
return undefined;
}
// eslint-disable-next-line
return value;
};
[
'get',
'set',
'default',
'index',
'unique',
'text',
'sparse',
'required',
'select'
].forEach((key) => {
['get', 'set', 'default', 'index', 'unique', 'text', 'sparse', 'required', 'select'].forEach(key => {
if (this[key] !== undefined) {

@@ -126,2 +116,3 @@ options[key] = this[key];

// $Flow
this.ref = ref;

@@ -140,3 +131,3 @@ // $Flow

}
let id = record.id;
let { id } = record;
if (id && String(id) === String(record.get(field.path))) {

@@ -151,3 +142,3 @@ next(new Error('Can not relate to record self, ' + model.path + '#' + field.path));

createFilter(filter: Object): any {
createFilter(filter) {
let value = filter;

@@ -159,2 +150,3 @@ let inverse = false;

}
// eslint-disable-next-line prefer-destructuring
value = filter.value;

@@ -180,3 +172,3 @@ if (filter.inverse === true || filter.inverse === 'true') {

value = parseInt(value);
if (isNaN(value)) return null;
if (_lodash2.default.isNaN(value)) return null;
return inverse ? { $ne: value } : value;

@@ -187,1 +179,19 @@ }

}
exports.default = RelationshipField;
RelationshipField.plain = TypeObjectId;
RelationshipField.viewOptions = ['filters', 'service', 'model', 'multi', 'checkbox', 'switch', (options, field) => {
let Model = field.ref;
if (Model) {
options.ref = Model.path;
options.title = Model.titleField;
if (!options.service && Model.service) {
options.service = Model.service.id;
}
}
}];
RelationshipField.defaultOptions = {
cell: 'RelationshipFieldCell',
view: 'RelationshipFieldView',
filter: 'RelationshipFieldFilter',
defaultValue: ''
};
{
"name": "alaska-field-relationship",
"version": "0.11.24",
"version": "0.12.0",
"description": "Alaska relationship field",

@@ -9,9 +9,10 @@ "keywords": [

],
"alaska": "field",
"main": "index.js",
"repository": "https://github.com/maichong/alaska/tree/master/packages/alaska-field-relationship",
"repository": "https://github.com/maichong/alaska/tree/master/src/alaska-field-relationship",
"author": "Liang <liang@maichong.it> (https://github.com/liangxingchen)",
"license": "MIT",
"dependencies": {
"alaska-field-select": "^0.11.22"
"alaska-field-select": "^0.12.0"
}
}

@@ -6,20 +6,19 @@ // @flow

import { connect } from 'react-redux';
import { Link } from 'react-router';
import { Link } from 'react-router-dom';
import { loadDetails } from 'alaska-admin-view/redux/details';
class RelationshipFieldCell extends React.Component {
type Props = {
+model: Alaska$view$Model,
+field: Alaska$view$Field,
+value: any,
settings: Alaska$view$Settings,
details: Alaska$view$details,
loadDetails: Function
};
props: {
field:Object;
value:any;
settings:Object;
details:Object;
loadDetails:Function;
};
shouldComponentUpdate(props: Object) {
let field = this.props.field;
let key = field.key;
let details = this.props.details;
let value = props.value;
class RelationshipFieldCell extends React.Component<Props> {
shouldComponentUpdate(newProps: Props) {
let { details, model } = this.props;
let { key } = model;
let { value } = newProps;
if (!value) {

@@ -31,7 +30,7 @@ return false;

}
if (!props.details[key] || !details[key]) {
if (!newProps.details[key] || !details[key]) {
return true;
}
if (typeof value === 'string') {
if (props.details[key][value] !== details[key][value]) {
if (newProps.details[key][value] !== details[key][value]) {
return true;

@@ -42,3 +41,3 @@ }

let id = value[i];
if (props.details[key][id] !== details[key][id]) {
if (newProps.details[key][id] !== details[key][id]) {
return true;

@@ -51,7 +50,7 @@ }

getLink(value) {
let field = this.props.field;
let details = this.props.details;
let Model = this.props.settings.models[field.ref];
let key = Model.key;
getLink(value: string) {
let { field, details, settings } = this.props;
if (!field.ref) return null;
let Model = settings.models[field.ref];
let { key } = Model;
let title = value;

@@ -70,10 +69,10 @@ if (value && details && details[key] && details[key][value]) {

}
return <Link
return (<Link
key={value}
to={'/edit/' + field.service + '/' + field.model + '/' + encodeURIComponent(value)}
>{title}</Link>;
>{title}</Link>);
}
render() {
let value = this.props.value;
let { value } = this.props;
if (!value) {

@@ -84,11 +83,10 @@ return <div className="relationship-field-cell" />;

if (Array.isArray(value)) {
display = [];
let arr = [];
value.forEach((v) => {
if (display.length) {
// $Flow
display.push(' , ');
if (arr.length) {
arr.push(' , ');
}
// $Flow
display.push(this.getLink(v));
arr.push(this.getLink(v));
});
display = arr;
} else {

@@ -95,0 +93,0 @@ display = this.getLink(value);

@@ -9,3 +9,3 @@ // @flow

function getFilters(filters: Object) {
function getFilters(filters?: Object) {
if (!filters) return {};

@@ -20,4 +20,10 @@ return _.reduce(filters, (res, value, key) => {

export default class RelationshipFieldFilter extends React.Component {
type State = {
value: string | number,
inverse: boolean,
error: boolean,
options: Alaska$SelectField$option[]
};
export default class RelationshipFieldFilter extends React.Component<Alaska$view$Field$Filter$Props, State> {
static contextTypes = {

@@ -27,18 +33,3 @@ t: PropTypes.func,

props: {
className: string,
value: any,
field: Object,
onChange: Function,
onClose: Function,
};
state: {
value:string|number;
inverse:boolean;
error:boolean;
options:Alaska$SelectField$option[]
};
constructor(props: Object) {
constructor(props: Alaska$view$Field$Filter$Props) {
super(props);

@@ -58,5 +49,5 @@ let v = props.value || {};

componentWillReceiveProps(props: Object) {
if (props.value !== this.props.value) {
let value = props.value;
componentWillReceiveProps(props: Alaska$view$Field$Filter$Props) {
let { value } = props;
if (value !== this.props.value) {
if (typeof value === 'string') {

@@ -74,7 +65,7 @@ value = { value };

handleSearch = (keyword: string, callback: Function) => {
let field = this.props.field;
let { field, value } = this.props;
api('/api/relation')
.param('service', field.service)
.param('model', field.model)
.param('value', field.value)
.param('value', value)
.search(keyword)

@@ -103,5 +94,7 @@ .where(getFilters(field.filters))

render() {
const t = this.context.t;
const { t } = this.context;
let { className, field, onClose } = this.props;
const { value, inverse, error, options } = this.state;
const {
value, inverse, error, options
} = this.state;
const buttonClassName = 'btn btn-default';

@@ -125,3 +118,4 @@ const buttonClassNameActive = buttonClassName + ' btn-success';

onClick={this.handleInverse}
>{t('inverse')}</a>
>{t('inverse')}
</a>
</div>

@@ -128,0 +122,0 @@ <a className="btn field-filter-close" onClick={onClose}><i className="fa fa-close" /></a>

@@ -17,31 +17,19 @@ // @flow

export default class RelationshipFieldView extends React.Component {
type State = {
value?: string | number | Array<any>;
options?: Alaska$SelectField$option[]
};
props: {
className: string,
model: Object,
field: Object,
data: Object,
errorText: string,
disabled: boolean,
value: any,
onChange: Function,
};
state: {
value?: string | number | Array<any>;
options: Alaska$SelectField$option[] | null
};
export default class RelationshipFieldView extends React.Component<Alaska$view$Field$View$Props, State> {
cache: Object;
constructor(props: Object) {
constructor(props: Alaska$view$Field$View$Props) {
super(props);
this.cache = {};
this.state = {
options: null
options: undefined
};
}
componentWillReceiveProps(props: Object) {
componentWillReceiveProps(props: Alaska$view$Field$View$Props) {
if (props.value !== this.props.value) {

@@ -53,9 +41,8 @@ if (_.find(this.state.options, (o) => o.value === props.value)) return;

shouldComponentUpdate(props: Object, state: Object) {
if (props.data !== this.props.data) {
let filters = props.field.filters;
shouldComponentUpdate(props: Alaska$view$Field$View$Props, state: State) {
if (props.record !== this.props.record) {
if (
_.find(
filters,
(v) => (_.isString(v) && v[0] === ':' && props.data[v.substr(1)] !== this.props.data[v.substr(1)])
props.field.filters,
(v) => (_.isString(v) && v[0] === ':' && props.record[v.substr(1)] !== this.props.record[v.substr(1)])
)

@@ -67,3 +54,3 @@ ) {

}
return !shallowEqualWithout(props, this.props, 'data', 'onChange', 'search')
return !shallowEqualWithout(props, this.props, 'record', 'onChange', 'search')
|| this.state.options !== state.options;

@@ -76,3 +63,3 @@ }

handleChange = (value: string|number) => {
handleChange = (value: string | number) => {
if (this.props.onChange) {

@@ -87,3 +74,3 @@ let val = null;

}
this.setState({ value });
//this.setState({ value });
this.props.onChange(val);

@@ -95,7 +82,7 @@ }

keyword = keyword || '';
const { field, data } = this.props;
let filters = _.reduce(field.filters || {}, (res: {}, value: any, key: string) => {
res[key] = value;
if (_.isString(value) && value[0] === ':') {
res[key] = data[value.substr(1)];
const { field, record, value } = this.props;
let filters = _.reduce(field.filters || {}, (res: {}, v: any, key: string) => {
res[key] = v;
if (_.isString(v) && v[0] === ':') {
res[key] = record[v.substr(1)];
}

@@ -117,3 +104,3 @@ return res;

.param('model', field.model)
.param('value', field.value)
.param('value', value)
.search(keyword)

@@ -129,5 +116,7 @@ .where(filters)

render() {
let { className, field, value, disabled, errorText } = this.props;
const options = this.state.options;
let help = field.help;
let {
className, field, value, disabled, errorText
} = this.props;
const { options } = this.state;
let { help } = field;
let View = Select;

@@ -149,5 +138,3 @@ if (field.checkbox) {

let opts = [];
if (!options) {
this.handleSearch();
} else {
if (options) {
if (typeof value === 'string') {

@@ -157,17 +144,17 @@ value = [value];

_.forEach(value, (v) => {
let opt: Alaska$SelectField$option = _.find(options, (o) => o.value === v);
let opt: ?Alaska$SelectField$option = _.find(options, (o) => o.value === v);
opts.push(opt || { value: v, label: v });
});
} else {
this.handleSearch();
}
inputElement = <p className="form-control-static">
{
opts.map(
(opt: Alaska$SelectField$option) => <a
key={opt.value}
href={`#/edit/${field.service}/${field.model}/${String(opt.value)}`}
style={{ paddingRight: 10 }}
>{opt.label}</a>
)
}
inputElement = <p className="form-control-static">{
opts.map((opt: Alaska$SelectField$option) => (<a
key={opt.value}
href={`#/edit/${field.service}/${field.model}/${String(opt.value)}`}
style={{ paddingRight: 10 }}
>{opt.label}
</a>))
}
</p>;

@@ -174,0 +161,0 @@ } else {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc