001/* 002 * #%L 003 * GwtMaterial 004 * %% 005 * Copyright (C) 2015 - 2017 GwtMaterialDesign 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package gwt.material.design.addins.client.scrollfire; 021 022import com.google.gwt.core.client.GWT; 023import com.google.gwt.dom.client.Element; 024import com.google.gwt.user.client.DOM; 025import gwt.material.design.addins.client.MaterialAddins; 026import gwt.material.design.addins.client.scrollfire.js.JsScrollfire; 027import gwt.material.design.client.MaterialDesignBase; 028import gwt.material.design.jquery.client.api.Functions; 029 030import static gwt.material.design.jquery.client.api.JQuery.$; 031 032//@formatter:off 033 034/** 035 * Material Scrollfire - executes callback functions depending on how far into the page you've scrolled. 036 * <p> 037 * <h3>Java Usage:</h3> 038 * <p> 039 * <pre> 040 * {@code 041 * // CAN BE CALLED AS A HELPER STATIC CONTEXT 042 * MaterialScrollfire.apply(Element e, Runnable runnableCallback); 043 * 044 * // INSTANTIATE THE PUSHPIN COMPONENT 045 * MaterialScrollfire scrollfire = new MaterialScrollfire(); 046 * scrollfire.setElement(element); 047 * scrollfire.setCallback(callback); 048 * scrollfire.setOffset(offset); 049 * scrollfire.apply(); 050 * } 051 * </pre> 052 * 053 * @author kevzlou7979 054 * @see <a href="http://gwtmaterialdesign.github.io/gwt-material-demo/#scrollfire">Material Scrollfire</a> 055 */ 056//@formatter:on 057public class MaterialScrollfire { 058 059 static { 060 if (MaterialAddins.isDebug()) { 061 MaterialDesignBase.injectDebugJs(MaterialScrollfireDebugClientBundle.INSTANCE.scrollfireDebugJs()); 062 } else { 063 MaterialDesignBase.injectJs(MaterialScrollfireClientBundle.INSTANCE.scrollfireJs()); 064 } 065 } 066 067 private int offset = 100; 068 private Element element; 069 private Functions.Func callback; 070 071 public MaterialScrollfire() { 072 } 073 074 public MaterialScrollfire(Element element) { 075 setElement(element); 076 } 077 078 /** 079 * Executes callback method depending on how far into the page you've scrolled 080 */ 081 public void apply() { 082 if (element != null) { 083 String uid = DOM.createUniqueId(); 084 element.setId(uid); 085 JsScrollfire.apply("#" + uid, offset, callback::call); 086 } else { 087 GWT.log("You must set the element before applying the scrollfire", new IllegalStateException()); 088 } 089 } 090 091 /** 092 * Executes callback method depending on how far into the page you've scrolled 093 * 094 * @param element Target element that is being tracked 095 * @param callback The method to be called when the scrollfire is applied 096 */ 097 public static void apply(Element element, Functions.Func callback) { 098 apply(element, 100, callback); 099 } 100 101 /** 102 * Executes callback method depending on how far into the page you've scrolled 103 * 104 * @param element Target element that is being tracked 105 * @param offset If this is 0, the callback will be fired when the selector element is at the very bottom of the user's window. 106 * @param callback The method to be called when the scrollfire is applied 107 */ 108 public static void apply(Element element, int offset, Functions.Func callback) { 109 MaterialScrollfire scrollfire = new MaterialScrollfire(); 110 scrollfire.setElement(element); 111 scrollfire.setCallback(callback); 112 scrollfire.setOffset(offset); 113 scrollfire.apply(); 114 } 115 116 /** 117 * Executes callback method depending on how far into the page you've scrolled 118 */ 119 public static void apply(String selector, Functions.Func callback) { 120 apply($(selector).asElement(), 100, callback); 121 } 122 123 /** 124 * Get the target element that is being tracked. 125 */ 126 public Element getElement() { 127 return element; 128 } 129 130 /** 131 * Set the target element that is being tracked. 132 */ 133 public void setElement(Element element) { 134 this.element = element; 135 } 136 137 /** 138 * Get the offset value 139 */ 140 public int getOffset() { 141 return offset; 142 } 143 144 /** 145 * Set the offset value 146 */ 147 public void setOffset(int offset) { 148 this.offset = offset; 149 } 150 151 /** 152 * Get the method to be called when the scrollfire is applied 153 */ 154 public Functions.Func getCallback() { 155 return callback; 156 } 157 158 /** 159 * Set the method to be called when the scrollfire is applied 160 */ 161 public void setCallback(Functions.Func callback) { 162 this.callback = callback; 163 } 164}