Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. module createAppLoginWidget {
  2.  
  3. declare var kendo;
  4. declare var ui;
  5. declare var $;
  6.  
  7. var ui = kendo.ui, Widget = ui.Widget
  8.  
  9. var appLoginWidget = {
  10.  
  11. options: {
  12. name: "AppLogin",
  13. currentUserName: ""
  14. },
  15.  
  16. showView: function ($showView) {
  17. // simple view handler
  18. this.ViewArray.forEach(function ($view) { if ($view != $showView) $view.hide() });
  19. $showView.show();
  20. },
  21.  
  22. refreshCustomBindings: function () {
  23. this.$CurrentUserName.text(this.options.currentUserName);
  24. },
  25.  
  26. doLogout: function (evt) {
  27. var widget = $(evt.target).parents("[data-role]").first().data("handler");
  28. // some async logout
  29. widget.showView(widget.$StatusView);
  30. setTimeout(function () {
  31. widget.options.currentUserName = "";
  32. widget.showView(widget.$LoginView);
  33. }, 1000);
  34. },
  35.  
  36. doTryLogin: function (evt) {
  37.  
  38. var widget = $(evt.target).parents("[data-role]").first().data("handler")
  39.  
  40. // some async login
  41. widget.showView(widget.$StatusView);
  42. setTimeout(function () {
  43.  
  44. widget.options.currentUserName = widget.$UserName.val();
  45.  
  46. widget.$UserName.val("");
  47. widget.$Password.val("");
  48.  
  49. widget.refreshCustomBindings();
  50. widget.showView(widget.$LoggedInView);
  51.  
  52. }, 1000);
  53.  
  54. },
  55.  
  56. $UserName: null,
  57. $Password: null,
  58. $CurrentUserName: null,
  59. $LoginView: null,
  60. $LoggedInView: null,
  61. $StatusView: null,
  62. ViewArray:null,
  63.  
  64.  
  65. init: function (element, options) {
  66.  
  67. if (typeof (options) == "undefined") options = this.options;
  68. // base call to widget initialization
  69. Widget.fn.init.call(this, element, options);
  70.  
  71. // Create HTML elements
  72. // Some are public ("this.$name")
  73.  
  74. // Elements are separated into "views" which can be hidden/shown
  75.  
  76. //
  77. // Login view:
  78. //
  79.  
  80. this.$UserName = $("<input>").attr({ type: 'text', placeholder: 'Username' });
  81. this.$Password = $("<input>").attr({ type: 'password', placeholder: 'Password' });
  82. var buttonLogin = $("<input>").attr({ type: 'button', value: 'Login' });
  83.  
  84. // Attach event
  85. buttonLogin.on("click", this.doTryLogin);
  86.  
  87. this.$LoginView = $("<div>").append([this.$UserName, this.$Password, buttonLogin]);
  88.  
  89. //
  90. // Logged in view:
  91. //
  92.  
  93. this.$CurrentUserName = $("<span>");
  94. var loggedInAs = $("<span>").text("Logged in as ").append(this.$CurrentUserName);
  95.  
  96. var buttonLogout = $("<input>").attr({ type: 'button', value: 'Logout' });
  97. buttonLogout.on("click", this.doLogout);
  98.  
  99. this.$LoggedInView = $("<div>").append([loggedInAs, buttonLogout]);
  100.  
  101. //
  102. // Status view:
  103. //
  104.  
  105. this.$StatusView = $("<div>").text("Wait...");
  106.  
  107. // Add views to array and hide all but one
  108. this.ViewArray = [this.$LoginView, this.$LoggedInView, this.$StatusView];
  109. this.showView(this.$LoginView);
  110.  
  111. // Append to DOM
  112. $(element).append(this.ViewArray);
  113.  
  114. }
  115.  
  116. }
  117.  
  118. ui.plugin(Widget.extend(appLoginWidget));
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement