Skip to content

Instantly share code, notes, and snippets.

@lpand
Created March 20, 2014 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lpand/9665376 to your computer and use it in GitHub Desktop.
Save lpand/9665376 to your computer and use it in GitHub Desktop.
;(function (angular) {
"use strict";
var app = angular.module("martVisualEnrichment.directives");
/**
*
* This implementation is highly inspired by https://github.com/angular-ui/bootstrap/blob/master/src/tabs/tabs.js
*
*/
app.controller("HSectionCtrl", ["$scope", function HSectionCtrl ($scope) {
var ctrl = this, sections = $scope.sections = ctrl.sections = [];
ctrl.addSection = function (section) {
ctrl.sections.push(section);
if (sections.length === 1 || section.active) {
ctrl.select(section);
}
return this;
}
ctrl.select = function (section) {
angular.forEach(sections, function (s) {
if (s.active && s !== section) {
s.active = false;
}
});
section.active = true;
return this;
}
}]);
app.directive("hsectionset", function mveSets() {
// In this way, sections share the same controller
return {
restrict: "E",
transclude: true,
replace: true,
scope: {},
controller: "HSectionCtrl",
templateUrl: "partials/hsection/hsectionset.html"
};
});
app.directive("hsection", [function mveSetSection () {
return {
restrict: "E",
require: "^hsectionset",
transclude: true,
replace: true,
scope: {
heading: "@"
},
controller: function () {},
templateUrl: "partials/hsection/hsection.html",
link: function (scope, elm, attrs, ctrl, transcludeFn) {
ctrl.addSection(scope);
scope.$transcludeFn = transcludeFn;
}
}
}]);
app.directive("hsectionContentTransclude", function hsectionContentTransclude (){
return {
restrict: "A",
require: "^hsectionset",
link: function (scope, elm, attrs, ctrl) {
var sec = scope.$eval(atts.hsectionContentTransclude);
// scope coming from the first element that introduces it, above
// the horizontal section.
sec.$transcludeFn(sec.$parent, function (contents) {
angular.forEach(contents, function (node) {
elm.append(node);
});
});
}
};
});
})(angular);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment