BxDolNestedForm.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) UNA, Inc - https://una.io
  3. * MIT License - https://opensource.org/licenses/MIT
  4. *
  5. * @defgroup UnaCore UNA Core
  6. * @{
  7. */
  8. function BxDolNestedForm(options) {
  9. this.init(options);
  10. this._eForm = null;
  11. };
  12. BxDolNestedForm.prototype.init = function (options) {
  13. this._sUniqId = options.uniq_id;
  14. this._sJsInstanceName = options.js_instance_name;
  15. this._sResultContainerId = 'bx-form-input-nested-form-forms-' + this._sUniqId;
  16. this._sNestedType = options.nested_type ? options.nested_type : '';
  17. this._sTemplateGhost = (options.template_ghost && typeof options.template_ghost == "string") ? options.template_ghost.replace(/{nested_type}/gi, options.nested_type) : '';
  18. this._sTemplateErrorGhosts = options.template_error ? options.template_error : '<div class="bx-informer-msg-error bx-def-padding-sec bx-def-round-corners bx-def-margin-sec-top">{error}</div>';
  19. this._sFormName = options.form_name ? options.form_name : '';
  20. this._sActionsUri = options.action_uri ? options.action_uri : '';
  21. this._iContentId = undefined == options.content_id || '' == options.content_id ? '' : parseInt(options.content_id);
  22. };
  23. BxDolNestedForm.prototype._getUrlWithStandardParams = function () {
  24. return sUrlRoot + this._sActionsUri + 'Nested?uid=' + this._sUniqId + '&s=' + this._sFormName + '&t=' + this._sNestedType
  25. };
  26. BxDolNestedForm.prototype._getContainerId = function (iItemId) {
  27. return 'bx-nested-form-' + this._sNestedType + '-' + iItemId;
  28. };
  29. BxDolNestedForm.prototype.addEmptyGhost = function () {
  30. var oVars = {
  31. js_instance_name: this._sJsInstanceName,
  32. file_id: 0,
  33. item_id: 0,
  34. }
  35. var sHTML = this._sTemplateGhost;
  36. for (var i in oVars)
  37. sHTML = sHTML.replace (new RegExp('{'+i+'}', 'g'), oVars[i]);
  38. $.each(this._aFields, function( index, value ) {
  39. sHTML = sHTML.replace (new RegExp('{'+value+'}', 'g'), '');
  40. });
  41. $('#' + this._sResultContainerId).append(sHTML).addWebForms();
  42. };
  43. BxDolNestedForm.prototype.deleteGhost = function (e, sSelector) {
  44. iItemId = 0;
  45. $(e).closest('.bx-nested-form-ghost-form').each(function() {
  46. iItemId = $( this ).find("input[name='"+sSelector+"[]']").val()
  47. });
  48. if (0 == parseInt(iItemId) || typeof(iItemId) == "undefined") {
  49. $(e).closest('.bx-nested-form-ghost').slideUp(function () {
  50. $(this).remove();
  51. });
  52. }
  53. else {
  54. var sUrl = this._getUrlWithStandardParams() + '&a=delete&id=' + iItemId;
  55. var $this = this;
  56. var sContainerId = $this._getContainerId(iItemId);
  57. bx_loading(sContainerId, true);
  58. $.post(sUrl, function (sMsg) {
  59. bx_loading(sContainerId, false);
  60. if ('ok' == sMsg) {
  61. $('#' + sContainerId).slideUp('slow', function () {
  62. $(this).remove();
  63. });
  64. } else {
  65. $('#' + $this._sResultContainerId).prepend($this._sTemplateErrorGhosts.replace('{error}', sMsg));
  66. }
  67. });
  68. }
  69. };
  70. BxDolNestedForm.prototype._replaceVars = function (sHTML, oVars) {
  71. oVars = $.extend({}, oVars, {js_instance_name: this._sJsInstanceName});
  72. // replace vars
  73. for (var i in oVars) {
  74. sHTML = sHTML.replace (new RegExp('{'+i+'}', 'g'), oVars[i]);
  75. }
  76. // set selected values
  77. var oHTML = $(sHTML);
  78. for (var i in oVars)
  79. oHTML.find('select[name="' + i + '[]"] option[value="' + oVars[i] + '"]').attr('selected', 'selected');
  80. sHTML = $('<div></div>').append(oHTML).html();
  81. return sHTML;
  82. };
  83. /** @} */