| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- /*
- * This source file is part of RmlUi, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://github.com/mikke89/RmlUi
- *
- * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
- * Copyright (c) 2019 The RmlUi Team, and contributors
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- */
- #include "precompiled.h"
- #include "../../Include/RmlUi/Core/DataView.h"
- #include "../../Include/RmlUi/Core/DataModel.h"
- namespace Rml {
- namespace Core {
- DataView::~DataView() {}
- Element* DataView::GetElement() const
- {
- Element* result = attached_element.get();
- if (!result)
- Log::Message(Log::LT_WARNING, "Could not retrieve element in view, was it destroyed?");
- return result;
- }
- DataView::DataView(Element* element) : attached_element(element->GetObserverPtr()), element_depth(0) {
- if (element)
- {
- for (Element* parent = element->GetParentNode(); parent; parent = parent->GetParentNode())
- element_depth += 1;
- }
- }
- DataViewText::DataViewText(DataModel& model, ElementText* in_parent_element, const String& in_text, const size_t index_begin_search) : DataView(in_parent_element)
- {
- text.reserve(in_text.size());
- bool success = true;
- size_t previous_close_brackets = 0;
- size_t begin_brackets = index_begin_search;
- while ((begin_brackets = in_text.find("{{", begin_brackets)) != String::npos)
- {
- text.insert(text.end(), in_text.begin() + previous_close_brackets, in_text.begin() + begin_brackets);
- const size_t begin_name = begin_brackets + 2;
- const size_t end_name = in_text.find("}}", begin_name);
- if (end_name == String::npos)
- {
- success = false;
- break;
- }
- DataEntry entry;
- entry.index = text.size();
- String address_str = StringUtilities::StripWhitespace(StringView(in_text.data() + begin_name, in_text.data() + end_name));
- entry.variable_address = model.ResolveAddress(address_str, in_parent_element);
- data_entries.push_back(std::move(entry));
- previous_close_brackets = end_name + 2;
- begin_brackets = previous_close_brackets;
- }
- if (data_entries.empty())
- success = false;
- if (success && previous_close_brackets < in_text.size())
- text.insert(text.end(), in_text.begin() + previous_close_brackets, in_text.end());
- if (!success)
- {
- text.clear();
- data_entries.clear();
- InvalidateView();
- }
- }
- bool DataViewText::Update(DataModel& model)
- {
- bool entries_modified = false;
- for (DataEntry& entry : data_entries)
- {
- String value;
- bool result = model.GetValue(entry.variable_address, value);
- if (result && entry.value != value)
- {
- entry.value = value;
- entries_modified = true;
- }
- }
- if (entries_modified)
- {
- if (Element* element = GetElement())
- {
- RMLUI_ASSERTMSG(rmlui_dynamic_cast<ElementText*>(element), "Somehow the element type was changed from ElementText since construction of the view. Should not be possible?");
- if(auto text_element = static_cast<ElementText*>(element))
- {
- String new_text = BuildText();
- text_element->SetText(new_text);
- }
- }
- else
- {
- Log::Message(Log::LT_WARNING, "Could not update data view text, element no longer valid. Was it destroyed?");
- }
- }
- return entries_modified;
- }
- String DataViewText::BuildText() const
- {
- size_t reserve_size = text.size();
- for (const DataEntry& entry : data_entries)
- reserve_size += entry.value.size();
- String result;
- result.reserve(reserve_size);
- size_t previous_index = 0;
- for (const DataEntry& entry : data_entries)
- {
- result += text.substr(previous_index, entry.index - previous_index);
- result += entry.value;
- previous_index = entry.index;
- }
- if (previous_index < text.size())
- result += text.substr(previous_index);
- return result;
- }
- DataViewAttribute::DataViewAttribute(DataModel& model, Element* element, const String& binding_name, const String& attribute_name)
- : DataView(element), attribute_name(attribute_name)
- {
- variable_address = model.ResolveAddress(binding_name, element);
- if (attribute_name.empty())
- InvalidateView();
- }
- bool DataViewAttribute::Update(DataModel& model)
- {
- bool result = false;
- String value;
- Element* element = GetElement();
- if (model.GetValue(variable_address, value) && element)
- {
- Variant* attribute = element->GetAttribute(attribute_name);
- if (!attribute || (attribute && attribute->Get<String>() != value))
- {
- element->SetAttribute(attribute_name, value);
- result = true;
- }
- }
- return result;
- }
- DataViewStyle::DataViewStyle(DataModel& model, Element* element, const String& binding_name, const String& property_name)
- : DataView(element), property_name(property_name)
- {
- variable_address = model.ResolveAddress(binding_name, element);
-
- if (variable_address.empty() || property_name.empty())
- InvalidateView();
- }
- bool DataViewStyle::Update(DataModel& model)
- {
- bool result = false;
- String value;
- Element* element = GetElement();
- if (model.GetValue(variable_address, value) && element)
- {
- const Property* p = element->GetLocalProperty(property_name);
- if (!p || p->Get<String>() != value)
- {
- element->SetProperty(property_name, value);
- result = true;
- }
- }
- return result;
- }
- DataViewIf::DataViewIf(DataModel& model, Element* element, const String& binding_name) : DataView(element)
- {
- variable_address = model.ResolveAddress(binding_name, element);
- if (variable_address.empty())
- InvalidateView();
- }
- bool DataViewIf::Update(DataModel& model)
- {
- bool result = false;
- bool value = false;
- Element* element = GetElement();
- if (model.GetValue(variable_address, value) && element)
- {
- bool is_visible = (element->GetLocalStyleProperties().count(PropertyId::Display) == 0);
- if(is_visible != value)
- {
- if (value)
- element->RemoveProperty(PropertyId::Display);
- else
- element->SetProperty(PropertyId::Display, Property(Style::Display::None));
- result = true;
- }
- }
- return result;
- }
- DataViewFor::DataViewFor(DataModel& model, Element* element, const String& in_binding_name, const String& in_rml_content)
- : DataView(element), rml_contents(in_rml_content)
- {
- StringList binding_list;
- StringUtilities::ExpandString(binding_list, in_binding_name, ':');
- if (binding_list.empty() || binding_list.size() > 2 || binding_list.front().empty() || binding_list.back().empty())
- {
- Log::Message(Log::LT_WARNING, "Invalid syntax in data-for '%s'", in_binding_name.c_str());
- InvalidateView();
- return;
- }
- if (binding_list.size() == 2)
- alias_name = binding_list.front();
- else
- alias_name = "it";
- const String& binding_name = binding_list.back();
- variable_address = model.ResolveAddress(binding_name, element);
- if (variable_address.empty())
- {
- InvalidateView();
- return;
- }
- attributes = element->GetAttributes();
- attributes.erase("data-for");
- element->SetProperty(PropertyId::Display, Property(Style::Display::None));
- }
- bool DataViewFor::Update(DataModel& model)
- {
- Variable variable = model.GetVariable(variable_address);
- if (!variable)
- return false;
- bool result = false;
- const int size = variable.Size();
- const int num_elements = (int)elements.size();
- Element* element = GetElement();
- for (int i = 0; i < Math::Max(size, num_elements); i++)
- {
- if (i >= num_elements)
- {
- ElementPtr new_element_ptr = Factory::InstanceElement(nullptr, element->GetTagName(), element->GetTagName(), attributes);
- Address replacement_address;
- replacement_address.reserve(variable_address.size() + 1);
- replacement_address = variable_address;
- replacement_address.push_back(AddressEntry(i));
- model.InsertAlias(new_element_ptr.get(), alias_name, replacement_address);
- Element* new_element = element->GetParentNode()->InsertBefore(std::move(new_element_ptr), element);
- elements.push_back(new_element);
- elements[i]->SetInnerRML(rml_contents);
- RMLUI_ASSERT(i < (int)elements.size());
- }
- if (i >= size)
- {
- model.EraseAliases(elements[i]);
- elements[i]->GetParentNode()->RemoveChild(elements[i]).reset();
- elements[i] = nullptr;
- }
- }
- if (num_elements > size)
- elements.resize(size);
- return result;
- }
- DataViews::DataViews()
- {}
- DataViews::~DataViews()
- {}
- void DataViews::Add(UniquePtr<DataView> view) {
- views_to_add.push_back(std::move(view));
- }
- void DataViews::OnElementRemove(Element* element)
- {
- for (auto it = views.begin(); it != views.end();)
- {
- auto& view = *it;
- if (view && view->GetElement() == element)
- {
- views_to_remove.push_back(std::move(view));
- it = views.erase(it);
- }
- else
- ++it;
- }
- }
- bool DataViews::Update(DataModel & model, const SmallUnorderedSet< String >& dirty_variables)
- {
- bool result = false;
- // View updates may result in newly added views, thus we do it recursively but with an upper limit.
- // Without the loop, newly added views won't be updated until the next Update() call.
- for(int i = 0; i == 0 || (!views_to_add.empty() && i < 10); i++)
- {
- std::vector<DataView*> dirty_views;
- if (!views_to_add.empty())
- {
- views.reserve(views.size() + views_to_add.size());
- for (auto&& view : views_to_add)
- {
- dirty_views.push_back(view.get());
- for (const String& variable_name : view->GetVariableNameList())
- name_view_map.emplace(variable_name, view.get());
- views.push_back(std::move(view));
- }
- views_to_add.clear();
- }
- for (const String& variable_name : dirty_variables)
- {
- auto pair = name_view_map.equal_range(variable_name);
- for (auto it = pair.first; it != pair.second; ++it)
- dirty_views.push_back(it->second);
- }
- // Remove duplicate entries
- std::sort(dirty_views.begin(), dirty_views.end());
- auto it_remove = std::unique(dirty_views.begin(), dirty_views.end());
- dirty_views.erase(it_remove, dirty_views.end());
- // Sort by the element's depth in the document tree so that any structural changes due to a changed variable are reflected in the element's children.
- // Eg. the 'data-for' view will remove children if any of its data variable array size is reduced.
- std::sort(dirty_views.begin(), dirty_views.end(), [](auto&& left, auto&& right) { return left->GetElementDepth() < right->GetElementDepth(); });
- for (DataView* view : dirty_views)
- {
- RMLUI_ASSERT(view);
- if (!view)
- continue;
- if (view->IsValid())
- result |= view->Update(model);
- }
- // Destroy views marked for destruction
- // @performance: Horrible...
- if (!views_to_remove.empty())
- {
- for (const auto& view : views_to_remove)
- {
- for (auto it = name_view_map.begin(); it != name_view_map.end(); )
- {
- if (it->second == view.get())
- it = name_view_map.erase(it);
- else
- ++it;
- }
- }
- views_to_remove.clear();
- }
- }
- return result;
- }
- }
- }
|