OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/webui_generator/view.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "components/webui_generator/view_model.h" |
| 10 |
| 11 namespace webui_generator { |
| 12 |
| 13 View::View(const std::string& id) |
| 14 : parent_(NULL), |
| 15 id_(id), |
| 16 ready_children_(0), |
| 17 view_model_ready_(false), |
| 18 ready_(false), |
| 19 weak_factory_(this) { |
| 20 } |
| 21 |
| 22 View::~View() { |
| 23 } |
| 24 |
| 25 void View::Init() { |
| 26 CreateAndAddChildren(); |
| 27 |
| 28 if (!IsRootView()) |
| 29 path_ = parent_->path() + "$"; |
| 30 |
| 31 path_ += id_; |
| 32 view_model_.reset(CreateViewModel()); |
| 33 view_model_->SetView(this); |
| 34 |
| 35 for (auto& id_child : children_) |
| 36 id_child.second->Init(); |
| 37 |
| 38 if (children_.empty()) |
| 39 OnChildrenReady(); |
| 40 } |
| 41 |
| 42 bool View::IsRootView() const { |
| 43 return !parent_; |
| 44 } |
| 45 |
| 46 ViewModel* View::GetViewModel() const { |
| 47 return view_model_.get(); |
| 48 } |
| 49 |
| 50 void View::OnViewModelReady() { |
| 51 view_model_ready_ = true; |
| 52 if (ready_children_ == static_cast<int>(children_.size())) |
| 53 OnReady(); |
| 54 } |
| 55 |
| 56 View* View::GetChild(const std::string& id) const { |
| 57 auto it = children_.find(id); |
| 58 if (it == children_.end()) |
| 59 return nullptr; |
| 60 |
| 61 return it->second; |
| 62 } |
| 63 |
| 64 void View::OnReady() { |
| 65 if (ready_) |
| 66 return; |
| 67 |
| 68 ready_ = true; |
| 69 if (!IsRootView()) |
| 70 parent_->OnChildReady(this); |
| 71 } |
| 72 |
| 73 void View::UpdateContext(const base::DictionaryValue& diff) { |
| 74 DCHECK(ready_); |
| 75 view_model_->UpdateContext(diff); |
| 76 } |
| 77 |
| 78 void View::HandleEvent(const std::string& event) { |
| 79 DCHECK(ready_); |
| 80 view_model_->OnEvent(event); |
| 81 } |
| 82 |
| 83 void View::AddChild(View* child) { |
| 84 DCHECK(children_.find(child->id()) == children_.end()); |
| 85 DCHECK(!child->id().empty()); |
| 86 children_.set(child->id(), make_scoped_ptr(child)); |
| 87 child->set_parent(this); |
| 88 } |
| 89 |
| 90 void View::OnChildReady(View* child) { |
| 91 ++ready_children_; |
| 92 if (ready_children_ == static_cast<int>(children_.size())) |
| 93 OnChildrenReady(); |
| 94 } |
| 95 |
| 96 void View::OnChildrenReady() { |
| 97 view_model_->OnChildrenReady(); |
| 98 if (view_model_ready_) |
| 99 OnReady(); |
| 100 } |
| 101 |
| 102 } // namespace webui_generator |
OLD | NEW |