Skip to content

Commit

Permalink
updating for 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
julien committed Nov 13, 2013
0 parents commit 68f3bc4
Show file tree
Hide file tree
Showing 19 changed files with 633 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.DS_Store
.localized
9 changes: 9 additions & 0 deletions LICENSE
@@ -0,0 +1,9 @@
This software is licensed under the MIT License.

Copyright Julien Castelain, 2013.

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.
24 changes: 24 additions & 0 deletions README.md
@@ -0,0 +1,24 @@
dart mv
==========

[![Build Status](https://drone.io/github.com/julien/dart_mv/status.png)](https://drone.io/github.com/julien/dart_mv/latest)

[Dart](http://www.dartlang.org) Model/View implementation.

A minimalist library that implements a Model and a View class.

A basic example is provided in the "web" directory.

Although the build status is "failing" since about two months,
the tests pass in Dartium.

**TODO**
- Add more tests.
- Generate documentation.

*[Available](http://pub.dartlang.org/packages/dart_mv) at the Dart [pub](http://pub.dartlang.org)*

**author**

*Julien Castelain* <jcastelain@gmail.com>

Binary file added lib/.DS_Store
Binary file not shown.
14 changes: 14 additions & 0 deletions lib/dart_mv.dart
@@ -0,0 +1,14 @@
library dart_mv;

import 'dart:async';
import 'dart:html';
import 'dart:convert';

part 'eventemitter.dart';
part 'models.dart';
part 'views.dart';
part 'router.dart';

void main() { }


51 changes: 51 additions & 0 deletions lib/eventemitter.dart
@@ -0,0 +1,51 @@
part of dart_mv;

class EventEmitter {

Map<String, StreamController> _controllers = new Map<String, StreamController>();

StreamController _createController(name) {
var ctrl = new StreamController(
onListen: () {},
onCancel: () {
_controllers[name].close();
off(name);
}
);
_controllers[name] = ctrl;
return _controllers[name];
}

Stream on(String name) {
var ctrl;
if (!_controllers.containsKey(name)) {
ctrl = _createController(name);
} else {
ctrl = _controllers[name];
}
return ctrl.stream;
}

void off(String name) {
if (_controllers.containsKey(name)) {
_controllers.remove(name);
}
}

void publish(String name, dynamic data) {
var ctrl;
if (_controllers.containsKey(name)) {
ctrl = _controllers[name];
} else {
ctrl = _createController(name);
}

if (ctrl.isClosed) {
return;
}

if (ctrl.hasListener && !ctrl.isPaused) {
ctrl.add(data);
}
}
}
82 changes: 82 additions & 0 deletions lib/models.dart
@@ -0,0 +1,82 @@
part of dart_mv;

class Model {

EventEmitter _emitter;

/// A map of attributes.
Map _attributes = new Map();

Model([Map attributes]) {

_emitter = new EventEmitter();

if (attributes != null) {
_attributes = new Map.from(attributes);
_attributes.forEach((k, v) => set(k, v));
}
}

Model set(String key, dynamic value) {
var data, type;

data = {};

if (_attributes.containsKey(key)) {
// "Nulling" the value means removing the
// attribute
if (value == null) {
_attributes.remove(key);
data = {'key': key, 'value': value };
_emitter.publish('remove', data);
return this;
}

if (_attributes[key] != value) {
data = {'key': key, 'oldValue': get(key), 'value': value };
type = 'change';
}

} else {
data = {'key': key, 'value': value };
type = 'add';
}

_attributes[key] = value;
_emitter.publish(type, data);
return this;
}

get(String key) {
if (_attributes.containsKey(key)) {
return _attributes[key];
}
}

/// Resets all of the attributes
void reset() => _attributes.clear();

/// [] Operator overloading, to be
/// able to use the model like and "array"
/// for attributes access.
operator [](String key) => get(key);

/// Returns a [Model] created with
/// 2 models who's attributes get merged.
Model operator +(Model m) {
m.attributes.forEach((key, value) {
_attributes.putIfAbsent(key, () {
return value;
});
});
return new Model(_attributes);
}

/// Attributes as JSON string.
toJson() => JSON.encode(_attributes);

Map<String, dynamic> get attributes => _attributes;

Stream on(String name) => _emitter.on(name);

}
40 changes: 40 additions & 0 deletions lib/router.dart
@@ -0,0 +1,40 @@
part of dart_mv;

class Router {

EventEmitter _emitter;

Map<String, dynamic> routes;

Router([Map<String, dynamic> this.routes]) {
_emitter = new EventEmitter();
window.onHashChange.listen(onHashChange);
}

void onHashChange(e) {
var hash, args, route, regexp;
hash = window.location.hash;
hash = hash.substring(0, 1);
args = [];

for (route in routes.keys) {
regexp = new RegExp(route);

regexp.allMatches(hash).forEach((match) {
args.add(match.group(0).substring(match.start, match.end));
});

_emitter.publish('change', {'route': route, 'args': args });

if (routes[route] is Function) {
routes[route](args);
return;
}
}
}

Stream on(String name) => _emitter.on(name);
}



60 changes: 60 additions & 0 deletions lib/views.dart
@@ -0,0 +1,60 @@
part of dart_mv;

class View {
static const String DEFAULT_TAG = 'div';

Element _el;
EventEmitter _emitter;

dynamic data;

Element _createElement([String id = '', String tagName = '', String className = '']) {
var tag;
if (id != null && querySelector(id) != null) {
_el = querySelector(id);
} else {
tag = (tagName == null) ? DEFAULT_TAG : tagName;
_el = new Element.tag(tag);
if (className != null) {
_el.classes.add(className);
}
}
return _el;
}

View({String id, String tagName, String className, dynamic this.data, String html}) {
_createElement(id, tagName, className);

if (html != null) {
template(html);
}
_emitter = new EventEmitter();
}

View template(String html) {
var element, fragment;
element = new Element.html(html);
fragment = document.createDocumentFragment();
_el.append(fragment.append(element));
return this;
}

/// Wrapper methods for the underlying [Element]
/// might be removed ...
void appendTo(Element parent) {
var fragment;
if (!document.body.contains(_el)) {
fragment = document.createDocumentFragment();
fragment.append(_el);
parent.append(fragment);
}
}

void remove() {
_el.remove();
}

Element get el => _el;
EventEmitter get emitter => _emitter;
}

16 changes: 16 additions & 0 deletions pubspec.yaml
@@ -0,0 +1,16 @@
name: dart_mv
author: Julien Castelain <jcastelain@gmail.com>
description:
A minimalist "model view" implementation
in Dart.
homepage: https://github.com/julien/dart_mv
version: 0.1.3

environment:
sdk: ">=0.8.10+6 <2.0.0"

dependencies:
unittest: "0.9.0"
browser: ">=0.9.0 < 0.10.0"


Binary file added test/.DS_Store
Binary file not shown.
41 changes: 41 additions & 0 deletions test/index.html
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<title>Dart MV</title>
</head>
<body>
<script type="application/dart" src="tests.dart"></script>
<script type="text/javascript">
//navigator.webkitStartDart();
</script>
<script>
(function () {
// only run if testRunner is defined -- we're in DRT
if (window.testRunner) {
// don't dump the structure.
// just the text of the output plus console output
testRunner.dumpAsText();

// don't finish when layout is done.
// instead, wait to be notified
testRunner.waitUntilDone();

// listen for messages from the test harness
window.addEventListener('message', function (event) {
console.log(event.data);
if (event.data === 'unittest-suite-done') {
console.log('OK');
testRunner.notifyDone();
}

}, false);

// listen for unhandled exceptions
window.addEventListener('error', function (e) {
testRunner.notifyDone();
});
}
}());
</script>
</body>
</html>
33 changes: 33 additions & 0 deletions test/run.sh
@@ -0,0 +1,33 @@
#!/bin/bash

set -e

#####
# Unit Tests

echo "DumpRenderTree test/index.html"
results=`DumpRenderTree test/index.html 2>&1`

echo "$results" | grep CONSOLE

echo $results | grep 'unittest-suite-done' >/dev/null

echo $results | grep -v 'Exception: Some tests failed.' >/dev/null

#####
# Type Analysis

echo
echo "dart_analyzer lib/*.dart"

results=`dart_analyzer lib/*.dart 2>&1`

echo "$results"

if [ -n "$results" ]; then
exit 1
else
echo "Passed analysis."
fi


0 comments on commit 68f3bc4

Please sign in to comment.