/*jslint forin:true sub:true anon:true, sloppy:true, stupid:true nomen:true, node:true continue:true*/

/*
* Copyright (c) 2012, Yahoo! Inc.  All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/

var util = require("util");
var log4js = require("log4js");
var Controller = require("../interface/controller");

function LocatorController(testConfig, testParams, driver) {
    Controller.call(this, testConfig, testParams, driver);

    this.logger = log4js.getLogger("LocatorController");
}

util.inherits(LocatorController, Controller);

LocatorController.prototype.execute = function (callback) {
    var self = this,
        config = this.testConfig,
        params = this.testParams,
        logger = this.logger,
        webdriver,
        locator,
        strategy,
        target,
        elem;

    webdriver = self.driver.getWebDriver(callback);
    if (!webdriver) {
        callback("Locator controller is only supported for the selenium driver");
        return;
    }

    target = params["value"];
    if (!target) {
        callback("\"value\" parameter is required");
        return;
    }
    strategy = params["using"];
    if (!strategy) {
        strategy = "css";
    }

    function done() {
        logger.info("done");
        callback();
    }

    locator = webdriver.By[strategy](target);
    function findAndAct() {
        elem = webdriver.findElement(locator);
        logger.info("Finding element: By " + strategy + " (" + target + ")");
        if (true === params["click"]) {
            elem.click();
            webdriver.waitForNextPage().then(done);
        } else {
            var sendKeys = params["text"];
            if (sendKeys) {
                elem.clear();
                elem.sendKeys(sendKeys).then(done);
            } else {
                done();
            }
        }
    }

    if (true === params["wait"]) {
        webdriver.waitForElementPresent(locator).then(findAndAct);
    } else {
        findAndAct();
    }
};

module.exports = LocatorController;


@@ -847,16 +847,30 @@
 target,%0A
+        stay,%0A
         
@@ -875,16 +875,16 @@
   elem;%0A
-
 %0A    web
@@ -1260,25 +1260,577 @@
 css%22;%0A    %7D%0A
+    %0A    /**%0A     * @param: stay%0A     * @purpose: passed, override default behavior of click requiring new page load%0A     *   (for in-page interactions)%0A     * @example: click on an element, to open a menu%0A     *%0A     * @usage:      %0A             %7B%0A                %22controller%22: %22locator%22,%0A                %22params%22: %7B%0A                    %22value%22: %22.yom-menu-switch%22,%0A                    %22click%22 : true,%0A                    %22stay%22 : true%0A                %7D%0A            %7D%0A     */%0A    stay = params%5B%22stay%22%5D;%0A    if (!stay) %7B%0A        stay = true;%0A    %7D%0A    
 %0A
-
     function
@@ -2150,22 +2150,116 @@
     
-elem.click();%0A
+if (stay) %7B%0A                elem.click().then(done);%0A            %7D else %7B%0A                elem.click();%0A    
     
@@ -2298,32 +2298,46 @@
 e().then(done);%0A
+            %7D%0A
         %7D else %7B
@@ -2713,16 +2713,16 @@
   %7D%0A%7D;%0A%0A
+
 module.e
@@ -2749,9 +2749,8 @@
 roller;%0A
-%0A

add in-page functionality to locator controllers click feature