Skip to content

Commit

Permalink
Merge branch 'FLUID-6180'
Browse files Browse the repository at this point in the history
* FLUID-6180:
  FLUID-6180: Cleaning up getClosestPanelIndex function.
  FLUID-6180: Adding debounce delay to options.
  FLUID-6180: using fluid.debounce for scroll event
  FLUID-6180: Adding debounce function to infusion
  FLUID-6180: Renamed source from "manualScroll" to "scrollEvent"
  FLUID-6180: Correcting typo
  FLUID-6180: Adding unit tests
  FLUID-6180: Adding in perstence for manual scrolling.
  • Loading branch information
amb26 committed Nov 2, 2017
2 parents 17fbd6f + 25a7c90 commit 328153e
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 4 deletions.
34 changes: 34 additions & 0 deletions src/framework/core/js/Fluid.js
Expand Up @@ -23,6 +23,11 @@ Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
Includes code from Underscore.js 1.4.3
http://underscorejs.org
(c) 2009-2012 Jeremy Ashkenas, DocumentCloud Inc.
Underscore may be freely distributed under the MIT license.
*/

/* global console */
Expand Down Expand Up @@ -859,6 +864,35 @@ var fluid = fluid || fluid_3_0_0;
}
};

/**
* Copied from Underscore.js 1.4.3 - see licence at head of this file
*
* Will execute the passed in function after the specified about of time since it was last executed.
* @param {Function} func - the function to execute
* @param {Number} wait - the number of milliseconds to wait before executing the function
* @param {Boolean} immediate - Whether to trigger the function at the start (true) or end (false) of
* the wait interval.
*/
fluid.debounce = function (func, wait, immediate) {
var timeout, result;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
if (!immediate) {
result = func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
result = func.apply(context, args);
}
return result;
};
};

/** Calls Object.freeze at each level of containment of the supplied object
* @return The supplied argument, recursively frozen
*/
Expand Down
41 changes: 39 additions & 2 deletions src/framework/preferences/js/ArrowScrolling.js
Expand Up @@ -27,12 +27,14 @@ var fluid_3_0_0 = fluid_3_0_0 || {};
// panels: "", // should be supplied by the fluid.prefs.prefsEditor grade.
scrollContainer: ".flc-prefsEditor-scrollContainer"
},
onScrollDelay: 100, // in ms, used to set the delay for debouncing the scroll event relay
model: {
// panelMaxIndex: null, // determined by the number of panels calculated after the onPrefsEditorMarkupReady event fired
panelIndex: 0
},
events: {
beforeReset: null // should be fired by the fluid.prefs.prefsEditor grade
beforeReset: null, // should be fired by the fluid.prefs.prefsEditor grade
onScroll: null
},
modelRelay: {
target: "panelIndex",
Expand All @@ -49,10 +51,23 @@ var fluid_3_0_0 = fluid_3_0_0 || {};
"panelIndex": {
listener: "fluid.prefs.arrowScrolling.scrollToPanel",
args: ["{that}", "{change}.value"],
excludeSource: ["scrollEvent"],
namespace: "scrollToPanel"
}
},
listeners: {
"onReady.scrollEvent": {
"this": "{that}.dom.scrollContainer",
method: "scroll",
args: [{
expander: {
// Relaying the scroll event to onScroll but debounced to reduce the rate of fire. A high rate
// of fire may negatively effect performance for complex handlers.
func: "fluid.debounce",
args: ["{that}.events.onScroll.fire", "{that}.options.onScrollDelay"]
}
}]
},
"onReady.windowResize": {
"this": window,
method: "addEventListener",
Expand All @@ -77,6 +92,16 @@ var fluid_3_0_0 = fluid_3_0_0 || {};
"beforeReset.resetPanelIndex": {
listener: "{that}.applier.fireChangeRequest",
args: {path: "panelIndex", value: 0, type: "ADD", source: "reset"}
},
"onScroll.setPanelIndex": {
changePath: "panelIndex",
value: {
expander: {
funcName: "fluid.prefs.arrowScrolling.getClosestPanelIndex",
args: "{that}.dom.panels"
}
},
source: "scrollEvent"
}
},
invokers: {
Expand All @@ -95,7 +120,6 @@ var fluid_3_0_0 = fluid_3_0_0 || {};
},
target: "{that > fluid.prefs.panel}.options.listeners"
}]

});

fluid.prefs.arrowScrolling.calculatePanelMaxIndex = function (panels) {
Expand All @@ -118,4 +142,17 @@ var fluid_3_0_0 = fluid_3_0_0 || {};
}
};

fluid.prefs.arrowScrolling.getClosestPanelIndex = function (panels) {
var panelArray = fluid.transform(panels, function (panel, idx) {
return {
index: idx,
offset: Math.abs($(panel).offset().left)
};
});
panelArray.sort(function (a, b) {
return a.offset - b.offset;
});
return fluid.get(panelArray, ["0", "index"]) || 0;
};

})(jQuery, fluid_3_0_0);
21 changes: 21 additions & 0 deletions tests/framework-tests/core/js/FluidJSTests.js
Expand Up @@ -307,6 +307,27 @@ https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
});
});

fluid.tests.debounceTests = [1, 2, 3, 4 ,5];

jqUnit.asyncTest("fluid.debounce", function () {
var result = {};
var lead = fluid.debounce(function (val) {
result.lead = val;
}, 3, true);
var trail = fluid.debounce(function (val) {
result.trail = val;
}, 3);

setTimeout(function () {
jqUnit.assertEquals("The first value should be returned when accepting the leading response", fluid.tests.debounceTests[0], result.lead);
jqUnit.assertEquals("The last value should be returned when accepting the trailing response", fluid.tests.debounceTests[4], result.trail);
jqUnit.start();
}, 5);

fluid.each(fluid.tests.debounceTests, lead);
fluid.each(fluid.tests.debounceTests, trail);
});

jqUnit.test("merge", function () {
jqUnit.expect(8);

Expand Down
Expand Up @@ -182,6 +182,8 @@ https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
jqUnit.assertNotEquals(testName + ": The panel at index " + idx + " should not be scrolled into view and have an offset greater or less than 0", 0, panelOffset);
}
});

jqUnit.assertEquals("fluid.prefs.arrowScrolling.getClosestPanelIndex should return " + panelIndex, panelIndex, fluid.prefs.arrowScrolling.getClosestPanelIndex(panels));
};

fluid.tests.prefs.responsive.clickArrow = function (elm, direction) {
Expand Down Expand Up @@ -212,7 +214,7 @@ https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
modules: [{
name: "Separated panel integration tests",
tests: [{
expect: 70,
expect: 77,
name: "Separated panel integration tests",
sequenceGrade: "fluid.tests.prefs.responsive.iframeSequence",
sequence: [{
Expand Down Expand Up @@ -265,6 +267,13 @@ https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
listener: "jqUnit.assert",
event: "{separatedPanel}.prefsEditor.events.onSignificantDOMChange",
args: ["A window resize event triggered the onSignificantDOMChange event"]
}, {
jQueryTrigger: "scroll",
element: "{separatedPanel}.prefsEditor.dom.scrollContainer"
}, {
listener: "jqUnit.assert",
event: "{separatedPanel}.prefsEditor.events.onScroll",
args: ["A scroll event triggered the onScroll event"]
}]
}]
}]
Expand Down Expand Up @@ -305,7 +314,7 @@ https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
modules: [{
name: "Separated panel initial panelIndex tester",
tests: [{
expect: 39,
expect: 40,
name: "Separated panel initial panelIndex tester",
sequenceGrade: "fluid.tests.prefs.responsive.iframeSequence",
sequence: [{
Expand Down

0 comments on commit 328153e

Please sign in to comment.