dx.aspnet.mvc.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*!
  2. * DevExtreme (dx.aspnet.mvc.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. ! function(factory) {
  10. if ("function" === typeof define && define.amd) {
  11. define(function(require, exports, module) {
  12. module.exports = factory(require("jquery"), require("./ui/set_template_engine"), require("./ui/widget/ui.template_base").renderedCallbacks, require("./core/guid"), require("./ui/validation_engine"), require("./core/utils/iterator"), require("./core/utils/dom").extractTemplateMarkup, require("./core/utils/string").encodeHtml)
  13. })
  14. } else {
  15. var ui = DevExpress.ui;
  16. DevExpress.aspnet = factory(window.jQuery, ui && ui.setTemplateEngine, ui && ui.templateRendered, DevExpress.data.Guid, DevExpress.validationEngine, DevExpress.utils.iterator, DevExpress.utils.dom.extractTemplateMarkup, DevExpress.utils.string.encodeHtml)
  17. }
  18. }(function($, setTemplateEngine, templateRendered, Guid, validationEngine, iteratorUtils, extractTemplateMarkup, encodeHtml) {
  19. var templateCompiler = createTemplateCompiler();
  20. var pendingCreateComponentRoutines = [];
  21. var enableAlternativeTemplateTags = true;
  22. function createTemplateCompiler() {
  23. var OPEN_TAG = "<%",
  24. CLOSE_TAG = "%>",
  25. ENCODE_QUALIFIER = "-",
  26. INTERPOLATE_QUALIFIER = "=";
  27. var EXTENDED_OPEN_TAG = /[<[]%/g,
  28. EXTENDED_CLOSE_TAG = /%[>\]]/g;
  29. function acceptText(bag, text) {
  30. if (text) {
  31. bag.push("_.push(", JSON.stringify(text), ");")
  32. }
  33. }
  34. function acceptCode(bag, code) {
  35. var encode = code.charAt(0) === ENCODE_QUALIFIER,
  36. value = code.substr(1),
  37. interpolate = code.charAt(0) === INTERPOLATE_QUALIFIER;
  38. if (encode || interpolate) {
  39. bag.push("_.push(");
  40. bag.push(encode ? "arguments[1](" + value + ")" : value);
  41. bag.push(");")
  42. } else {
  43. bag.push(code + "\n")
  44. }
  45. }
  46. return function(text) {
  47. var bag = ["var _ = [];", "with(obj||{}) {"],
  48. chunks = text.split(enableAlternativeTemplateTags ? EXTENDED_OPEN_TAG : OPEN_TAG);
  49. acceptText(bag, chunks.shift());
  50. for (var i = 0; i < chunks.length; i++) {
  51. var tmp = chunks[i].split(enableAlternativeTemplateTags ? EXTENDED_CLOSE_TAG : CLOSE_TAG);
  52. if (2 !== tmp.length) {
  53. throw "Template syntax error"
  54. }
  55. acceptCode(bag, tmp[0]);
  56. acceptText(bag, tmp[1])
  57. }
  58. bag.push("}", "return _.join('')");
  59. return new Function("obj", bag.join(""))
  60. }
  61. }
  62. function createTemplateEngine() {
  63. return {
  64. compile: function(element) {
  65. return templateCompiler(extractTemplateMarkup(element))
  66. },
  67. render: function(template, data) {
  68. var html = template(data, encodeHtml);
  69. var dxMvcExtensionsObj = window.MVCx;
  70. if (dxMvcExtensionsObj && !dxMvcExtensionsObj.isDXScriptInitializedOnLoad) {
  71. html = html.replace(/(<script[^>]+)id="dxss_.+?"/g, "$1")
  72. }
  73. return html
  74. }
  75. }
  76. }
  77. function getValidationSummary(validationGroup) {
  78. var result;
  79. $(".dx-validationsummary").each(function(_, element) {
  80. var summary = $(element).data("dxValidationSummary");
  81. if (summary && summary.option("validationGroup") === validationGroup) {
  82. result = summary;
  83. return false
  84. }
  85. });
  86. return result
  87. }
  88. function createValidationSummaryItemsFromValidators(validators, editorNames) {
  89. var items = [];
  90. iteratorUtils.each(validators, function(_, validator) {
  91. var widget = validator.$element().data("dx-validation-target");
  92. if (widget && $.inArray(widget.option("name"), editorNames) > -1) {
  93. items.push({
  94. text: widget.option("validationError.message"),
  95. validator: validator
  96. })
  97. }
  98. });
  99. return items
  100. }
  101. function createComponent(name, options, id, validatorOptions) {
  102. var selector = "#" + String(id).replace(/[^\w-]/g, "\\$&");
  103. pendingCreateComponentRoutines.push(function() {
  104. var $element = $(selector);
  105. if ($element.length) {
  106. var $component = $(selector)[name](options);
  107. if ($.isPlainObject(validatorOptions)) {
  108. $component.dxValidator(validatorOptions)
  109. }
  110. return true
  111. }
  112. return false
  113. })
  114. }
  115. templateRendered.add(function() {
  116. var snapshot = pendingCreateComponentRoutines.slice();
  117. var leftover = [];
  118. pendingCreateComponentRoutines = [];
  119. snapshot.forEach(function(func) {
  120. if (!func()) {
  121. leftover.push(func)
  122. }
  123. });
  124. pendingCreateComponentRoutines = pendingCreateComponentRoutines.concat(leftover)
  125. });
  126. return {
  127. createComponent: createComponent,
  128. renderComponent: function(name, options, id, validatorOptions) {
  129. id = id || "dx-" + new Guid;
  130. createComponent(name, options, id, validatorOptions);
  131. return '<div id="' + id + '"></div>'
  132. },
  133. getEditorValue: function(inputName) {
  134. var $widget = $("input[name='" + inputName + "']").closest(".dx-widget");
  135. if ($widget.length) {
  136. var dxComponents = $widget.data("dxComponents"),
  137. widget = $widget.data(dxComponents[0]);
  138. if (widget) {
  139. return widget.option("value")
  140. }
  141. }
  142. },
  143. setTemplateEngine: function() {
  144. if (setTemplateEngine) {
  145. setTemplateEngine(createTemplateEngine())
  146. }
  147. },
  148. enableAlternativeTemplateTags: function(value) {
  149. enableAlternativeTemplateTags = value
  150. },
  151. createValidationSummaryItems: function(validationGroup, editorNames) {
  152. var groupConfig, items, summary = getValidationSummary(validationGroup);
  153. if (summary) {
  154. groupConfig = validationEngine.getGroupConfig(validationGroup);
  155. if (groupConfig) {
  156. items = createValidationSummaryItemsFromValidators(groupConfig.validators, editorNames);
  157. items.length && summary.option("items", items)
  158. }
  159. }
  160. }
  161. }
  162. });