/*
 * Copyright 2011-2019 Kay Stenschke
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kstenschke.shifter.models.shiftable_types;

/**
 * JavaScript Variables (multi-lined declarations of multiple vars)
 */
public class JqueryObserver {

    public static final String ACTION_TEXT = "Shift jQuery Observer";

    /**
     * Check whether given string represents a declaration of (multiple) JS variables:
     * -selection has multiple lines
     * -each trimmed line starts w/ "var" (at least 2 occurrences)
     * -each trimmed line ends w/ ";"
     * -there can be empty lines
     * -there can be commented lines, beginning w/ "//"
     *
     * @param  str     String to be checked
     * @return boolean
     */
    public static Boolean isJQueryObserver(String str) {
        if (str.startsWith(".")) {
            str = str.substring(1);
        }

        return str.equals("blur(")
            || str.equals("change(")
            || str.equals("click(")
            || str.equals("dblclick(")
            || str.equals("error(")
            || str.equals("focus(")
            || str.equals("keypress(")
            || str.equals("keydown(")
            || str.equals("keyup(")
            || str.equals("load(")
            || str.equals("mouseenter(")
            || str.equals("mouseleave(")
            || str.equals("resize(")
            || str.equals("submit(")
            || str.equals("scroll(")
            || str.equals("unload(");
    }

    /**
     * @param  str      text selection to be shifted
     * @return String
     */
    public static String getShifted(String str) {
        boolean startsWithDot = str.startsWith(".");
        if (startsWithDot) {
            str = str.substring(1);
        }
        String eventName = str.replace("(", "");

        return (startsWithDot ? "." : "") + "on('" + eventName + "', ";
    }
}
@@ -1387,27 +1387,31 @@
  return 
-str
+%22blur(%22
 .equals(
 %22blur(%22)
@@ -1402,23 +1402,19 @@
 .equals(
-%22blur(%22
+str
 )%0A      
@@ -1426,288 +1426,288 @@
  %7C%7C 
-str.equals(%22change(%22)%0A            %7C%7C str.equals(%22click(%22)%0A            %7C%7C str.equals(%22dblclick(%22)%0A            %7C%7C str.equals(%22error(%22)%0A            %7C%7C str.equals(%22focus(%22)%0A            %7C%7C str.equals(%22keypress(%22)%0A            %7C%7C str.equals(%22keydown(%22)%0A            %7C%7C str.equals(%22keyup(%22
+%22change(%22.equals(str)%0A            %7C%7C %22click(%22.equals(str)%0A            %7C%7C %22dblclick(%22.equals(str)%0A            %7C%7C %22error(%22.equals(str)%0A            %7C%7C %22focus(%22.equals(str)%0A            %7C%7C %22keypress(%22.equals(str)%0A            %7C%7C %22keydown(%22.equals(str)%0A            %7C%7C %22keyup(%22.equals(str
 )%0A  
@@ -1719,27 +1719,31 @@
      %7C%7C 
-str
+%22load(%22
 .equals(
 %22load(%22)
@@ -1734,23 +1734,19 @@
 .equals(
-%22load(%22
+str
 )%0A      
@@ -1750,35 +1750,24 @@
          %7C%7C 
-str.equals(
 %22mouseenter(
@@ -1767,16 +1767,27 @@
 eenter(%22
+.equals(str
 )%0A      
@@ -1795,27 +1795,16 @@
      %7C%7C 
-str.equals(
 %22mousele
@@ -1808,16 +1808,27 @@
 eleave(%22
+.equals(str
 )%0A      
@@ -1840,139 +1840,139 @@
  %7C%7C 
-str.equals(%22resize(%22)%0A            %7C%7C str.equals(%22submit(%22)%0A            %7C%7C str.equals(%22scroll(%22)%0A            %7C%7C str.equals(%22unload(%22
+%22resize(%22.equals(str)%0A            %7C%7C %22submit(%22.equals(str)%0A            %7C%7C %22scroll(%22.equals(str)%0A            %7C%7C %22unload(%22.equals(str
 );%0A 

fix comparison style