ElementForm.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Controls/ElementForm.h"
  29. #include "../../Include/RmlUi/Core/Dictionary.h"
  30. #include "../../Include/RmlUi/Core/ElementUtilities.h"
  31. #include "../../Include/RmlUi/Controls/ElementFormControl.h"
  32. namespace Rml {
  33. namespace Controls {
  34. // Constructs a new ElementForm. This should not be called directly; use the Factory instead.
  35. ElementForm::ElementForm(const Rml::Core::String& tag) : Core::Element(tag)
  36. {
  37. }
  38. ElementForm::~ElementForm()
  39. {
  40. }
  41. // Submits the form.
  42. void ElementForm::Submit(const Rml::Core::String& name, const Rml::Core::String& submit_value)
  43. {
  44. Rml::Core::Dictionary values;
  45. if (name.empty())
  46. values["submit"] = submit_value;
  47. else
  48. values[name] = submit_value;
  49. Core::ElementList form_controls;
  50. Core::ElementUtilities::GetElementsByTagName(form_controls, this, "input");
  51. Core::ElementUtilities::GetElementsByTagName(form_controls, this, "textarea");
  52. Core::ElementUtilities::GetElementsByTagName(form_controls, this, "select");
  53. Core::ElementUtilities::GetElementsByTagName(form_controls, this, "dataselect");
  54. for (size_t i = 0; i < form_controls.size(); i++)
  55. {
  56. ElementFormControl* control = dynamic_cast< ElementFormControl* >(form_controls[i]);
  57. if (!control)
  58. continue;
  59. // Skip disabled controls.
  60. if (control->IsDisabled())
  61. continue;
  62. // Only process controls that should be submitted.
  63. if (!control->IsSubmitted())
  64. continue;
  65. Rml::Core::String control_name = control->GetName();
  66. Rml::Core::String control_value = control->GetValue();
  67. // Skip over unnamed form controls.
  68. if (control_name.empty())
  69. continue;
  70. // If the item already exists, append to it.
  71. Rml::Core::Variant* value = GetIf(values, control_name);
  72. if (value != nullptr)
  73. *value = value->Get< Rml::Core::String >() + ", " + control_value;
  74. else
  75. values[control_name] = control_value;
  76. }
  77. DispatchEvent(Core::EventId::Submit, values);
  78. }
  79. }
  80. }