All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.gQueryImpl.js Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
function GQueryImpl() {
  var gSobject = gs.inherit(gs.baseClass,'GQueryImpl');
  gSobject.clazz = { name: 'org.grooscript.jquery.GQueryImpl', simpleName: 'GQueryImpl'};
  gSobject.clazz.superclass = { name: 'java.lang.Object', simpleName: 'Object'};
  gSobject.clazz.interfaces = [{ name: 'org.grooscript.jquery.GQuery', simpleName: 'GQuery'}];
  gSobject.bind = function(selector, target, nameProperty, closure) {
    if (closure === undefined) closure = null;
    var sourceDom = $(selector);
        //Create set method
        var nameSetMethod = 'set'+nameProperty.capitalize();

        if (sourceDom.is(":text")) {
            target[nameSetMethod] = function(newValue) {
                this[nameProperty] = newValue;
                sourceDom.val(newValue);
                if (closure) { closure(newValue); };
            };
            sourceDom.bind('input', function() {
                var currentVal = $(this).val();
                target[nameProperty] = currentVal;
                if (closure) { closure(currentVal); };
            });
        } else if (sourceDom.is('textarea')) {
            target[nameSetMethod] = function(newValue) {
                this[nameProperty] = newValue;
                sourceDom.val(newValue);
                if (closure) { closure(newValue); };
            };
            sourceDom.bind('input propertychange', function() {
                var currentVal = $(this).val();
                target[nameProperty] = currentVal;
                if (closure) { closure(currentVal); };
            });
        } else if (sourceDom.is(":checkbox")) {
            target[nameSetMethod] = function(newValue) {
                this[nameProperty] = newValue;
                sourceDom.prop('checked', newValue);
                if (closure) { closure(newValue); };
            };
            sourceDom.change(function() {
                var currentVal = $(this).is(':checked');
                target[nameProperty] = currentVal;
                if (closure) { closure(currentVal); };
            });
        } else if (sourceDom.is(":radio")) {
            target[nameSetMethod] = function(newValue) {
                this[nameProperty] = newValue;
                $(selector +'[value="' + newValue + '"]').prop('checked', true);
                if (closure) { closure(newValue); };
            };
            sourceDom.change(function() {
                var currentVal = $(this).val();
                target[nameProperty] = currentVal;
                if (closure) { closure(currentVal); };
            });
        } else if (sourceDom.is("select")) {
            target[nameSetMethod] = function(newValue) {
                this[nameProperty] = newValue;
                sourceDom.val(newValue);
                if (closure) { closure(newValue); };
            };
            sourceDom.bind('change', function() {
                var currentVal = $(this).val();
                target[nameProperty] = currentVal;
                if (closure) { closure(currentVal); };
            });
        } else {
            console.log('Not supporting bind for selector ' + selector);
        }
  }
  gSobject.existsId = function(id) {
    return $("#" + id).length > 0
  }
  gSobject.existsName = function(name) {
    return $("[name='" + name + "']").length > 0
  }
  gSobject.existsGroup = function(name) {
    return $("input:radio[name='" + name + "']").length > 0
  }
  gSobject.onEvent = function(selector, nameEvent, func) {
    $(selector).on(nameEvent, func);
  }
  gSobject.doRemoteCall = function(url, type, params, onSuccess, onFailure, objectResult) {
    if (objectResult === undefined) objectResult = null;
    $.ajax({
            type: type, //GET or POST
            data: gs.toJavascript(params),
            url: url,
            dataType: 'text'
        }).done(function(newData) {
            if (onSuccess) {
                onSuccess(gs.toGroovy(jQuery.parseJSON(newData), objectResult));
            }
        })
        .fail(function(error) {
            if (onFailure) {
                onFailure(error);
            }
        });
  }
  gSobject.onReady = function(func) {
    $(document).ready(func);
  }
  gSobject['attachMethodsToDomEvents'] = function(obj) {
    return gs.mc(gs.gp((obj = gs.metaClass(obj)),"methods"),"each",[function(method) {
      if (gs.mc(gs.gp(method,"name"),"endsWith",["Click"])) {
        var shortName = gs.mc(gs.gp(method,"name"),"substring",[0, gs.minus(gs.mc(gs.gp(method,"name"),"length",[]), 5)]);
        if (gs.mc(gSobject,"existsId",[shortName])) {
          gs.mc(gSobject,"onEvent",[gs.plus("#", shortName), "click", obj["" + (gs.gp(method,"name")) + ""]]);
        };
      };
      if (gs.mc(gs.gp(method,"name"),"endsWith",["Submit"])) {
        var shortName = gs.mc(gs.gp(method,"name"),"substring",[0, gs.minus(gs.mc(gs.gp(method,"name"),"length",[]), 6)]);
        if (gs.mc(gSobject,"existsId",[shortName])) {
          gs.mc(gSobject,"onEvent",[gs.plus("#", shortName), "submit", gs.mc(obj["" + (gs.gp(method,"name")) + ""],'leftShift', gs.list([function(it) {
            return gs.mc(it,"preventDefault",[]);
          }]))]);
        };
      };
      if (gs.mc(gs.gp(method,"name"),"endsWith",["Change"])) {
        var shortName = gs.mc(gs.gp(method,"name"),"substring",[0, gs.minus(gs.mc(gs.gp(method,"name"),"length",[]), 6)]);
        if (gs.mc(gSobject,"existsId",[shortName])) {
          return gs.mc(gSobject,"onChange",[shortName, obj["" + (gs.gp(method,"name")) + ""]]);
        };
      };
    }]);
  }
  gSobject.onChange = function(id, closure) {
    var sourceDom = $('#' + id);

        if (sourceDom.is(":text")) {
            sourceDom.bind('input', function() {
                closure($(this).val());
            });
        } else if (sourceDom.is('textarea')) {
            sourceDom.bind('input propertychange', function() {
                closure($(this).val());
            });
        } else if (sourceDom.is(":checkbox")) {
            sourceDom.change(function() {
                closure($(this).is(':checked'));
            });
        } else if (sourceDom.is(":radio")) {
            sourceDom.change(function() {
                closure($(this).val());
            });
        } else if (sourceDom.is("select")) {
            sourceDom.bind('change', function() {
                closure($(this).val());
            });
        } else {
            console.log('Not supporting onChange for id ' + id);
        }
  }
  gSobject.focusEnd = function(selector) {
    var sourceDom = $(selector);

        if (sourceDom) {
            if (sourceDom.is(":text") || sourceDom.is('textarea')) {
                var originalValue = sourceDom.val();
                sourceDom.val('');
                sourceDom.blur().focus().val(originalValue);
            } else {
                sourceDom.focus();
            }
        }
  }
  gSobject['bindAllProperties'] = function(target) {
    return gs.mc(gs.gp(target,"properties"),"each",[function(name, value) {
      if (gs.mc(gSobject,"existsId",[name])) {
        gs.mc(gSobject,"bind",["#" + (name) + "", target, name]);
      };
      if (gs.mc(gSobject,"existsName",[name])) {
        gs.mc(gSobject,"bind",["[name='" + (name) + "']", target, name]);
      };
      if (gs.mc(gSobject,"existsGroup",[name])) {
        return gs.mc(gSobject,"bind",["input:radio[name=" + (name) + "]", target, name]);
      };
    }]);
  }
  gSobject['bindAll'] = function(target) {
    gs.mc(gSobject,"bindAllProperties",[target]);
    return gs.mc(gSobject,"attachMethodsToDomEvents",[target]);
  }
  gSobject['observeEvent'] = function(selector, nameEvent, data) {
    if (data === undefined) data = gs.map();
    var observable = gs.execStatic(Observable,'listen', this,[]);
    gs.mc(gs.execCall(this, this, [selector]),"on",[nameEvent, data, function(event) {
      return gs.mc(observable,"produce",[event]);
    }]);
    return observable;
  }
  gSobject['call'] = function(selector) {
    return GQueryList(selector);
  }
  if (arguments.length == 1) {gs.passMapToObject(arguments[0],gSobject);};
  
  return gSobject;
};

function GQueryList() {
  var gSobject = gs.inherit(gs.baseClass,'GQueryList');
  gSobject.clazz = { name: 'org.grooscript.jquery.GQueryList', simpleName: 'GQueryList'};
  gSobject.clazz.superclass = { name: 'java.lang.Object', simpleName: 'Object'};
  gSobject.list = null;
  gSobject.jqueryList = function(selector) {
    return $(selector);
  }
  gSobject.methodMissing = function(name, args) {
    return gSobject.list[name].apply(gSobject.list, args);
  }
  gSobject['GQueryList1'] = function(selector) {
    gSobject.list = gs.mc(gSobject,"jqueryList",[selector]);
    return this;
  }
  if (arguments.length==1) {gSobject.GQueryList1(arguments[0]); }
  
  return gSobject;
};




© 2015 - 2024 Weber Informatics LLC | Privacy Policy