/* * Copyright 2007-2017 Charles du Jeu - Abstrium SAS * This file is part of Pydio. * * Pydio is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Pydio is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Pydio. If not, see . * * The latest code can be found at . */ const React = require('react') const Pydio = require('pydio') const {PydioContextConsumer, AsyncComponent} = Pydio.requireLib('boot') let GridBuilder = React.createClass({ propTypes:{ namespaces : React.PropTypes.array, onCreateCard : React.PropTypes.func, onEditStatusChange : React.PropTypes.func }, getInitialState:function(){ return { selectedIndex:0, availableWidgets:this.listAvailableWidgets() } }, listAvailableWidgets:function(secondPass = false){ var widgets = []; let additionalNamespaces = []; this.props.namespaces.map(function(ns){ if(!global[ns]) { additionalNamespaces.push(ns); return; } for(var k in global[ns]){ if(global[ns].hasOwnProperty(k)){ var widgetClass = global[ns][k]; if(widgetClass.hasBuilderFields && widgetClass.hasBuilderFields()){ widgets.push({reactClass:widgetClass, fullName:ns+'.'+widgetClass.displayName}); } } } }); if(additionalNamespaces.length && !secondPass){ ResourcesManager.loadClassesAndApply(additionalNamespaces, function(){ this.setState({ availableWidgets:this.listAvailableWidgets(true) }); }.bind(this)); } return widgets; }, onDropDownChange:function(event, index, item){ var defaultValues={}; if(index != 0){ item.payload['reactClass'].getBuilderFields().map(function(f){ if(f['default']) defaultValues[f.name] = f['default']; }); } if(this.props.onEditStatusChange){ this.props.onEditStatusChange((index != 0)); } this.setState({ selectedIndex:index, selectedWidget:item.payload, currentFormValues:defaultValues }); }, cancel:function(){ if(this.props.onEditStatusChange){ this.props.onEditStatusChange(false); } this.setState({selectedIndex:0}); }, onFormValueChange:function(newValues){ this.setState({currentFormValues:newValues}); }, onFormSubmit:function(){ var values = this.state.currentFormValues; var selectedWidget = this.state.selectedWidget; var title = (values.title?values.title:values.legend); if(!title) title = this.state.selectedWidget['reactClass'].builderDisplayName; this.props.onCreateCard({ componentClass:selectedWidget.fullName, title:title, props:values }); this.cancel(); }, resetLayout: function(){ if(window.confirm(this.props.getMessage('home.51'))){ this.props.onResetLayout(); } }, render:function(){ const {getMessage} = this.props; var selectorItems = [{payload:0,text:getMessage('home.50')}].concat( this.state.availableWidgets.map(function(w, index){ return {payload:w, text:w['reactClass'].builderDisplayName}; }) ); var selector = ( ); var form, add; if(this.state.selectedIndex != 0){ var fields = this.state.selectedWidget['reactClass'].getBuilderFields(); var defaultValues={}; fields.map(function(f){ if(f['default']) defaultValues[f.name] = f['default']; }); if(this.state.currentFormValues){ defaultValues = LangUtils.mergeObjectsRecursive(defaultValues, this.state.currentFormValues); } form =( ); add = (
 
); } return (

{getMessage('home.53')}

{getMessage('home.54')}
{getMessage('home.55')}
{selector} {form} {add}
); } }); GridBuilder = PydioContextConsumer(GridBuilder); export {GridBuilder as default}