DataView.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "precompiled.h"
  29. #include "../../Include/RmlUi/Core/DataView.h"
  30. #include "../../Include/RmlUi/Core/DataModel.h"
  31. namespace Rml {
  32. namespace Core {
  33. DataViewText::DataViewText(const DataModel& model, ElementText* in_parent_element, const String& in_text, const size_t index_begin_search) : element(in_parent_element->GetObserverPtr())
  34. {
  35. text.reserve(in_text.size());
  36. bool success = true;
  37. size_t previous_close_brackets = 0;
  38. size_t begin_brackets = index_begin_search;
  39. while ((begin_brackets = in_text.find("{{", begin_brackets)) != String::npos)
  40. {
  41. text.insert(text.end(), in_text.begin() + previous_close_brackets, in_text.begin() + begin_brackets);
  42. const size_t begin_name = begin_brackets + 2;
  43. const size_t end_name = in_text.find("}}", begin_name);
  44. if (end_name == String::npos)
  45. {
  46. success = false;
  47. break;
  48. }
  49. DataEntry entry;
  50. entry.index = text.size();
  51. entry.binding_name = (String)StringUtilities::StripWhitespace(StringView(in_text.data() + begin_name, in_text.data() + end_name));
  52. entry.binding_name = model.ResolveVariableName(entry.binding_name, in_parent_element);
  53. data_entries.push_back(std::move(entry));
  54. previous_close_brackets = end_name + 2;
  55. begin_brackets = previous_close_brackets;
  56. }
  57. if (data_entries.empty())
  58. success = false;
  59. if (success && previous_close_brackets < in_text.size())
  60. text.insert(text.end(), in_text.begin() + previous_close_brackets, in_text.end());
  61. if (success)
  62. {
  63. Update(model);
  64. }
  65. else
  66. {
  67. text.clear();
  68. data_entries.clear();
  69. }
  70. }
  71. bool DataViewText::Update(const DataModel& model)
  72. {
  73. bool entries_modified = false;
  74. for (DataEntry& entry : data_entries)
  75. {
  76. String value;
  77. bool result = model.GetValue(entry.binding_name, value);
  78. if (result && entry.value != value)
  79. {
  80. entry.value = value;
  81. entries_modified = true;
  82. }
  83. }
  84. if (entries_modified)
  85. {
  86. if (element)
  87. {
  88. RMLUI_ASSERTMSG(rmlui_dynamic_cast<ElementText*>(element.get()), "Somehow the element type was changed from ElementText since construction of the view. Should not be possible?");
  89. if(auto text_element = static_cast<ElementText*>(element.get()))
  90. {
  91. String new_text = BuildText();
  92. text_element->SetText(new_text);
  93. }
  94. }
  95. else
  96. {
  97. Log::Message(Log::LT_WARNING, "Could not update data view text, element no longer valid. Was it destroyed?");
  98. }
  99. }
  100. return entries_modified;
  101. }
  102. String DataViewText::BuildText() const
  103. {
  104. size_t reserve_size = text.size();
  105. for (const DataEntry& entry : data_entries)
  106. reserve_size += entry.value.size();
  107. String result;
  108. result.reserve(reserve_size);
  109. size_t previous_index = 0;
  110. for (const DataEntry& entry : data_entries)
  111. {
  112. result += text.substr(previous_index, entry.index - previous_index);
  113. result += entry.value;
  114. previous_index = entry.index;
  115. }
  116. if (previous_index < text.size())
  117. result += text.substr(previous_index);
  118. return result;
  119. }
  120. DataViewAttribute::DataViewAttribute(const DataModel& model, Element* element, const String& binding_name, const String& attribute_name)
  121. : element(element->GetObserverPtr()), binding_name(binding_name), attribute_name(attribute_name)
  122. {
  123. Update(model);
  124. }
  125. bool DataViewAttribute::Update(const DataModel& model)
  126. {
  127. bool result = false;
  128. String value;
  129. if (model.GetValue(binding_name, value))
  130. {
  131. Variant* attribute = element->GetAttribute(attribute_name);
  132. if (!attribute || (attribute && attribute->Get<String>() != value))
  133. {
  134. element->SetAttribute(attribute_name, value);
  135. result = true;
  136. }
  137. }
  138. return result;
  139. }
  140. DataViewStyle::DataViewStyle(const DataModel& model, Element* element, const String& binding_name, const String& property_name)
  141. : element(element->GetObserverPtr()), binding_name(binding_name), property_name(property_name)
  142. {
  143. Update(model);
  144. }
  145. bool DataViewStyle::Update(const DataModel& model)
  146. {
  147. bool result = false;
  148. String value;
  149. if (model.GetValue(binding_name, value))
  150. {
  151. const Property* p = element->GetLocalProperty(property_name);
  152. if (!p || p->Get<String>() != value)
  153. {
  154. element->SetProperty(property_name, value);
  155. result = true;
  156. }
  157. }
  158. return result;
  159. }
  160. DataViewIf::DataViewIf(const DataModel& model, Element* element, const String& binding_name) : element(element->GetObserverPtr()), binding_name(binding_name)
  161. {
  162. Update(model);
  163. }
  164. bool DataViewIf::Update(const DataModel& model)
  165. {
  166. bool result = false;
  167. bool value = false;
  168. if (model.GetValue(binding_name, value))
  169. {
  170. bool is_visible = (element->GetLocalStyleProperties().count(PropertyId::Display) == 0);
  171. if(is_visible != value)
  172. {
  173. if (value)
  174. element->RemoveProperty(PropertyId::Display);
  175. else
  176. element->SetProperty(PropertyId::Display, Property(Style::Display::None));
  177. result = true;
  178. }
  179. }
  180. return result;
  181. }
  182. DataViewFor::DataViewFor(const DataModel& model, Element* element, const String& binding_name, const String& in_rml_content)
  183. : element(element->GetObserverPtr()), binding_name(binding_name), rml_contents(in_rml_content)
  184. {
  185. attributes = element->GetAttributes();
  186. attributes.erase("data-for");
  187. element->SetProperty(PropertyId::Display, Property(Style::Display::None));
  188. Update(model);
  189. }
  190. bool DataViewFor::Update(const DataModel& model)
  191. {
  192. bool result = false;
  193. bool value = false;
  194. const int size = model.containers.find(binding_name)->second->Size();
  195. const int num_elements = (int)elements.size();
  196. for (int i = 0; i < Math::Max(size, num_elements); i++)
  197. {
  198. if (i >= num_elements)
  199. {
  200. ElementPtr new_element_ptr = Factory::InstanceElement(nullptr, element->GetTagName(), element->GetTagName(), attributes);
  201. new_element_ptr->SetDataModel((DataModel*)(&model));
  202. Element* new_element = element->GetParentNode()->InsertBefore(std::move(new_element_ptr), element.get());
  203. elements.push_back(new_element);
  204. auto& alias_map = model.aliases.emplace(new_element, SmallUnorderedMap<String, String>()).first->second;
  205. alias_map["it"] = binding_name + "[" + ToString(i) + "]";
  206. elements[i]->SetInnerRML(rml_contents);
  207. RMLUI_ASSERT(i < (int)elements.size());
  208. }
  209. if (i >= size)
  210. {
  211. elements[i]->GetParentNode()->RemoveChild(elements[i]).reset();
  212. model.aliases.erase(elements[i]);
  213. elements[i] = nullptr;
  214. }
  215. }
  216. if (num_elements > size)
  217. elements.resize(size);
  218. return result;
  219. }
  220. }
  221. }