summaryrefslogtreecommitdiffstats
path: root/components/webui_generator/view_model.cc
blob: d81ce98752807e6361fcf95b7f7032f49b6ddd28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/webui_generator/view_model.h"

#include "components/webui_generator/view.h"

namespace webui_generator {

ViewModel::ContextEditor::ContextEditor(ViewModel& view_model)
    : view_model_(view_model), context_(view_model_.context()) {
}

ViewModel::ContextEditor::~ContextEditor() {
  view_model_.CommitContextChanges();
}

const ViewModel::ContextEditor& ViewModel::ContextEditor::SetBoolean(
    const KeyType& key,
    bool value) const {
  context_.SetBoolean(key, value);
  return *this;
}

const ViewModel::ContextEditor& ViewModel::ContextEditor::SetInteger(
    const KeyType& key,
    int value) const {
  context_.SetInteger(key, value);
  return *this;
}

const ViewModel::ContextEditor& ViewModel::ContextEditor::SetDouble(
    const KeyType& key,
    double value) const {
  context_.SetDouble(key, value);
  return *this;
}

const ViewModel::ContextEditor& ViewModel::ContextEditor::SetString(
    const KeyType& key,
    const std::string& value) const {
  context_.SetString(key, value);
  return *this;
}

const ViewModel::ContextEditor& ViewModel::ContextEditor::SetString(
    const KeyType& key,
    const base::string16& value) const {
  context_.SetString(key, value);
  return *this;
}

const ViewModel::ContextEditor& ViewModel::ContextEditor::SetStringList(
    const KeyType& key,
    const StringList& value) const {
  context_.SetStringList(key, value);
  return *this;
}

const ViewModel::ContextEditor& ViewModel::ContextEditor::SetString16List(
    const KeyType& key,
    const String16List& value) const {
  context_.SetString16List(key, value);
  return *this;
}

ViewModel::ViewModel() : view_(nullptr) {
}

ViewModel::~ViewModel() {
}

void ViewModel::SetView(View* view) {
  view_ = view;
  Initialize();
}

void ViewModel::OnChildrenReady() {
  OnAfterChildrenReady();
  view_->OnViewModelReady();
}

void ViewModel::UpdateContext(const base::DictionaryValue& diff) {
  std::vector<std::string> changed_keys;
  context_.ApplyChanges(diff, &changed_keys);
  OnContextChanged(changed_keys);
}

void ViewModel::OnEvent(const std::string& event) {
}

ViewModel::ContextEditor ViewModel::GetContextEditor() {
  return ContextEditor(*this);
}

void ViewModel::CommitContextChanges() {
  if (!context().HasChanges())
    return;

  base::DictionaryValue diff;
  context().GetChangesAndReset(&diff);
  view_->OnContextChanged(diff);
}

}  // namespace webui_generator