|
1 | | -import { deepExtend, isArray, isDevelopment, isString, noop, pick } from '@semantic-ui/utils'; |
| 1 | +import { deepExtend, filterObject, isArray, isDevelopment, isString, noop, pick } from '@semantic-ui/utils'; |
2 | 2 | import { Behavior } from './behavior.js'; |
3 | 3 | import { Query } from './query.js'; |
4 | 4 |
|
@@ -80,7 +80,11 @@ export const registerBehavior = (behavior) => { |
80 | 80 |
|
81 | 81 | // Create abstraction around behavior initialization |
82 | 82 | Query.prototype[name] = function(settings) { |
83 | | - // At run time we need to check if defaults are changed from original registration |
| 83 | + // ignore undefined settings |
| 84 | + settings = filterObject(settings, (value) => value !== undefined); |
| 85 | + |
| 86 | + // At run time we need to check if defaults are changed by user |
| 87 | + // this can occur if $.plugin.defaultSettings is modified |
84 | 88 | const defaultValues = ['defaultSettings', 'classNames', 'errors', 'selector']; |
85 | 89 | const behaviorDefaults = pick(Query.prototype[name], ...defaultValues); |
86 | 90 | const runtimeConfig = { |
@@ -115,13 +119,17 @@ export const registerBehavior = (behavior) => { |
115 | 119 |
|
116 | 120 | // behavior is stored in namespace like el.behavior |
117 | 121 | let instance = Behavior.getInstance(element, namespace); |
| 122 | + let existingInstance = !!instance; |
118 | 123 |
|
119 | 124 | const behaviorConfig = { |
120 | 125 | sharedBehavior, // setup can pass shared behavior across instances |
121 | 126 | $element, |
122 | 127 | Query, |
123 | 128 | ...runtimeConfig, |
124 | 129 | settings: runtimeSettings, |
| 130 | + // pass element index information |
| 131 | + elementIndex: index, |
| 132 | + totalElements: $elements.length, |
125 | 133 | }; |
126 | 134 |
|
127 | 135 | // create behavior instance if not defined |
@@ -149,7 +157,7 @@ export const registerBehavior = (behavior) => { |
149 | 157 | returnedValue = response; |
150 | 158 | } |
151 | 159 | } |
152 | | - else if (instance !== undefined) { |
| 160 | + else if (existingInstance) { |
153 | 161 | // if they are not calling a method and there are settings |
154 | 162 | // than they are attempting to reinitialize the behavior with new settings |
155 | 163 | instance.reinitialize(behaviorConfig); |
|
0 commit comments