PageRenderTime 37ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/assets/js/jquery-impromptu.js

https://github.com/sphilp/com_weever
JavaScript | 357 lines | 283 code | 51 blank | 23 comment | 59 complexity | 57eb09975c1e2d3af4d5e33004fb6536 MD5 | raw file
  1. /*
  2. * jQuery Impromptu
  3. * By: Trent Richardson [http://trentrichardson.com]
  4. * Version 3.1
  5. * Last Modified: 3/30/2010
  6. *
  7. * Copyright 2011 Trent Richardson
  8. * Dual licensed under the MIT and GPL licenses.
  9. * http://trentrichardson.com/Impromptu/GPL-LICENSE.txt
  10. * http://trentrichardson.com/Impromptu/MIT-LICENSE.txt
  11. *
  12. */
  13. (function($) {
  14. $.prompt = function(message, options) {
  15. options = $.extend({},$.prompt.defaults,options);
  16. $.prompt.currentPrefix = options.prefix;
  17. var ie6 = ($.browser.msie && $.browser.version < 7);
  18. var $body = $(document.body);
  19. var $window = $(window);
  20. options.classes = $.trim(options.classes);
  21. if(options.classes != '')
  22. options.classes = ' '+ options.classes;
  23. //build the box and fade
  24. var msgbox = '<div class="'+ options.prefix +'box'+ options.classes +'" id="'+ options.prefix +'box">';
  25. if(options.useiframe && (($('object, applet').length > 0) || ie6)) {
  26. msgbox += '<iframe src="javascript:false;" style="display:block;position:absolute;z-index:-1;" class="'+ options.prefix +'fade" id="'+ options.prefix +'fade"></iframe>';
  27. } else {
  28. if(ie6) {
  29. $('select').css('visibility','hidden');
  30. }
  31. msgbox +='<div class="'+ options.prefix +'fade" id="'+ options.prefix +'fade"></div>';
  32. }
  33. msgbox += '<div class="'+ options.prefix +'" id="'+ options.prefix +'"><div class="'+ options.prefix +'container"><div class="';
  34. msgbox += options.prefix +'close">X</div><div id="'+ options.prefix +'states"></div>';
  35. msgbox += '</div></div></div>';
  36. var $jqib = $(msgbox).appendTo($body);
  37. var $jqi = $jqib.children('#'+ options.prefix);
  38. var $jqif = $jqib.children('#'+ options.prefix +'fade');
  39. //if a string was passed, convert to a single state
  40. if(message.constructor == String){
  41. message = {
  42. state0: {
  43. html: message,
  44. buttons: options.buttons,
  45. focus: options.focus,
  46. submit: options.submit
  47. }
  48. };
  49. }
  50. //build the states
  51. var states = "";
  52. $.each(message,function(statename,stateobj){
  53. stateobj = $.extend({},$.prompt.defaults.state,stateobj);
  54. message[statename] = stateobj;
  55. states += '<div id="'+ options.prefix +'_state_'+ statename +'" class="'+ options.prefix + '_state" style="display:none;"><div class="'+ options.prefix +'message">' + stateobj.html +'</div><div class="'+ options.prefix +'buttons">';
  56. $.each(stateobj.buttons, function(k, v){
  57. if(typeof v == 'object')
  58. states += '<button name="' + options.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" id="' + options.prefix + '_' + statename + '_button' + v.title.replace(/[^a-z0-9]+/gi,'') + '" value="' + v.value + '">' + v.title + '</button>';
  59. else states += '<button name="' + options.prefix + '_' + statename + '_button' + k + '" id="' + options.prefix + '_' + statename + '_button' + k + '" value="' + v + '">' + k + '</button>';
  60. });
  61. states += '</div></div>';
  62. });
  63. //insert the states...
  64. $jqi.find('#'+ options.prefix +'states').html(states).children('.'+ options.prefix +'_state:first').css('display','block');
  65. $jqi.find('.'+ options.prefix +'buttons:empty').css('display','none');
  66. //Events
  67. $.each(message,function(statename,stateobj){
  68. var $state = $jqi.find('#'+ options.prefix +'_state_'+ statename);
  69. $state.children('.'+ options.prefix +'buttons').children('button').click(function(){
  70. var msg = $state.children('.'+ options.prefix +'message');
  71. var clicked = stateobj.buttons[$(this).text()];
  72. if(clicked == undefined){
  73. for(var i in stateobj.buttons)
  74. if(stateobj.buttons[i].title == $(this).text())
  75. clicked = stateobj.buttons[i].value;
  76. }
  77. if(typeof clicked == 'object')
  78. clicked = clicked.value;
  79. var forminputs = {};
  80. //collect all form element values from all states
  81. $.each($jqi.find('#'+ options.prefix +'states :input').serializeArray(),function(i,obj){
  82. if (forminputs[obj.name] === undefined) {
  83. forminputs[obj.name] = obj.value;
  84. } else if (typeof forminputs[obj.name] == Array || typeof forminputs[obj.name] == 'object') {
  85. forminputs[obj.name].push(obj.value);
  86. } else {
  87. forminputs[obj.name] = [forminputs[obj.name],obj.value];
  88. }
  89. });
  90. var close = stateobj.submit(clicked,msg,forminputs);
  91. if(close === undefined || close) {
  92. removePrompt(true,clicked,msg,forminputs);
  93. }
  94. });
  95. $state.find('.'+ options.prefix +'buttons button:eq('+ stateobj.focus +')').addClass(options.prefix +'defaultbutton');
  96. });
  97. var ie6scroll = function(){
  98. $jqib.css({ top: $window.scrollTop() });
  99. };
  100. var fadeClicked = function(){
  101. if(options.persistent){
  102. var i = 0;
  103. $jqib.addClass(options.prefix +'warning');
  104. var intervalid = setInterval(function(){
  105. $jqib.toggleClass(options.prefix +'warning');
  106. if(i++ > 1){
  107. clearInterval(intervalid);
  108. $jqib.removeClass(options.prefix +'warning');
  109. }
  110. }, 100);
  111. }
  112. else {
  113. removePrompt();
  114. }
  115. };
  116. var keyPressEventHandler = function(e){
  117. var key = (window.event) ? event.keyCode : e.keyCode; // MSIE or Firefox?
  118. //escape key closes
  119. if(key==27) {
  120. fadeClicked();
  121. }
  122. //constrain tabs
  123. if (key == 9){
  124. var $inputels = $(':input:enabled:visible',$jqib);
  125. var fwd = !e.shiftKey && e.target == $inputels[$inputels.length-1];
  126. var back = e.shiftKey && e.target == $inputels[0];
  127. if (fwd || back) {
  128. setTimeout(function(){
  129. if (!$inputels)
  130. return;
  131. var el = $inputels[back===true ? $inputels.length-1 : 0];
  132. if (el)
  133. el.focus();
  134. },10);
  135. return false;
  136. }
  137. }
  138. };
  139. var positionPrompt = function(){
  140. $jqib.css({
  141. position: (ie6) ? "absolute" : "fixed",
  142. height: $window.height(),
  143. width: "100%",
  144. top: (ie6)? $window.scrollTop() : 0,
  145. left: 0,
  146. right: 0,
  147. bottom: 0
  148. });
  149. $jqif.css({
  150. position: "absolute",
  151. height: $window.height(),
  152. width: "100%",
  153. top: 0,
  154. left: 0,
  155. right: 0,
  156. bottom: 0
  157. });
  158. $jqi.css({
  159. position: "absolute",
  160. top: options.top,
  161. left: "50%",
  162. marginLeft: (($jqi.outerWidth()/2)*-1)
  163. });
  164. };
  165. var stylePrompt = function(){
  166. $jqif.css({
  167. zIndex: options.zIndex,
  168. display: "none",
  169. opacity: options.opacity
  170. });
  171. $jqi.css({
  172. zIndex: options.zIndex+1,
  173. display: "none"
  174. });
  175. $jqib.css({
  176. zIndex: options.zIndex
  177. });
  178. };
  179. var removePrompt = function(callCallback, clicked, msg, formvals){
  180. $jqi.remove();
  181. //ie6, remove the scroll event
  182. if(ie6) {
  183. $body.unbind('scroll',ie6scroll);
  184. }
  185. $window.unbind('resize',positionPrompt);
  186. $jqif.fadeOut(options.overlayspeed,function(){
  187. $jqif.unbind('click',fadeClicked);
  188. $jqif.remove();
  189. if(callCallback) {
  190. options.callback(clicked,msg,formvals);
  191. }
  192. $jqib.unbind('keypress',keyPressEventHandler);
  193. $jqib.remove();
  194. if(ie6 && !options.useiframe) {
  195. $('select').css('visibility','visible');
  196. }
  197. });
  198. };
  199. positionPrompt();
  200. stylePrompt();
  201. //ie6, add a scroll event to fix position:fixed
  202. if(ie6) {
  203. $window.scroll(ie6scroll);
  204. }
  205. $jqif.click(fadeClicked);
  206. $window.resize(positionPrompt);
  207. $jqib.bind("keydown keypress",keyPressEventHandler);
  208. $jqi.find('.'+ options.prefix +'close').click(removePrompt);
  209. //Show it
  210. $jqif.fadeIn(options.overlayspeed);
  211. $jqi[options.show](options.promptspeed,options.loaded);
  212. $jqi.find('#'+ options.prefix +'states .'+ options.prefix +'_state:first .'+ options.prefix +'defaultbutton').focus();
  213. if(options.timeout > 0)
  214. setTimeout($.prompt.close,options.timeout);
  215. return $jqib;
  216. };
  217. $.prompt.defaults = {
  218. prefix:'jqi',
  219. classes: '',
  220. buttons: {
  221. Ok: true
  222. },
  223. loaded: function(){
  224. },
  225. submit: function(){
  226. return true;
  227. },
  228. callback: function(){
  229. },
  230. opacity: 0.6,
  231. zIndex: 999,
  232. overlayspeed: 'slow',
  233. promptspeed: 'fast',
  234. show: 'fadeIn',
  235. focus: 0,
  236. useiframe: false,
  237. top: "15%",
  238. persistent: true,
  239. timeout: 0,
  240. state: {
  241. html: '',
  242. buttons: {
  243. Ok: true
  244. },
  245. focus: 0,
  246. submit: function(){
  247. return true;
  248. }
  249. }
  250. };
  251. $.prompt.currentPrefix = $.prompt.defaults.prefix;
  252. $.prompt.setDefaults = function(o) {
  253. $.prompt.defaults = $.extend({}, $.prompt.defaults, o);
  254. };
  255. $.prompt.setStateDefaults = function(o) {
  256. $.prompt.defaults.state = $.extend({}, $.prompt.defaults.state, o);
  257. };
  258. $.prompt.getStateContent = function(state) {
  259. return $('#'+ $.prompt.currentPrefix +'_state_'+ state);
  260. };
  261. $.prompt.getCurrentState = function() {
  262. return $('.'+ $.prompt.currentPrefix +'_state:visible');
  263. };
  264. $.prompt.getCurrentStateName = function() {
  265. var stateid = $.prompt.getCurrentState().attr('id');
  266. return stateid.replace($.prompt.currentPrefix +'_state_','');
  267. };
  268. $.prompt.goToState = function(state, callback) {
  269. $('.'+ $.prompt.currentPrefix +'_state').slideUp('slow');
  270. $('#'+ $.prompt.currentPrefix +'_state_'+ state).slideDown('slow',function(){
  271. $(this).find('.'+ $.prompt.currentPrefix +'defaultbutton').focus();
  272. if (typeof callback == 'function')
  273. callback();
  274. });
  275. };
  276. $.prompt.nextState = function(callback) {
  277. var $next = $('.'+ $.prompt.currentPrefix +'_state:visible').next();
  278. $('.'+ $.prompt.currentPrefix +'_state').slideUp('slow');
  279. $next.slideDown('slow',function(){
  280. $next.find('.'+ $.prompt.currentPrefix +'defaultbutton').focus();
  281. if (typeof callback == 'function')
  282. callback();
  283. });
  284. };
  285. $.prompt.prevState = function(callback) {
  286. var $next = $('.'+ $.prompt.currentPrefix +'_state:visible').prev();
  287. $('.'+ $.prompt.currentPrefix +'_state').slideUp('slow');
  288. $next.slideDown('slow',function(){
  289. $next.find('.'+ $.prompt.currentPrefix +'defaultbutton').focus();
  290. if (typeof callback == 'function')
  291. callback();
  292. });
  293. };
  294. $.prompt.close = function() {
  295. $('#'+ $.prompt.currentPrefix +'box').fadeOut('fast',function(){
  296. $(this).remove();
  297. });
  298. };
  299. $.fn.prompt = function(options){
  300. if(options == undefined)
  301. options = {};
  302. if(options.withDataAndEvents == undefined)
  303. options.withDataAndEvents = false;
  304. $.prompt($(this).clone(options.withDataAndEvents).html(),options);
  305. }
  306. })(jQuery);