1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/webui_generator/view_model.h"
7 #include "components/webui_generator/view.h"
9 namespace webui_generator
{
11 ViewModel::ContextEditor::ContextEditor(ViewModel
& view_model
)
12 : view_model_(view_model
), context_(view_model_
.context()) {
15 ViewModel::ContextEditor::~ContextEditor() {
16 view_model_
.CommitContextChanges();
19 const ViewModel::ContextEditor
& ViewModel::ContextEditor::SetBoolean(
22 context_
.SetBoolean(key
, value
);
26 const ViewModel::ContextEditor
& ViewModel::ContextEditor::SetInteger(
29 context_
.SetInteger(key
, value
);
33 const ViewModel::ContextEditor
& ViewModel::ContextEditor::SetDouble(
36 context_
.SetDouble(key
, value
);
40 const ViewModel::ContextEditor
& ViewModel::ContextEditor::SetString(
42 const std::string
& value
) const {
43 context_
.SetString(key
, value
);
47 const ViewModel::ContextEditor
& ViewModel::ContextEditor::SetString(
49 const base::string16
& value
) const {
50 context_
.SetString(key
, value
);
54 const ViewModel::ContextEditor
& ViewModel::ContextEditor::SetStringList(
56 const StringList
& value
) const {
57 context_
.SetStringList(key
, value
);
61 const ViewModel::ContextEditor
& ViewModel::ContextEditor::SetString16List(
63 const String16List
& value
) const {
64 context_
.SetString16List(key
, value
);
68 ViewModel::ViewModel() : view_(nullptr) {
71 ViewModel::~ViewModel() {
74 void ViewModel::SetView(View
* view
) {
79 void ViewModel::OnChildrenReady() {
80 OnAfterChildrenReady();
81 view_
->OnViewModelReady();
84 void ViewModel::UpdateContext(const base::DictionaryValue
& diff
) {
85 std::vector
<std::string
> changed_keys
;
86 context_
.ApplyChanges(diff
, &changed_keys
);
87 OnContextChanged(changed_keys
);
90 void ViewModel::OnEvent(const std::string
& event
) {
93 ViewModel::ContextEditor
ViewModel::GetContextEditor() {
94 return ContextEditor(*this);
97 void ViewModel::CommitContextChanges() {
98 if (!context().HasChanges())
101 base::DictionaryValue diff
;
102 context().GetChangesAndReset(&diff
);
103 view_
->OnContextChanged(diff
);
106 } // namespace webui_generator