Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // file: main.js
  2.  
  3. var boxes = document.querySelectorAll('.textbox');
  4. var resetButton = document.querySelector('#btn-reset');
  5.  
  6. for (var i=0; i < boxes.length; i++) {
  7.   var box = new LimitedTextbox(boxes[i], boxes[i].dataset.maxlength);
  8.   resetButton.addEventListener('click', box.reset.bind(box));
  9. }
  10.  
  11. // file: panel.js
  12. function Panel(el, parent) {
  13.   this.el = {
  14.     root: el,
  15.     title: el.querySelector('.title'),
  16.     val: el.querySelector('.val')
  17.   };
  18.   this.parent = parent;  
  19. }
  20.  
  21. Panel.prototype.start = function() {
  22.   this.el.title.textContent = this.titleText;
  23.   this.display(this.parent.currentTextLength());
  24. };
  25.  
  26. Panel.prototype.display = function(textLength) {
  27.   this.el.val.textContent = this.getDisplayValue(textLength);
  28. };
  29. // file: panels/current-length.js
  30. function CurrentLengthPanel(el, parent) {
  31.   Panel.call(this, el, parent);
  32. }
  33.  
  34. CurrentLengthPanel.prototype = Object.create(Panel.prototype);
  35.  
  36. CurrentLengthPanel.prototype.titleText = 'Current Length: ';
  37.  
  38. CurrentLengthPanel.prototype.getDisplayValue = function(textLength) {
  39.   return textLength;
  40. };
  41. // file: limited-textbox.js
  42. function LimitedTextbox(el, maxLength) {
  43.   Utils.injectHtmlTemplate(el, 'tmpl-textbox');
  44.   this.maxLength = maxLength;
  45.   this.el = {
  46.     root: el,
  47.     textarea: el.querySelector('textarea'),
  48.     panel: el.querySelector('.panel')
  49.   };
  50.   this.maxLength = maxLength;
  51.  
  52.   this.panels = [
  53.     new RemainingPanel(this.el.panel, this),
  54.     new CurrentLengthPanel(this.el.panel, this)
  55.   ];
  56.   this.activePanelIndex = 0;
  57.   this.panels[this.activePanelIndex].start();
  58.  
  59.   // set HTML maxLength attribute so text area will actually be limited
  60.   this.el.textarea.maxLength = this.maxLength;
  61.  
  62.   // add the event handler
  63.   this.el.textarea.addEventListener('input', this.updateRemainingText.bind(this));  
  64.   this.el.panel.addEventListener('click', this.togglePanel.bind(this));
  65. }
  66.  
  67. LimitedTextbox.prototype.togglePanel = function() {
  68.   this.activePanelIndex = (this.activePanelIndex + 1) % this.panels.length;
  69.   this.panels[this.activePanelIndex].start();
  70. };
  71.  
  72. LimitedTextbox.prototype.currentTextLength = function() {
  73.   return this.el.textarea.value.length;
  74. };
  75.  
  76. LimitedTextbox.prototype.updateRemainingText = function() {
  77.   var currentLength = this.el.textarea.value.length;
  78.   this.display(currentLength);
  79. };
  80.  
  81. LimitedTextbox.prototype.display = function(textLength) {
  82.   this.panels[this.activePanelIndex].display(textLength);
  83. };
  84.  
  85. LimitedTextbox.prototype.reset = function() {
  86.   this.el.textarea.value = '';
  87.   this.display(0);
  88. };
  89. // file: remaining-panel.js
  90. function RemainingPanel(el, parent) {
  91.   Panel.call(this, el, parent);
  92.   this.maxLength = parent.maxLength;
  93. }
  94.  
  95. RemainingPanel.prototype = Object.create(Panel.prototype);
  96.  
  97. RemainingPanel.prototype.titleText = 'Characters Remaining: ';
  98.  
  99. RemainingPanel.prototype.getDisplayValue = function(textLength) {
  100.   return this.maxLength - textLength;
  101. };
  102.  
  103. // file: utils.js
  104.  
  105. var Utils = {};
  106.  
  107. Utils.injectHtmlTemplate = function(el, templateId) {
  108.   var html = document.getElementById(templateId).innerHTML;
  109.   el.innerHTML = html;  
  110. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement