diff --git a/.bowerrc b/.bowerrc new file mode 100644 index 0000000..44491d3 --- /dev/null +++ b/.bowerrc @@ -0,0 +1,3 @@ +{ + "directory": "bower_components" +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a088b6f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +bower_components diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..47c894c --- /dev/null +++ b/bower.json @@ -0,0 +1,22 @@ +{ + "name": "ng-layout", + "main": "ng-layout.js", + "version": "0.0.0", + "authors": [ + "Dmitri Akatov " + ], + "description": "simple reusable layouts", + "keywords": [ + "layout" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test" + ], + "devDependencies": { + "angular": "~1.2.0" + } +} diff --git a/ng-layout.js b/ng-layout.js new file mode 100644 index 0000000..440192c --- /dev/null +++ b/ng-layout.js @@ -0,0 +1,43 @@ +'use strict'; + +angular.module('ngLayout', []) + .directive('ngLayout', [ + '$compile', '$timeout', '$templateCache', '$http', + function ($compile, $timeout, $templateCache, $http) { + var CONTENT_FOR = 'CONTENT_FOR' + var YIELD = 'YIELD' + return { + restrict: 'E', + scope: { src: '=' }, + transclude: true, + compile: function(elmt, attr, transclude) { + var element = elmt + return function(scope, element, attrs) { + $http.get(attr.src, {cache: $templateCache}).success(function(html) { + var dom = $compile(html)(scope.$parent) + element.append(dom) + transclude(scope.$parent, function(clone, innerScope) { + var cs = {} + angular.forEach(clone, function(c) { + var e = angular.element(c) + var a = e.attr(CONTENT_FOR) + if (!!a) { + cs[a] = $compile(e)(innerScope) + } + }) + angular.forEach(element.find(YIELD), function(e) { + var el = angular.element(e) + var a = el.attr(CONTENT_FOR) + var c = cs[a] + if (!!c) { + el.parent().append(c) + el.remove() + } + }) + }) + }) + } + } + } + } + ]) diff --git a/test/fixtures/index.html b/test/fixtures/index.html new file mode 100644 index 0000000..50e3aeb --- /dev/null +++ b/test/fixtures/index.html @@ -0,0 +1,26 @@ + + + + + + + +
+ {{ left }} + +
+ {{ left }} +
+
+ {{ right }} +
+
+
+ + diff --git a/test/fixtures/layout.html b/test/fixtures/layout.html new file mode 100644 index 0000000..cfd56f8 --- /dev/null +++ b/test/fixtures/layout.html @@ -0,0 +1,10 @@ + + + + + +
+ + + +