zhiqim_floater.mobile.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。
  3. *
  4. * 指定登记&发行网站: https://www.zhiqim.com/ 欢迎加盟知启蒙,[编程有你,知启蒙一路随行]。
  5. *
  6. * 本文采用《知启蒙登记发行许可证》,除非符合许可证,否则不可使用该文件!
  7. * 1、您可以免费使用、修改、合并、出版发行和分发,再授权软件、软件副本及衍生软件;
  8. * 2、您用于商业用途时,必须在原作者指定的登记网站,按原作者要求进行登记;
  9. * 3、您在使用、修改、合并、出版发行和分发时,必须包含版权声明、许可声明,及保留原作者的著作权、商标和专利等知识产权;
  10. * 4、您在互联网、移动互联网等大众网络下发行和分发再授权软件、软件副本及衍生软件时,必须在原作者指定的发行网站进行发行和分发;
  11. * 5、您可以在以下链接获取一个完整的许可证副本。
  12. *
  13. * 许可证链接:http://zhiqim.org/licenses/zhiqim_register_publish_license.htm
  14. *
  15. * 除非法律需要或书面同意,软件由原始码方式提供,无任何明示或暗示的保证和条件。详见完整许可证的权限和限制。
  16. */
  17. +(function(Z)
  18. {
  19. /****************************************/
  20. //“操作”弹窗
  21. /****************************************/
  22. Z.Popup = Z.Class.newInstance();
  23. Z.Popup.cache = new Z.HashMap();
  24. Z.Popup.close = function(id, immediate)
  25. {
  26. id = id?((id.charAt(0)=="#")?id.substring(1):id):id;
  27. if (!id)
  28. {//删除全部
  29. Z.each(Z.Popup.cache.values(), function(zmPopup){
  30. zmPopup.fadeOut(immediate);
  31. });
  32. Z.Popup.cache.clear();
  33. }
  34. else
  35. {//找到id删除
  36. var zmPopup = Z.Popup.cache.get(id);
  37. if (zmPopup)
  38. {
  39. zmPopup.fadeOut(immediate);
  40. Z.Popup.cache.remove(id);
  41. }
  42. }
  43. };
  44. Z.Popup.get = function(id)
  45. {
  46. id = id?((id.charAt(0)=="#")?id.substring(1):id):id;
  47. return Z.Popup.cache.get(id);
  48. };
  49. Z.Popup.prototype =
  50. {
  51. //start of Z.Popup.prototype
  52. defaults:
  53. {//定义
  54. //基础参数
  55. target: null, //指定弹出对象
  56. start: "left", //弹窗起始位置:left/top/right/bottom
  57. overlay: "all", //覆盖部分:all/part
  58. overlayRatio: "75", //覆盖该主轴上的比例
  59. },
  60. validate: function()
  61. {//验证参数是否有误
  62. if (!this.target)
  63. {//null,或为空、0
  64. Z.alert("Z.Popup参数“target”未设置!");
  65. return false;
  66. }
  67. if (Z.T.isString(this.target) || Z.T.isObject(this.target))
  68. {//字符串类型,或者对象类型
  69. if (!Z(this.target)[0]) {
  70. Z.alert("Z.Popup参数“target”设置不正确!");
  71. return false;
  72. }
  73. }
  74. if (!Z.AR.contains(["left","top","right","bottom"],this.start)){
  75. this.start = "left";
  76. }
  77. if (!Z.AR.contains(["part","all"],this.overlay)){
  78. this.start = "part";
  79. }
  80. return true;
  81. },
  82. execute: function()
  83. {//执行
  84. if (!this.validate()){
  85. return;
  86. }
  87. this.id = Z(this.target).attr("id");
  88. if (!this.id){
  89. this.id = "Z_zmPopup_" + Z.Ids.uuid();
  90. Z(this.target).attr(this.id);
  91. }
  92. Z.Popup.cache.put(this.id,this);
  93. //定义popup
  94. this.creatPopup();
  95. //事件绑定
  96. this.attachEvent();
  97. //执行弹出
  98. this.fadeIn();
  99. },
  100. creatPopup: function()
  101. {//创建popup盒子
  102. this.$popup = Z(this.target);
  103. this.$popup.removeClass("z-overlay-all").removeClass("z-overlay-part")
  104. .addClass("z-overlay-" + this.overlay);
  105. this.$popup.removeClass("z-top").removeClass("z-right").removeClass("z-bottom").removeClass("z-left")
  106. .addClass("z-" + this.start).css("display","none");
  107. this.$bg = this.$popup.find(".z-popup-bg");
  108. this.$main = this.$popup.find(".z-popup-main");
  109. this.$title = this.$popup.find(".z-popup-title");
  110. this.$sure = this.$popup.find(".z-popup-sure");
  111. this.$cancel = this.$popup.find(".z-popup-cancel");
  112. this.$con = this.$popup.find(".z-popup-content");
  113. },
  114. attachEvent: function()
  115. {//事件绑定
  116. this.$bg.on("touchstart",this.fadeOut,this);
  117. this.$cancel.on("touchstart",this.fadeOut,this);
  118. if (this.$sure[0]){
  119. this.$sure[0].$this = this;
  120. this.$sure.on("touchstart",function(ev){
  121. this.$this.fadeOut(ev);
  122. })
  123. };
  124. var transitionEndEvent = this.transitionFixed();
  125. this.$main.on(transitionEndEvent,this.transitionEnd,this);
  126. },
  127. fadeIn: function()
  128. {//显示弹窗
  129. var that = this;
  130. var req = window.requestAnimationFrame
  131. || window.webkitRequestAnimationFrame
  132. || window.mozRequestAnimationFrame
  133. || window.oRequestAnimationFrame
  134. || function(fun){window.setTimeout(fun,1000/60)};
  135. req(function(){
  136. that.$popup.css("display","block");
  137. req(function(){
  138. that.$popup.addClass("z-popup-fadeIn");
  139. that = null;
  140. });
  141. });
  142. },
  143. fadeOut: function(ev)
  144. {//隐藏弹窗
  145. Z.E.forbidden(ev);
  146. this.$popup.removeClass("z-popup-fadeIn");
  147. this.$popup.addClass("z-popup-fadeOut");
  148. Z.Popup.cache.remove(this.id);
  149. if (Z(this.target).attr("id").indexOf("Z_zmPopup_") > -1){
  150. Z(this.target).removeAttr("id");
  151. }
  152. },
  153. transitionEnd: function(ev)
  154. {//动画结束,退出动画结束时触发
  155. if(this.$popup.hasClass("z-popup-fadeOut")){
  156. this.$popup.css("display","none");
  157. this.$popup.removeClass("z-popup-fadeOut");
  158. }
  159. },
  160. transitionFixed: function()
  161. {//兼容写法
  162. var $div = document.createElement('div');
  163. if ($div.style["transition"] !== undefined ){
  164. $div = null;
  165. return "transitionend";
  166. }
  167. if ($div.style["OTransition"] !== undefined ){
  168. $div = null;
  169. return "oTransitionEnd";
  170. }
  171. if ($div.style["WebkitTransition"] !== undefined ){
  172. $div = null;
  173. return "webkitTransitionEnd";
  174. }
  175. },
  176. //end of Z.Popup.prototype
  177. }
  178. /****************************************/
  179. //“操作列表”,底部展示/选择按钮
  180. /****************************************/
  181. Z.ActionList = Z.Class.newInstance();
  182. Z.ActionList.cache = new Z.HashMap();
  183. Z.ActionList.close = function(id, immediate)
  184. {
  185. id = id?((id.charAt(0)=="#")?id.substring(1):id):id;
  186. if (!id)
  187. {//删除全部
  188. Z.each(Z.ActionList.cache.values(), function(actionList){
  189. actionList.fadeOut(immediate);
  190. });
  191. Z.ActionList.cache.clear();
  192. }
  193. else
  194. {//找到id删除
  195. var actionList = Z.ActionList.cache.get(id);
  196. if (actionList)
  197. {
  198. actionList.fadeOut(immediate);
  199. Z.ActionList.cache.remove(id);
  200. }
  201. }
  202. };
  203. Z.ActionList.get = function(id)
  204. {
  205. id = id?((id.charAt(0)=="#")?id.substring(1):id):id;
  206. return Z.ActionList.cache.get(id);
  207. };
  208. Z.ActionList.prototype =
  209. {
  210. //start of Z.ActionList.prototype
  211. defaults:
  212. {//定义
  213. id: "", //按钮ID,可选
  214. html: "", //内容自定义
  215. height: .7, //高度限制
  216. },
  217. validate: function()
  218. {//验证参数是否有误
  219. if (Z.V.isEmpty(this.html)){
  220. Z.alert('Z.ActionList参数“html”未设置!');
  221. return false;
  222. }
  223. if (!this.id) {
  224. this.id = "Z_actionList_" + Z.Ids.uuid();
  225. }
  226. return true;
  227. },
  228. execute: function()
  229. {//执行
  230. if (!this.validate){
  231. return;
  232. }
  233. Z.ActionList.cache.put(this.id,this);
  234. //定义popup
  235. this.creatActionList();
  236. //事件绑定
  237. this.attachEvent();
  238. //执行弹出
  239. this.fadeIn();
  240. },
  241. creatActionList: function()
  242. {//创建popup盒子
  243. // language=HTML
  244. var listHtml = '<div class="z-popup z-overlay-part z-bottom"><div class="z-popup-bg"></div><div class="z-popup-main"><div class="z-popup-content"></div></div></div></div>';
  245. this.$action = Z(listHtml);
  246. this.$action.appendTo(Z("body"));
  247. //touchend 事件
  248. Z.ResetTouchEnd.load(this.$action);
  249. //数值按钮
  250. var $content = this.$action.find(".z-popup-content");
  251. $content.htmlc(this.html);
  252. if (this.html.match(/data-role\s*=\s*"z-numInput"/)){
  253. Z.ZmNumInput.load($content);
  254. }
  255. this.$bg = this.$action.find(".z-popup-bg");
  256. this.$main = this.$action.find(".z-popup-main");
  257. this.$con = this.$action.find(".z-popup-content");
  258. //计算高度
  259. var docHeight = parseInt(Z.D.clientHeight());
  260. var conHeight = docHeight * this.height;
  261. this.$con.css("height",conHeight)
  262. },
  263. attachEvent: function()
  264. {//事件绑定
  265. this.$bg.on("touchstart",this.fadeOut,this);
  266. var transitionEndEvent = this.transitionFixed();
  267. this.$main.on(transitionEndEvent,this.transitionEnd,this);
  268. },
  269. fadeIn: function()
  270. {//显示弹窗
  271. var that = this;
  272. var req = window.requestAnimationFrame
  273. || window.webkitRequestAnimationFrame
  274. || window.mozRequestAnimationFrame
  275. || window.oRequestAnimationFrame
  276. || function(fun){window.setTimeout(fun,1000/60)};
  277. req(function(){
  278. that.$action[0].style.display = "block";
  279. req(function(){
  280. that.$action.addClass("z-popup-fadeIn");
  281. });
  282. });
  283. },
  284. fadeOut: function()
  285. {//隐藏弹窗
  286. Z.ActionList.cache.remove(this.id);
  287. this.$action.removeClass("z-popup-fadeIn");
  288. this.$action.addClass("z-popup-fadeOut");
  289. },
  290. transitionEnd: function(ev)
  291. {//动画结束,退出动画结束时触发
  292. if(this.$action.hasClass("z-popup-fadeOut")){
  293. this.$action.remove();
  294. }
  295. },
  296. transitionFixed: function()
  297. {//兼容写法
  298. var $div = document.createElement('div');
  299. if ($div.style["transition"] !== undefined ){
  300. $div = null;
  301. return "transitionend";
  302. }
  303. if ($div.style["OTransition"] !== undefined ){
  304. $div = null;
  305. return "oTransitionEnd";
  306. }
  307. if ($div.style["WebkitTransition"] !== undefined ){
  308. $div = null;
  309. return "webkitTransitionEnd";
  310. }
  311. },
  312. //end of Z.ActionList.prototype
  313. }
  314. //END
  315. })(zhiqim);