Skip to content

Instantly share code, notes, and snippets.

@mcranston18
Last active August 29, 2015 14:21
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 mcranston18/b03514d1030396f24a3b to your computer and use it in GitHub Desktop.
Save mcranston18/b03514d1030396f24a3b to your computer and use it in GitHub Desktop.
generic semantic ui directive
/**
* semanticUi - Generic directive for Semantic UI Javascript initialization.
*/
'use strict';
angular.module('cozumo')
.directive('semanticUi', function ($timeout) {
return {
restrict: 'A',
require: '?ngModel',
scope: {
options: '=',
type: '@',
visible: '='
},
link: function (scope, element, attrs, ngModel) {
var uiElement = $(element)[0];
var options = angular.copy(scope.options) || {};
if (scope.type === 'dropdown') {
options.onChange = function (value) {
if (scope.options && scope.options.onChange) {
return scope.options.onChange(value);
}
if (!ngModel) {
return;
}
ngModel.$setViewValue(value);
};
}
if (scope.type === 'modal') {
options.onHidden = function () {
scope.visible = false;
scope.$apply();
};
}
var semanticElement = $(uiElement)[scope.type](options);
if (scope.type === 'modal') {
scope.$watch('visible', function (value) {
if (value) {
semanticElement.modal('show');
} else {
semanticElement.modal('hide');
}
});
}
if (scope.type === 'dropdown') {
if (ngModel) {
ngModel.$render = function () {
var newValue = ngModel.$viewValue;
$timeout(function () {
if (newValue !== null &&
newValue !== undefined &&
newValue !== '') {
semanticElement.dropdown('set selected', newValue);
} else {
semanticElement.dropdown('clear');
}
});
};
}
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment