diff --git a/CHANGES.md b/CHANGES.md index beee19fa7..0284cc9cb 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,7 @@ Features - Added support for a push subsystem using reethinkdb and horizon. That allows us to trigger an injection by sending a push_marker to all connected browsers. (This is still in an evaluation state) +- pat-forward: understand the trigger auto option Fixes ~~~~~ diff --git a/src/pat/forward/documentation.md b/src/pat/forward/documentation.md index 2ad85d7e5..f11e277ef 100644 --- a/src/pat/forward/documentation.md +++ b/src/pat/forward/documentation.md @@ -10,8 +10,17 @@ This pattern can be used to redirect a click on an element to one or more other Submit +If the trigger parameter is set to `auto`, the click event will be triggered +immediately after the page is loaded. + + Autosubmit form +
+ Submit +
+ ### Option reference | Property | Description | Default | Allowed Values | Type | |------|------|-----|------| -| selector | The element to which the click event should be forwarded. | | | CSS Selector | +| `selector` | The element to which the click event should be forwarded. | | | CSS Selector | +| `trigger` | When the forward action should be fired | `click` | `click|auto` | One of the mutually exclusive string values | diff --git a/src/pat/forward/forward.js b/src/pat/forward/forward.js index adcc98507..a179cf773 100644 --- a/src/pat/forward/forward.js +++ b/src/pat/forward/forward.js @@ -11,20 +11,24 @@ define([ var parser = new Parser("forward"); parser.addArgument("selector"); + parser.addArgument("trigger", "click", ["click", "auto"]); var _ = { name: "forward", trigger: ".pat-forward", init: function($el, opts) { - return $el.each(function() { + return $el.each(function () { var $el = $(this), - options = parser.parse($el, opts); + options = parser.parse($el, opts); if (!options.selector) - return; + return; $el.on("click", null, options.selector, _._onClick); + if (options.trigger === "auto") { + $el.trigger("click"); + } }); }, @@ -39,4 +43,3 @@ define([ }); // vim: sw=4 expandtab - diff --git a/src/pat/forward/index.html b/src/pat/forward/index.html index a99b31b43..f7a0b88c5 100644 --- a/src/pat/forward/index.html +++ b/src/pat/forward/index.html @@ -13,10 +13,16 @@ - + +
+ +
+ diff --git a/src/pat/forward/tests.js b/src/pat/forward/tests.js new file mode 100644 index 000000000..8cda65d1c --- /dev/null +++ b/src/pat/forward/tests.js @@ -0,0 +1,49 @@ +define(["pat-forward"], function(pattern) { + + describe("pat-forward", function() { + + beforeEach(function() { + $("
", {id: "lab"}).appendTo(document.body); + }); + + afterEach(function() { + $("#lab").remove(); + }); + + describe("Clicking on a button sends the click to another element", function() { + it("allows you to forward the click from an element to another one", function () { + var $lab = $("#lab"); + $lab.append($( + "
" + + ' ' + + ' ' + + "
" + )); + + pattern.init($(pattern.trigger)); + expect($lab.find("#checkbox").is(":checked")).toBeFalsy(); + $lab.find(".pat-forward").click(); + expect($lab.find("#checkbox").is(":checked")).toBeTruthy(); + }); + }); + + describe("Setting the trigger auto option triggers the click on init", function() { + it("allows you to forward the click authomatically when the pattern is initialized", function () { + var $lab = $("#lab"); + $lab.append($( + "
" + + ' ' + + ' ' + + "
" + )); + + pattern.init($(pattern.trigger)); + expect($lab.find("#checkbox").is(":checked")).toBeTruthy(); + $lab.find(".pat-forward").click(); + expect($lab.find("#checkbox").is(":checked")).toBeFalsy(); + }); + }); + + }); + +});