AWV-3: Merging changes from upstream

This commit is contained in:
Bruno Alves de Faria committed 2016-09-03 14:47:39 -03:00
commit 176b503c5a
153 files changed
+2377 -1125

No files matched your search

@@ -10,6 +10,7 @@ import './mixins/formItem.js';
import './mixins/group.js';
import './mixins/groupRadio.js';
import './mixins/input.js';
import './mixins/checkbox.js';
import './mixins/schemaData.js';
import './mixins/select.js';
import './mixins/select2.js';
@@ -0,0 +1,26 @@
import { OHIF } from 'meteor/ohif:core';
import { Template } from 'meteor/templating';
import { _ } from 'meteor/underscore';
/*
* inputCheckbox: controls a checkbox input
*/
OHIF.mixins.checkbox = new OHIF.Mixin({
dependencies: 'input',
composition: {
onCreated() {
const instance = Template.instance();
const component = instance.component;
// Get or set the checked state using jQuery's prop method
component.value = value => {
const isGet = _.isUndefined(value);
if (isGet) {
return component.parseData(component.$element.is(':checked'));
}
component.$element.prop('checked', value);
};
}
}
});
@@ -1,5 +1,8 @@
import { OHIF } from 'meteor/ohif:core';
import { Template } from 'meteor/templating';
import { Tracker } from 'meteor/tracker';
import { _ } from 'meteor/underscore';
import { $ } from 'meteor/jquery';
/*
* form: controls a form and its registered inputs
@@ -11,40 +14,83 @@ OHIF.mixins.form = new OHIF.Mixin({
const instance = Template.instance();
const component = instance.component;
// Define the form's data schema
const schema = instance.data.schema;
component.schema = schema && schema.newContext();
// Set the form identifier flag
component.isForm = true;
// Check if the form data is valid in its schema
component.validationObserver = new Tracker.Dependency();
// Reset the pathKey
instance.data.pathKey = '';
// Debound the observer call to prevent tons of re-rendering
component.validationRan = _.debounce(() => {
// Enable reactivity by changing a Tracker.Dependency observer
component.validationObserver.changed();
}, 200);
// Change the validation function to focus the fields with error
const validateSelf = component.validate;
component.validate = () => {
// Assume validation result as true
let result = true;
// Call the original validation function
validateSelf();
// Return true if there's no data schema defined
if (!component.schema) {
return result;
// Focus the first error field if some validation failed
if (component.schema && component.schema._invalidKeys.length) {
instance.$('.state-error :input:first').focus();
}
// Iterate over each registered form item and validate it
component.registeredItems.forEach(child => {
const key = child.templateInstance.data.key;
// Change result to false if any form item is invalid
if (key && !child.validate()) {
result = false;
}
});
// Return the validation result
return result;
};
},
onRendered() {
const instance = Template.instance();
const component = instance.component;
// Set the component main and style elements
component.$style = component.$element = instance.$('form:first');
},
events: {
'click .validation-error-container a'(event, instance) {
// Get the target key
const targetKey = $(event.currentTarget).attr('data-target');
// Focus the first input inside the element with error state
instance.$(`.state-error[data-key="${targetKey}"] :input:first`).focus();
}
},
helpers: {
validationErrors() {
const instance = Template.instance();
const component = instance.component;
// Create a dependency on child components validation
component.validationObserver.depend();
// Stop here if no schema was defined for the form
if (!component.schema) {
return;
}
// Check if there were some validation errors
if (component.schema._invalidKeys.length) {
const result = [];
// Iterate over each validation error and add to result
component.schema._invalidKeys.forEach(item => {
const label = component.schema._schema[item.name].label;
let message = component.schema.keyErrorMessage(item.name);
message = message.replace(label, `<strong>${label}</strong>`);
result.push({
key: item.name,
message: Spacebars.SafeString(message)
});
});
// Return the resulting validation errors
return result;
}
}
}
}
});
@@ -1,6 +1,7 @@
import { OHIF } from 'meteor/ohif:core';
import { Template } from 'meteor/templating';
import { Tracker } from "meteor/tracker";
import { Tracker } from 'meteor/tracker';
import { Meteor } from 'meteor/meteor';
import { _ } from 'meteor/underscore';
import { $ } from 'meteor/jquery';
@@ -31,7 +32,7 @@ OHIF.mixins.formItem = new OHIF.Mixin({
component.value = value => {
const isGet = _.isUndefined(value);
if (isGet) {
return component.$element.val();
return component.parseData(component.$element.val());
}
component.$element.val(value).trigger('change');
@@ -53,6 +54,19 @@ OHIF.mixins.formItem = new OHIF.Mixin({
component.$wrapper[method]();
};
// Check if the focus is inside this element
component.hasFocus = () => {
// Get the focused element
const focused = $(':focus')[0];
// Check if the focused element is inside the component
const contains = $.contains(component.$wrapper[0], focused);
const isEqual = component.$wrapper[0] === focused;
// Return true if he component has the focus
return contains || isEqual;
};
// Add or remove a state from the component
component.state = (state, flag) => {
component.$wrapper.toggleClass(`state-${state}`, !!flag);
@@ -65,24 +79,77 @@ OHIF.mixins.formItem = new OHIF.Mixin({
// Set or remove the error message
if (errorMessage) {
component.$element.trigger('errorin');
component.$wrapper.attr('data-error', errorMessage);
} else {
component.$element.trigger('errorout');
component.$wrapper.removeAttr('data-error', errorMessage);
}
};
// Toggle the tooltip over the component
component.toggleTooltip = (isShow, message) => {
if (isShow && message) {
// Stop here if the tooltip is already created
if (component.$wrapper.next('.tooltip').length) {
return;
}
// Create the tooltip
component.$wrapper.tooltip({
trigger: 'manual',
title: message
}).tooltip('show');
} else {
// Destroy the tooltip
component.$wrapper.tooltip('destroy');
}
};
// Toggle a state message as a tooltip over the component
component.toggleMessage = isShow => {
// Check if the action is to hide
if (!isShow) {
Meteor.setTimeout(() => {
// Check if the component has the focus
if (component.hasFocus()) {
// Prevent the tooltip from being hidden
return;
}
// Hide the tooltip
component.toggleTooltip(false);
}, 100);
return;
}
// Check for error state and message
const errorMessage = component.$wrapper.attr('data-error');
if (errorMessage) {
// Show the tooltip with the error message
component.toggleTooltip(true, errorMessage);
}
},
// Search for the parent form component
component.getForm = () => {
let currentComponent = component;
while (currentComponent) {
currentComponent = currentComponent.parent;
if (currentComponent && currentComponent.isForm) {
return currentComponent;
}
}
};
// Check if the component value is valid in its form's schema
component.validate = () => {
// Get the component's form
const form = component.parent;
const form = component.getForm();
// Get the form's data schema
const schema = form && form.schema;
// Get the current component's key
const key = instance.data.key;
const key = instance.data.pathKey;
// Return true if validation is not needed
if (!key || !schema || !component.$wrapper.is(':visible')) {
@@ -90,12 +157,18 @@ OHIF.mixins.formItem = new OHIF.Mixin({
}
// Create the data document for validation
const document = {
const document = OHIF.blaze.getNestedObject({
[key]: component.value()
};
});
// Get the validation result
const validationResult = schema.validateOne(document, key);
// Notify the form that the validation ran
form.validationRan();
// Check if the document validation failed
if (!schema.validateOne(document, key)) {
if (!validationResult) {
// Set the component in error state and display the message
component.error(schema.keyErrorMessage(key));
@@ -125,6 +198,9 @@ OHIF.mixins.formItem = new OHIF.Mixin({
// Set the most outer wrapper element
component.$wrapper = instance.wrapper.$('*').first();
// Add the pathKey to the wrapper element
component.$wrapper.attr('data-key', instance.data.pathKey);
},
onDestroyed() {
@@ -143,24 +219,55 @@ OHIF.mixins.formItem = new OHIF.Mixin({
component.$style = component.$element;
}
// Set the component in jQuery data after all mixins are rendered
// Set the component in element and wrapper jQuery data
component.$element.data('component', component);
component.$wrapper.data('component', component);
},
events: {
// Enable reactivity by changing a Tracker.Dependency observer
// Handle the change event for the component
change(event, instance) {
instance.component.changeObserver.changed();
const component = instance.component;
// Prevent execution on upper components
if (event.currentTarget === component.$element[0]) {
// Enable reactivity by changing a Tracker.Dependency observer
component.changeObserver.changed();
// Revalidate the component
component.validate();
}
},
// TODO: [design] remove log, show error box/hint over the wrapper
errorin(event, instance) {
console.log('ERROR when validating component', instance.component);
focus(event, instance) {
const component = instance.component;
// Stop here if it is an group
if (component.isGroup || component.isCustomFocus) {
return;
}
// Prevent event bubbling
event.stopPropagation();
// Check for state messages and show it
component.toggleMessage(true);
},
// TODO: [design] hide error box/hint
errorout(event, instance) {
blur(event, instance) {
const component = instance.component;
// Stop here if it is an group
if (component.isGroup || component.isCustomFocus) {
return;
}
// Prevent event bubbling
event.stopPropagation();
// Hide state messages
component.toggleMessage(false);
}
}
@@ -12,29 +12,124 @@ OHIF.mixins.group = new OHIF.Mixin({
const instance = Template.instance();
const component = instance.component;
// Set the group identifier flag
component.isGroup = true;
// Run this computation every time the schema property is changed
instance.autorun(() => {
let schema = instance.data.schema;
// Check if the schema is reactive
if (schema instanceof ReactiveVar) {
// Register a dependency on schema property
schema = schema.get();
}
// Set the form's data schema
component.schema = schema && schema.newContext();
});
// Get or set the child components values
component.value = value => {
const isGet = _.isUndefined(value);
const isArray = instance.data.arrayValues;
// Create the result data as array or object
const result = isArray ? [] : {};
// Get the group current value and return it
if (isGet) {
const result = {};
// Iterate over each registered item and extract its value
component.registeredItems.forEach(child => {
const key = child.templateInstance.data.key;
if (key) {
result[key] = child.value();
// Check if it is an array or an object group
if (isArray) {
// Push the item value to the result array
result.push(child.value());
} else {
// Get the item key
const key = child.templateInstance.data.key;
//Check if a key is set for the item
if (key) {
// Add the item value to the result object
result[key] = child.value();
}
}
});
// Return the resulting data as array or object
return result;
}
const groupValue = typeof value === 'object' ? value : {};
// Get the group current value
const groupValue = typeof value === 'object' ? value : result;
// Stop here if there is no value defined for this group
if (!groupValue) {
return;
}
// Iterate over each registered item and set its value
let i = 0;
component.registeredItems.forEach(child => {
const key = child.templateInstance.data.key;
const key = isArray ? i : child.templateInstance.data.key;
const childValue = _.isUndefined(groupValue[key]) ? null : groupValue[key];
child.value(childValue);
i++;
});
// Trigger the change event after setting the new value
component.$element.trigger('change');
};
// Get a registered item in form by its key
component.item = itemKey => {
let found;
// Iterate over each registered form item
component.registeredItems.forEach(child => {
const key = child.templateInstance.data.key;
// Change the found item if current key is the same as given
if (key === itemKey) {
found = child;
}
});
// Return the found item or undefined if it was not found
return found;
};
// Check if the form data is valid in its schema
const validateSelf = component.validate;
component.validate = () => {
// Assume validation result as true
let result = true;
// Return true if there's no data schema defined
if (component.isForm && !component.schema) {
return result;
}
// Validate the component itself if it has a key
if (instance.data.pathKey && !validateSelf()) {
result = false;
}
// Iterate over each registered form item and validate it
component.registeredItems.forEach(child => {
const key = child.templateInstance.data.key;
// Change result to false if any form item is invalid
if ((key || instance.data.arrayValues) && !child.validate()) {
result = false;
}
});
// Return the validation result
return result;
};
// Disable or enable the component
component.disable = isDisable => {
component.registeredItems.forEach(child => child.disable(isDisable));
@@ -21,7 +21,7 @@ OHIF.mixins.groupRadio = new OHIF.Mixin({
component.registeredItems.forEach(child => elements.push(child.$element[0]));
const $elements = $(elements);
if (isGet) {
return $elements.filter(':checked').val();
return component.parseData($elements.filter(':checked').val());
}
$elements.filter(`[value='${value}']`).prop('checked', true).trigger('change');
@@ -7,7 +7,13 @@ import { _ } from 'meteor/underscore';
// Helper function to get the component's current schema
const getCurrentSchema = (parentComponent, key) => {
// Get the parent component schema
const schema = parentComponent && parentComponent.schema;
let schema = parentComponent && parentComponent.schema;
// Try to get the form schema if it was not found
if (parentComponent && !schema) {
const form = parentComponent.getForm();
schema = form && form.schema;
}
// Stop here if there's no key or schema defined
if (!key || !schema) {
@@ -17,6 +23,11 @@ const getCurrentSchema = (parentComponent, key) => {
// Get the current schema data using component's key
const currentSchema = _.clone(schema._schema[key]);
// Stop here if no schema was found for the given key
if (!currentSchema) {
return;
}
// Merge the sub-schema properties if it's an array
if (Array.isArray(currentSchema.type())) {
_.extend(currentSchema, schema._schema[key + '.$']);
@@ -40,8 +51,21 @@ OHIF.mixins.schemaData = new OHIF.Mixin({
// Get the parent component
const parent = OHIF.blaze.getParentComponent(Blaze.currentView);
// Get he parent component key
const parentKey = parent && parent.templateInstance.data.pathKey;
// Check if the parent is an array group
const isParentArray = parent && parent.templateInstance.data.arrayValues;
// Set the path key for this component
data.pathKey = data.key || (isParentArray ? '$' : '');
if (data.pathKey && typeof parentKey === 'string') {
const prefix = parentKey ? `${parentKey}.` : '';
data.pathKey = `${prefix}${data.pathKey}`;
}
// Get the current schema data using component's key
const currentSchema = getCurrentSchema(parent, data.key);
const currentSchema = getCurrentSchema(parent, data.pathKey);
// Stop here if there's no schema data for current key
if (!currentSchema) {
@@ -53,6 +77,11 @@ OHIF.mixins.schemaData = new OHIF.Mixin({
data.label = new ReactiveVar(currentSchema.label);
}
// Set the emptyOption data attribute if given on schema
if (currentSchema.emptyOption) {
data.emptyOption = currentSchema.emptyOption;
}
// Fill the items if it's an array schema
if (!data.items && Array.isArray(currentSchema.allowedValues)) {
// Initialize the items array
@@ -76,12 +105,41 @@ OHIF.mixins.schemaData = new OHIF.Mixin({
}
},
onCreated() {
const instance = Template.instance();
const component = instance.component;
// Create a data parser according to current schema key
component.parseData = value => {
// Get the current schema data using component's key
const currentSchema = getCurrentSchema(component.parent, instance.data.pathKey);
// Stop here if there's no schema data for current key
if (!currentSchema) {
return;
}
// Check if the schema is a Number
if (currentSchema.type === Number) {
return parseFloat(value);
}
// Check if the schema is a Boolean
if (currentSchema.type === Boolean) {
return !!value;
}
// Return the original value if none of the checks matched
return value;
};
},
onMixins() {
const instance = Template.instance();
const component = instance.component;
// Get the current schema data using component's key
const currentSchema = getCurrentSchema(component.parent, instance.data.key);
const currentSchema = getCurrentSchema(component.parent, instance.data.pathKey);
// Stop here if there's no schema data for current key
if (!currentSchema) {
@@ -8,6 +8,46 @@ import { $ } from 'meteor/jquery';
OHIF.mixins.select2 = new OHIF.Mixin({
dependencies: 'select',
composition: {
onCreated() {
const instance = Template.instance();
// Set the custom focus flag
instance.component.isCustomFocus = true;
// Check if this select will include a placeholder
const placeholder = instance.data.options && instance.data.options.placeholder;
if (placeholder) {
// Get the option items
let items = instance.data.items;
// Check if the items are reactive and get them if true
const isReactive = items instanceof ReactiveVar;
if (isReactive) {
items = items.get();
}
// Check if there is already an empty option on items list
const query = {
value: ''
};
if (!_.findWhere(items, query)) {
// Clone the current items
const newItems = _.clone(items) || [];
newItems.unshift({
label: placeholder,
value: ''
});
// Set the new items list including the empty option
if (isReactive) {
instance.data.items.set(newItems);
} else {
instance.data.items = newItems;
}
}
}
},
onRendered() {
const instance = Template.instance();
const component = instance.component;
@@ -17,6 +57,22 @@ OHIF.mixins.select2 = new OHIF.Mixin({
// Store the select2 instance to allow its further destruction
component.select2Instance = component.$element.data('select2');
// Get the focusable elements
const elements = [];
elements.push(component.$element[0]);
elements.push(component.$element.nextAll('.select2:first').find('.select2-selection')[0]);
// Attach focus and blur handlers to focusable elements
$(elements).on('focus', event => {
event.stopPropagation();
// Show the state message on elements focus
component.toggleMessage(true);
}).on('blur', event => {
event.stopPropagation();
// Hide the state message on elements blur
component.toggleMessage(false);
});
},
onDestroyed() {
@@ -25,14 +81,6 @@ OHIF.mixins.select2 = new OHIF.Mixin({
// Destroy the select2 instance to remove unwanted DOM elements
component.select2Instance.destroy();
},
events: {
'focus .select2-hidden-accessible'(event, instance) {
// Redirect the focus to select2 focus control in case of hidden
// accessible being focused (e.g. clicking on outer label)
$(event.currentTarget).nextAll('.select2:first').find('.select2-selection').focus();
}
}
}
});
@@ -30,7 +30,7 @@ OHIF.mixins.selectTree = new OHIF.Mixin({
// Return the current value
if (isGet) {
return rootInstance.currentValue;
return component.parseData(rootInstance.currentValue);
}
// Change the current value
@@ -11,6 +11,13 @@
target="{{this.target}}"
{{this.tagAttributes}}
>
{{#if validationErrors}}
<div class="validation-error-container alert alert-danger" role="alert">
{{#each error in validationErrors}}
<p><a href="#" class="text-danger" data-target="{{error.key}}">{{error.message}}</a></p>
{{/each}}
</div>
{{/if}}
{{>UI.contentBlock}}
</form>
</template>
@@ -4,6 +4,7 @@
class="{{this.class}}"
name="{{this.name}}"
multiple="{{#if this.multiple}}multiple{{/if}}"
disabled="{{#if this.disabled}}disabled{{/if}}"
{{this.tagAttributes}}
>
{{>UI.contentBlock}}
@@ -1,6 +1,8 @@
import './form/form.html';
import './form/group.html';
import './input/checkbox.html';
import './input/hidden.html';
import './input/groupRadio.html';
import './input/radio.html';
import './input/select.html';
@@ -0,0 +1,15 @@
<template name="inputCheckbox">
{{#baseComponent (extend this
base='baseInput'
type='checkbox'
mixins=(concat 'checkbox ' this.mixins)
labelAfter=(valueIf (isUndefined this.labelAfter) true this.labelAfter)
labelClass=(concat 'checkboxLabel ' this.labelClass)
wrappers=(concat ''
(valueIf reactive this.label 'wrapperLabel ' '')
this.wrappers
)
)}}
{{>UI.contentBlock}}
{{/baseComponent}}
</template>
@@ -0,0 +1,7 @@
<template name="inputHidden">
{{>baseComponent (extend this
base='baseInput'
type='hidden'
mixins=(concat 'input ' this.mixins)
)}}
</template>
@@ -4,7 +4,7 @@
type='radio'
mixins=(concat 'input ' this.mixins)
labelAfter=(valueIf (isUndefined this.labelAfter) true this.labelAfter)
labelClass=(concat 'checkboxLabel ' this.labelClass)
labelClass=(concat 'radioLabel ' this.labelClass)
wrappers=(concat ''
(valueIf reactive this.label 'wrapperLabel ' '')
this.wrappers
@@ -11,17 +11,19 @@
<div class="tree-content">
{{#let currentNode=this.currentNode.get}}
{{#if (and (not this.root) (ne this.search false))}}
{{>inputText
{{#inputText
label=this.label
labelClass='tree-search'
placeholder=(choose this.searchPlaceholder 'Search')
}}
<i class="fa fa-search"></i>
{{/inputText}}
{{/if}}
<div class="tree-options {{#if currentNode}}collapsed{{/if}}">
{{#if this.root}}
{{>selectTreeBreadcrumb component=this.component}}
{{/if}}
<div class="tree-inputs">
<div class="tree-inputs clearfix">
{{#each item in treeItems}}
{{>inputRadio
id=(concat this.storageKey '_' (encodeId item.value))
@@ -45,8 +47,8 @@
</div>
{{/let}}
</div>
{{#if (and (not this.root) this.storageKey)}}
{{>selectTreeCommon (clone this component=this.component)}}
{{#if (and (not this.root) (or this.storageKey this.commonItems))}}
{{>selectTreeCommon (clone this component=this.component treeInstance=instance)}}
{{/if}}
{{/group}}
</template>
@@ -11,6 +11,45 @@ import transition from 'meteor/ohif:core/client/lib/third-party/transition-to-fr
Template.selectTree.onCreated(() => {
const instance = Template.instance();
// Return the common items
instance.getCommonItems = () => {
const instance = Template.instance();
// Return the common items if given
if (instance.data.commonItems) {
return instance.data.commonItems;
}
// Get all the tree leaves
const leaves = instance.data.component.getLeaves();
// Generate an object with encoded keys from the tree leaves
leavesObject = {};
_.each(leaves, leaf => {
leavesObject[OHIF.string.encodeId(leaf.value)] = leaf;
});
// Get the current items ranking
const ranking = OHIF.user.getData(instance.data.storageKey);
// Sort the items based on how many times each one was used
const sorted = [];
_.each(ranking, (count, key) => sorted.push([key, count]));
sorted.sort((a, b) => b[1] - a[1]);
// Create the result and push every item respecting the ranking order
const result = [];
_.each(sorted, item => {
const current = leavesObject[item[0]];
if (current) {
result.push(current);
}
});
// Return the resulting array
return result;
};
// Create a reactive variable to control the search
instance.searchTerm = new ReactiveVar('');
});
@@ -25,7 +64,10 @@ Template.selectTree.onRendered(() => {
instance.component = component;
// Set the margin to display the common section
$treeRoot.children('.tree-content').css('margin-right', $treeRoot.width());
const isTreeColumns = instance.data.treeColumns;
const marginProperty = isTreeColumns ? 'margin-left' : 'margin-right';
const marginWidth = isTreeColumns ? $treeRoot.width() / 2 : $treeRoot.width();
$treeRoot.children('.tree-content').css(marginProperty, marginWidth);
// Make the component respect the window boundaries
$treeRoot.bounded();
@@ -144,6 +186,18 @@ Template.selectTree.events({
$treeRoot.addClass('interacted');
},
'keydown .tree-search input'(event, instance) {
// Get the search term
const searchTerm = $(event.currentTarget).val().trim();
const $labels = instance.$('.tree-inputs:first>label');
// Select the label if ENTER was pressed and there is only one result
if (searchTerm.length && $labels.length === 1 && event.which === 13) {
$labels.find('input').click();
}
},
'change .select-tree:first>.tree-content>.tree-options>.tree-inputs>label>input'(event, instance) {
const component = instance.component;
const $target = $(event.currentTarget);
@@ -235,27 +289,6 @@ Template.selectTree.events({
currentInstance = currentInstance.component.parent.templateInstance;
}
}
},
'click .select-tree-common label'(event, instance) {
// Get the clicked label
const $target = $(event.currentTarget);
// Build the input selector based on the label target
const inputSelector = '#' + $target.attr('for');
// Check if the input is not rendered
if (!$(inputSelector).length) {
// Change the search terms with the clicked label string
instance.$('.tree-search input').val($target.text()).trigger('input');
// Wait for options rerendering
Tracker.afterFlush(() => {
// Wait for components initialization
Meteor.defer(() => $(inputSelector).click());
});
}
}
});
@@ -276,6 +309,23 @@ Template.selectTree.helpers({
// Filter only the tree leaves
items = _.filter(items, item => !item.items);
} else if (instance.data.hideCommon) {
// Get the values of common items
const commonValues = _.pluck(instance.getCommonItems(), 'value');
// Filter only the items that are not common
items = _.filter(items, item => !_.contains(commonValues, item.value));
}
// Return the items sorted for tree columns
if (instance.data.treeColumns) {
const begin = items.splice(0, Math.ceil(items.length / 2));
const sortedItems = [];
_.each(begin, (item, index) => {
sortedItems.push(item);
items[index] && sortedItems.push(items[index]);
});
items = sortedItems;
}
// Return the tree items
@@ -2,38 +2,35 @@ import { OHIF } from 'meteor/ohif:core';
import { Template } from 'meteor/templating';
import { _ } from 'meteor/underscore';
Template.selectTreeCommon.helpers({
// Get the common items ordered by use history
items() {
const instance = Template.instance();
Template.selectTreeCommon.events({
'click .select-tree-common label'(event, instance) {
// Get the clicked label
const $target = $(event.currentTarget);
// Get all the tree leaves
const leaves = instance.data.component.getLeaves();
// Build the input selector based on the label target
const inputSelector = '#' + $target.attr('for');
// Generate an object with encoded keys from the tree leaves
leavesObject = {};
_.each(leaves, leaf => {
leavesObject[OHIF.string.encodeId(leaf.value)] = leaf;
});
// Get the tree instance
const treeInstance = instance.data.treeInstance;
// Get the current items ranking
const ranking = OHIF.user.getData(instance.data.storageKey);
// Check if the input is not rendered
if (!$(inputSelector).length) {
// Change the search terms with the clicked label string
// Sort the items based on how many times each one was used
const sorted = [];
_.each(ranking, (count, key) => sorted.push([key, count]));
sorted.sort((a, b) => b[1] - a[1]);
treeInstance.$('.tree-search input').val($target.text()).trigger('input');
// Wait for options rerendering
// Create the result and push every item respecting the ranking order
const result = [];
_.each(sorted, item => {
const current = leavesObject[item[0]];
if (current) {
result.push(current);
}
});
// Return the resulting array
return result;
Tracker.afterFlush(() => {
// Wait for components initialization
Meteor.defer(() => $(inputSelector).click());
});
}
}
});
Template.selectTreeCommon.helpers({
// Get the common items
items() {
return Template.instance().data.treeInstance.getCommonItems();
}
});
@@ -1,6 +1,6 @@
<template name="componentPlayground">
<div class="component-playground">
{{>measureFlow}}
{{>measureFlow treeColumns=true hideCommon=true}}
<!--
<div class="p-a-3">
{{>selectTree
+1 -1
View File
@@ -31,7 +31,7 @@ OHIF.blaze.getParentView = (view, parentViewName) => {
};
// Search for the parent component of the given view
OHIF.blaze.getParentComponent = (view) => {
OHIF.blaze.getParentComponent = view => {
let currentView = view;
while (currentView) {
currentView = currentView.originalParentView || currentView.parentView;
+1
View File
@@ -1,4 +1,5 @@
import './blaze.js';
import './object.js';
import './string.js';
import './ui.js';
import './user.js';
+50
View File
@@ -0,0 +1,50 @@
import { OHIF } from 'meteor/ohif:core';
OHIF.object = {};
// Transforms a shallow object with keys separated by "." into a nested object
OHIF.blaze.getNestedObject = shallowObject => {
var nestedObject = {};
for (var key in shallowObject) {
var value = shallowObject[key];
var propertyArray = key.split('.');
var currentObject = nestedObject;
while (propertyArray.length) {
var currentProperty = propertyArray.shift();
if (!propertyArray.length) {
currentObject[currentProperty] = value;
} else {
if (!currentObject[currentProperty]) {
currentObject[currentProperty] = {};
}
currentObject = currentObject[currentProperty];
}
}
}
return nestedObject;
};
// Transforms a nested object into a shallowObject merging its keys with "." character
OHIF.blaze.getShallowObject = nestedObject => {
var shallowObject = {};
var putValues = function(baseKey, nestedObject, resultObject) {
for (var key in nestedObject) {
var currentKey = baseKey ? baseKey + '.' + key : key;
var currentValue = nestedObject[key];
if (typeof currentValue === 'object') {
if (currentValue instanceof Array) {
currentKey += '[]';
}
putValues(currentKey, currentValue, resultObject);
} else {
resultObject[currentKey] = currentValue;
}
}
};
putValues('', nestedObject, shallowObject);
return shallowObject;
};