Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. (function($, window, document, undefined) {
  2. "use strict";
  3.  
  4. var pluginName = "jscTest",
  5. defaults = {};
  6.  
  7. var e = function(ns) {
  8. return ns + "." + pluginName;
  9. };
  10.  
  11. function Plugin(element, options) {
  12. this.element = element;
  13. this.$element = $(element);
  14. this.settings = $.extend({}, defaults, options);
  15. this._defaults = defaults;
  16. this._name = pluginName;
  17. this._ajaxHandle = {};
  18. this.init();
  19. }
  20.  
  21. $.extend(Plugin.prototype, {
  22. init: function() {
  23. var self = this;
  24. var $el = self.getElement();
  25. self.initCoreEvents();
  26. },
  27. getElement: function() {
  28. var self = this;
  29. return self.$element;
  30. },
  31.  
  32. initCoreEvents: function() {
  33. var self = this;
  34. var $el = self.getElement();
  35. $el.on(e("initialize"), function() {
  36. var $this = $(this);
  37. var $el = self.getElement();
  38. self.initialize();
  39. $this.trigger(e("initialized"));
  40. });
  41. // $el.on(e("initialized"), function() {
  42. // var $this = $(this);
  43. // var $el = self.getElement();
  44. // });
  45. },
  46. initialize: function() {
  47. var self = this;
  48. var $el = self.getElement();
  49. $el.find('.testselect').each(function () {
  50. var resDiv = $el.find('.dynamicFields');
  51. $(this).on("change", function(evt) {
  52. resDiv.html('');
  53. var $this = $(this);
  54. var val = $this.val();
  55. var name = 'df[' + $this.attr('data-name') + '][]';
  56. var id = 'df-' + $this.attr('data-name') + i;
  57. for (var i = 0; i < val; i++) {
  58. $('<label/>').attr({ for: id}).html('Value ' + (i + 1) + '&nbsp;&nbsp;').appendTo(resDiv);
  59. $('<input/>').attr({ type: 'text', id: id, name: name}).appendTo(resDiv);
  60. $('<br/>').appendTo(resDiv);
  61. }
  62. });
  63. $(this).trigger('change');
  64. });
  65. },
  66. bla: function() {},
  67. getAjaxHandle: function(name) {
  68. if (!name) {
  69. name = "default";
  70. }
  71. var self = this;
  72. if (!name in self._ajaxHandle) {
  73. self._ajaxHandle[name] = null;
  74. }
  75. return self._ajaxHandle[name];
  76. },
  77. setAjaxHandle: function(ajaxHandle, name) {
  78. if (!name) {
  79. name = "default";
  80. }
  81. var self = this;
  82. self._ajaxHandle[name] = ajaxHandle;
  83. return self;
  84. },
  85. stopAjaxCalls: function(name) {
  86. if (!name) {
  87. name = "default";
  88. }
  89. var self = this;
  90. try {
  91. self.getAjaxHandle(name).abort();
  92. } catch (e) {}
  93. return self;
  94. },
  95. destroy: function() {
  96. var self = this;
  97. self.getElement().off("." + pluginName);
  98. $(document).off("." + pluginName);
  99. self.getElement()
  100. .find("." + self.settings.entryClass)
  101. .off("." + pluginName);
  102. $("html").off("." + pluginName);
  103. $(window).off("." + pluginName);
  104. self.getElement().removeData("plugin_" + pluginName);
  105. }
  106. });
  107.  
  108. $.fn[pluginName] = function(options) {
  109. return this.each(function() {
  110. if (!$.data(this, "plugin_" + pluginName)) {
  111. $.data(this, "plugin_" + pluginName, new Plugin(this, options));
  112. }
  113. $(this).trigger(e("initialize"));
  114. });
  115. };
  116. })(jQuery, window, document);
  117.  
  118. $(function() {
  119. var $elem = $("{{selector}}");
  120. $elem.jscTest();
  121. });
  122.  
  123. // <div class="jscTest" style="">
  124. // <select class="form-control testselect" name="testselect" data-name="blub">
  125. // <option value="1" selected="selected">1</option>
  126. // <option value="2">2</option>
  127. // <option value="3">3</option>
  128. // </select>
  129. // <div class="dynamicFields"></div>
  130. // </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement