app.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. 'use strict';
  2. angular.module('filters', [])
  3. .filter('camelCase', function(){
  4. return function(input) {
  5. var words = input.split('-');
  6. return words.map(function(value){
  7. return value.charAt(0).toUpperCase() + value.slice(1);
  8. }).join(' ');
  9. }
  10. });
  11. angular.module('controllers', [])
  12. .controller('HeaderCtrl', function($scope, $state, $rootScope){
  13. $scope.headerContents = {
  14. main: {
  15. heading: 'Getting Started',
  16. desc: 'An overview of nya-bootstrap-select, how to install and use, basic usages and examples. migrate instructions from old version.'
  17. },
  18. examples: {
  19. heading: 'Examples',
  20. desc: 'Examples to demonstrate how to utilities the power of nya-bootstrap-select, also, contains some features from Bootstrap-select'
  21. },
  22. api: {
  23. heading: 'API Reference',
  24. desc: 'Reference for nya-bs-select and nya-bs-option directives'
  25. }
  26. };
  27. $scope.$on('$stateChangeSuccess', function() {
  28. $scope.currentState = $state.current;
  29. $rootScope.isHome = $state.is('home');
  30. });
  31. $scope.$watch('isHome', function(newValue) {
  32. console.log('isHome: ', newValue);
  33. });
  34. })
  35. .controller('MainCtrl', function($scope, pages, $filter){
  36. $scope.articles = [];
  37. angular.forEach(pages, function(stateName){
  38. $scope.articles.push({
  39. state: stateName,
  40. title: $filter('camelCase')(stateName)
  41. });
  42. });
  43. })
  44. .controller('ExamplesCtrl', function($scope, pages, $filter){
  45. $scope.articles = [];
  46. angular.forEach(pages, function(stateName){
  47. $scope.articles.push({
  48. state: stateName,
  49. title: $filter('camelCase')(stateName)
  50. });
  51. });
  52. })
  53. .controller('ApiCtrl', function($scope, pages){
  54. $scope.articles = [];
  55. angular.forEach(pages, function(stateName){
  56. var words = stateName.split('-');
  57. var title = words.map(function(word, index) {
  58. return index === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1);
  59. }).join('');
  60. $scope.articles.push({
  61. state: stateName,
  62. title: title
  63. });
  64. });
  65. });
  66. angular.module('directives', [])
  67. .directive('example', function() {
  68. return {
  69. template: '<div class="panel panel-default example-panel">' +
  70. '<div class="panel-heading">EXAMPLE</div>' +
  71. '<div class="panel-body">' +
  72. '<ul class="nav nav-tabs">' +
  73. '<li ng-repeat="file in fileList" ng-class="{active: currentFile===file}">' +
  74. '<a ng-click="changeTab(file)">{{file}}</a>' +
  75. '</li>' +
  76. '</ul>' +
  77. '<div class="file-container" ng-transclude=""></div>' +
  78. '</div>' +
  79. '</div>',
  80. transclude: true,
  81. restrict: 'E',
  82. scope: {},
  83. controller: function($scope) {
  84. $scope.fileList = [];
  85. this.addFile = function(name) {
  86. $scope.fileList.push(name);
  87. if(!$scope.currentFile) {
  88. $scope.currentFile = $scope.fileList[0];
  89. }
  90. };
  91. },
  92. link: function($scope, $element) {
  93. $element.find('.panel-body').append($element.next());
  94. $scope.changeTab = function(file) {
  95. $scope.currentFile = file;
  96. };
  97. $scope.$watch('currentFile', function(file) {
  98. console.log('currentFile', file);
  99. if(file){
  100. $element.find('file:not([name="'+ file +'"])').hide();
  101. $element.find('file[name="'+ file +'"]').show();
  102. }
  103. })
  104. }
  105. };
  106. })
  107. .directive('file', function() {
  108. return {
  109. restrict: 'E',
  110. require: '^example',
  111. template: '<div class="example-file" ng-transclude=""></div>',
  112. scope: {
  113. fileName: '@name'
  114. },
  115. transclude: true,
  116. link: function postLink($scope, $element, $attrs, $ctrl) {
  117. $ctrl.addFile($scope.fileName);
  118. }
  119. };
  120. });
  121. angular.module('docApp', ['ui.router', 'nya.bootstrap.select', 'directives', 'filters', 'controllers'])
  122. .config(function($stateProvider, $urlRouterProvider, $locationProvider){
  123. $locationProvider.hashPrefix('!');
  124. var pages = {
  125. main: [
  126. 'getting-started',
  127. 'migrate-instructions'
  128. ],
  129. examples: [
  130. 'basic-usage',
  131. 'groups-and-more',
  132. 'form-control-feature',
  133. 'layout-and-styles',
  134. 'live-search',
  135. 'alternative-display',
  136. 'select-text-format',
  137. 'tick-icon',
  138. 'custom-content',
  139. 'disable-an-option',
  140. 'control-dropdown-menu-size',
  141. 'show-menu-arrow'
  142. ],
  143. api: [
  144. 'nya-bs-select',
  145. 'nya-bs-option',
  146. 'nya-bs-config-provider'
  147. ]
  148. };
  149. $urlRouterProvider
  150. .when('/main', '/main/'+ pages.main[0])
  151. .when('/examples', '/examples/' + pages.examples[0])
  152. .when('/api', '/api/' + pages.api[0])
  153. .otherwise('/');
  154. $stateProvider.state('home', {
  155. url: '/'
  156. });
  157. angular.forEach(pages, function(children, stateName){
  158. $stateProvider.state(stateName, {
  159. templateUrl: 'partials/' + stateName + '.html',
  160. controller: capitalize(stateName) + 'Ctrl',
  161. url: '/' + stateName,
  162. resolve: {
  163. pages: function() {
  164. return children;
  165. }
  166. }
  167. });
  168. angular.forEach(children, function(childState) {
  169. console.log('partials/' + stateName + '/' + childState + '.html');
  170. $stateProvider.state(childState, {
  171. templateUrl: 'partials/' + stateName + '/' + childState + '.html',
  172. url: '/' + childState,
  173. parent: stateName
  174. });
  175. });
  176. });
  177. function capitalize(str) {
  178. return str.charAt(0).toUpperCase() + str.slice(1);
  179. }
  180. });