PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/panel/js/neon-login.js

https://github.com/skydotint/kawsar
JavaScript | 391 lines | 257 code | 94 blank | 40 comment | 13 complexity | e25e566a3bcc6c1328343dbd111051e5 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. /**
  2. * Neon Login Script
  3. *
  4. * Developed by Arlind Nushi - www.laborator.co
  5. */
  6. var neonLogin = neonLogin || {};
  7. ;(function($, window, undefined)
  8. {
  9. "use strict";
  10. $(document).ready(function()
  11. {
  12. neonLogin.$container = $("#form_login");
  13. // Login Form & Validation
  14. neonLogin.$container.validate({
  15. rules: {
  16. username: {
  17. required: true
  18. },
  19. password: {
  20. required: true
  21. },
  22. },
  23. highlight: function(element){
  24. $(element).closest('.input-group').addClass('validate-has-error');
  25. },
  26. unhighlight: function(element)
  27. {
  28. $(element).closest('.input-group').removeClass('validate-has-error');
  29. },
  30. submitHandler: function(ev)
  31. {
  32. /*
  33. Updated on v1.1.4
  34. Login form now processes the login data, here is the file: data/sample-login-form.php
  35. */
  36. $(".login-page").addClass('logging-in'); // This will hide the login form and init the progress bar
  37. // Hide Errors
  38. $(".form-login-error").slideUp('fast');
  39. // We will wait till the transition ends
  40. setTimeout(function()
  41. {
  42. var random_pct = 25 + Math.round(Math.random() * 30);
  43. // The form data are subbmitted, we can forward the progress to 70%
  44. neonLogin.setPercentage(40 + random_pct);
  45. // Send data to the server
  46. $.ajax({
  47. url: baseurl + 'data/sample-login-form.php',
  48. method: 'POST',
  49. dataType: 'json',
  50. data: {
  51. username: $("input#username").val(),
  52. password: $("input#password").val(),
  53. },
  54. error: function()
  55. {
  56. alert("An error occoured!");
  57. },
  58. success: function(response)
  59. {
  60. // Login status [success|invalid]
  61. var login_status = response.login_status;
  62. // Form is fully completed, we update the percentage
  63. neonLogin.setPercentage(100);
  64. // We will give some time for the animation to finish, then execute the following procedures
  65. setTimeout(function()
  66. {
  67. // If login is invalid, we store the
  68. if(login_status == 'invalid')
  69. {
  70. $(".login-page").removeClass('logging-in');
  71. neonLogin.resetProgressBar(true);
  72. }
  73. else
  74. if(login_status == 'success')
  75. {
  76. // Redirect to login page
  77. setTimeout(function()
  78. {
  79. var redirect_url = baseurl;
  80. if(response.redirect_url && response.redirect_url.length)
  81. {
  82. redirect_url = response.redirect_url;
  83. }
  84. window.location.href = redirect_url;
  85. }, 400);
  86. }
  87. }, 1000);
  88. }
  89. });
  90. }, 650);
  91. }
  92. });
  93. // Lockscreen & Validation
  94. var is_lockscreen = $(".login-page").hasClass('is-lockscreen');
  95. if(is_lockscreen)
  96. {
  97. neonLogin.$container = $("#form_lockscreen");
  98. neonLogin.$ls_thumb = neonLogin.$container.find('.lockscreen-thumb');
  99. neonLogin.$container.validate({
  100. rules: {
  101. password: {
  102. required: true
  103. },
  104. },
  105. highlight: function(element){
  106. $(element).closest('.input-group').addClass('validate-has-error');
  107. },
  108. unhighlight: function(element)
  109. {
  110. $(element).closest('.input-group').removeClass('validate-has-error');
  111. },
  112. submitHandler: function(ev)
  113. {
  114. /*
  115. Demo Purpose Only
  116. Here you can handle the page login, currently it does not process anything, just fills the loader.
  117. */
  118. $(".login-page").addClass('logging-in-lockscreen'); // This will hide the login form and init the progress bar
  119. // We will wait till the transition ends
  120. setTimeout(function()
  121. {
  122. var random_pct = 25 + Math.round(Math.random() * 30);
  123. neonLogin.setPercentage(random_pct, function()
  124. {
  125. // Just an example, this is phase 1
  126. // Do some stuff...
  127. // After 0.77s second we will execute the next phase
  128. setTimeout(function()
  129. {
  130. neonLogin.setPercentage(100, function()
  131. {
  132. // Just an example, this is phase 2
  133. // Do some other stuff...
  134. // Redirect to the page
  135. setTimeout("window.location.href = '../../'", 600);
  136. }, 2);
  137. }, 820);
  138. });
  139. }, 650);
  140. }
  141. });
  142. }
  143. // Login Form Setup
  144. neonLogin.$body = $(".login-page");
  145. neonLogin.$login_progressbar_indicator = $(".login-progressbar-indicator h3");
  146. neonLogin.$login_progressbar = neonLogin.$body.find(".login-progressbar div");
  147. neonLogin.$login_progressbar_indicator.html('0%');
  148. if(neonLogin.$body.hasClass('login-form-fall'))
  149. {
  150. var focus_set = false;
  151. setTimeout(function(){
  152. neonLogin.$body.addClass('login-form-fall-init')
  153. setTimeout(function()
  154. {
  155. if( !focus_set)
  156. {
  157. neonLogin.$container.find('input:first').focus();
  158. focus_set = true;
  159. }
  160. }, 550);
  161. }, 0);
  162. }
  163. else
  164. {
  165. neonLogin.$container.find('input:first').focus();
  166. }
  167. // Focus Class
  168. neonLogin.$container.find('.form-control').each(function(i, el)
  169. {
  170. var $this = $(el),
  171. $group = $this.closest('.input-group');
  172. $this.prev('.input-group-addon').click(function()
  173. {
  174. $this.focus();
  175. });
  176. $this.on({
  177. focus: function()
  178. {
  179. $group.addClass('focused');
  180. },
  181. blur: function()
  182. {
  183. $group.removeClass('focused');
  184. }
  185. });
  186. });
  187. // Functions
  188. $.extend(neonLogin, {
  189. setPercentage: function(pct, callback)
  190. {
  191. pct = parseInt(pct / 100 * 100, 10) + '%';
  192. // Lockscreen
  193. if(is_lockscreen)
  194. {
  195. neonLogin.$lockscreen_progress_indicator.html(pct);
  196. var o = {
  197. pct: currentProgress
  198. };
  199. TweenMax.to(o, .7, {
  200. pct: parseInt(pct, 10),
  201. roundProps: ["pct"],
  202. ease: Sine.easeOut,
  203. onUpdate: function()
  204. {
  205. neonLogin.$lockscreen_progress_indicator.html(o.pct + '%');
  206. drawProgress(parseInt(o.pct, 10)/100);
  207. },
  208. onComplete: callback
  209. });
  210. return;
  211. }
  212. // Normal Login
  213. neonLogin.$login_progressbar_indicator.html(pct);
  214. neonLogin.$login_progressbar.width(pct);
  215. var o = {
  216. pct: parseInt(neonLogin.$login_progressbar.width() / neonLogin.$login_progressbar.parent().width() * 100, 10)
  217. };
  218. TweenMax.to(o, .7, {
  219. pct: parseInt(pct, 10),
  220. roundProps: ["pct"],
  221. ease: Sine.easeOut,
  222. onUpdate: function()
  223. {
  224. neonLogin.$login_progressbar_indicator.html(o.pct + '%');
  225. },
  226. onComplete: callback
  227. });
  228. },
  229. resetProgressBar: function(display_errors)
  230. {
  231. TweenMax.set(neonLogin.$container, {css: {opacity: 0}});
  232. setTimeout(function()
  233. {
  234. TweenMax.to(neonLogin.$container, .6, {css: {opacity: 1}, onComplete: function()
  235. {
  236. neonLogin.$container.attr('style', '');
  237. }});
  238. neonLogin.$login_progressbar_indicator.html('0%');
  239. neonLogin.$login_progressbar.width(0);
  240. if(display_errors)
  241. {
  242. var $errors_container = $(".form-login-error");
  243. $errors_container.show();
  244. var height = $errors_container.outerHeight();
  245. $errors_container.css({
  246. height: 0
  247. });
  248. TweenMax.to($errors_container, .45, {css: {height: height}, onComplete: function()
  249. {
  250. $errors_container.css({height: 'auto'});
  251. }});
  252. // Reset password fields
  253. neonLogin.$container.find('input[type="password"]').val('');
  254. }
  255. }, 800);
  256. }
  257. });
  258. // Lockscreen Create Canvas
  259. if(is_lockscreen)
  260. {
  261. neonLogin.$lockscreen_progress_canvas = $('<canvas></canvas>');
  262. neonLogin.$lockscreen_progress_indicator = neonLogin.$container.find('.lockscreen-progress-indicator');
  263. neonLogin.$lockscreen_progress_canvas.appendTo(neonLogin.$ls_thumb);
  264. var thumb_size = neonLogin.$ls_thumb.width();
  265. neonLogin.$lockscreen_progress_canvas.attr({
  266. width: thumb_size,
  267. height: thumb_size
  268. });
  269. neonLogin.lockscreen_progress_canvas = neonLogin.$lockscreen_progress_canvas.get(0);
  270. // Create Progress Circle
  271. var bg = neonLogin.lockscreen_progress_canvas,
  272. ctx = ctx = bg.getContext('2d'),
  273. imd = null,
  274. circ = Math.PI * 2,
  275. quart = Math.PI / 2,
  276. currentProgress = 0;
  277. ctx.beginPath();
  278. ctx.strokeStyle = '#eb7067';
  279. ctx.lineCap = 'square';
  280. ctx.closePath();
  281. ctx.fill();
  282. ctx.lineWidth = 3.0;
  283. imd = ctx.getImageData(0, 0, thumb_size, thumb_size);
  284. var drawProgress = function(current) {
  285. ctx.putImageData(imd, 0, 0);
  286. ctx.beginPath();
  287. ctx.arc(thumb_size/2, thumb_size/2, 70, -(quart), ((circ) * current) - quart, false);
  288. ctx.stroke();
  289. currentProgress = current * 100;
  290. }
  291. drawProgress(0/100);
  292. neonLogin.$lockscreen_progress_indicator.html('0%');
  293. ctx.restore();
  294. }
  295. });
  296. })(jQuery, window);