UNPKG

ldx-widgets

Version:

widgets

93 lines (88 loc) 3.05 kB
(function() { var React, div; React = require('react'); div = require('react-dom-factories').div; module.exports = { getInitialState: function() { return { layout: 2 }; }, componentWillMount: function() { this.MIN_MODULE_WIDTH = this.MIN_MODULE_WIDTH || 500; window.addEventListener('resize', this.measureColumns); return this.measureColumns(); }, componentWillUnmount: function() { return window.removeEventListener('resize', this.measureColumns); }, measureColumns: function() { var layout, newColumnCount, width; layout = this.state.layout; width = window.innerWidth; newColumnCount = Math.floor(width / this.MIN_MODULE_WIDTH); newColumnCount = newColumnCount === 0 ? 1 : newColumnCount; newColumnCount = newColumnCount === 5 ? 4 : newColumnCount; if (newColumnCount !== layout) { return this.setState({ layout: newColumnCount }); } }, featureIsEnabled: function(feature, features) { var action, dep, ref, ref1, subDepConditionMet; if (feature == null) { return false; } if (feature.subdeps == null) { return feature.actions[feature.primaryAction]; } subDepConditionMet = false; ref = feature.subdeps; for (dep in ref) { action = ref[dep]; if ((ref1 = features[dep]) != null ? ref1.actions[action] : void 0) { subDepConditionMet = true; break; } } return feature.actions[feature.primaryAction] && subDepConditionMet; }, buildColumns: function() { var activeModules, authorityId, c, col, columnCount, columnEls, columns, factory, features, i, j, k, l, layout, len, len1, moduleList, mpi, patientId, ref, ref1; layout = this.state.layout; mpi = this.props.params.mpi; ref = this.props.location.query, authorityId = ref.authorityId, patientId = ref.patientId; features = this.getStore('loggedInUser').features; activeModules = this.makeModuleList(); layout = activeModules.length < layout ? layout - 1 : layout; columns = []; for (c = j = 0, ref1 = layout; 0 <= ref1 ? j < ref1 : j > ref1; c = 0 <= ref1 ? ++j : --j) { columns.push([]); } col = 0; for (i = k = 0, len = activeModules.length; k < len; i = ++k) { factory = activeModules[i]; col = col === layout ? 0 : col; columns[col].push(factory({ key: "module-" + i, mpi: mpi, authorityId: authorityId, patientId: patientId })); col += 1; } columnEls = []; columnCount = 0; for (i = l = 0, len1 = columns.length; l < len1; i = ++l) { moduleList = columns[i]; columnEls.push(div({ key: "column-" + columnCount, className: "column layout-" + layout + " col-" + columnCount }, moduleList)); columnCount += 1; } return columnEls; } }; }).call(this);