mentio.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. 'use strict';
  2. angular.module('mentio-demo', [])
  3. .config(function($routeProvider) {
  4. $routeProvider
  5. .when('/', {
  6. templateUrl: 'examples.html',
  7. tab: 'examples',
  8. title: 'Ment.io examples'
  9. })
  10. .when('/documentation', {
  11. templateUrl: 'documentation.html',
  12. tab: 'documentation',
  13. title: 'Ment.io Documentation'
  14. })
  15. .when('/examples', {
  16. templateUrl: 'examples.html',
  17. tab: 'examples',
  18. title: 'Ment.io examples'
  19. });
  20. })
  21. .run(function ($rootScope) {
  22. $rootScope.$on('$routeChangeSuccess', function (event, current) {
  23. if (current.$$route) {
  24. $rootScope.title = current.$$route.title;
  25. $rootScope.tab = current.$$route.tab;
  26. }
  27. });
  28. })
  29. .controller('mentio-demo-ctrl', function ($scope, $rootScope, $http, $q, $sce, $timeout, mentioUtil) {
  30. $scope.tinyMceOptions = {
  31. init_instance_callback: function(editor) {
  32. $scope.iframeElement = editor.iframeElement;
  33. }
  34. };
  35. $scope.macros = {
  36. 'brb': 'Be right back',
  37. 'omw': 'On my way',
  38. '(smile)' : '<img src="http://a248.e.akamai.net/assets.github.com/images/icons/emoji/smile.png"' +
  39. ' height="20" width="20">'
  40. };
  41. // shows the use of dynamic values in mentio-id and mentio-for to link elements
  42. $scope.myIndexValue = "5";
  43. $scope.searchProducts = function(term) {
  44. var prodList = [];
  45. return $http.get('productdata.json').then(function (response) {
  46. angular.forEach(response.data, function(item) {
  47. if (item.title.toUpperCase().indexOf(term.toUpperCase()) >= 0) {
  48. prodList.push(item);
  49. }
  50. });
  51. $scope.products = prodList;
  52. return $q.when(prodList);
  53. });
  54. };
  55. $scope.searchPeople = function(term) {
  56. var peopleList = [];
  57. return $http.get('peopledata.json').then(function (response) {
  58. angular.forEach(response.data, function(item) {
  59. if (item.name.toUpperCase().indexOf(term.toUpperCase()) >= 0) {
  60. peopleList.push(item);
  61. }
  62. });
  63. $scope.people = peopleList;
  64. return $q.when(peopleList);
  65. });
  66. };
  67. $scope.searchSimplePeople = function(term) {
  68. return $http.get('simplepeopledata.json').then(function (response) {
  69. $scope.simplePeople = [];
  70. angular.forEach(response.data, function(item) {
  71. if (item.label.toUpperCase().indexOf(term.toUpperCase()) >= 0) {
  72. $scope.simplePeople.push(item);
  73. }
  74. });
  75. });
  76. };
  77. $scope.getProductText = function(item) {
  78. return '[~<strong>' + item.sku + '</strong>]';
  79. };
  80. $scope.getProductTextRaw = function(item) {
  81. var deferred = $q.defer();
  82. /* the select() function can also return a Promise which ment.io will handle
  83. propertly during replacement */
  84. // simulated async promise
  85. $timeout(function() {
  86. deferred.resolve('#' + item.sku);
  87. }, 500);
  88. return deferred.promise;
  89. };
  90. $scope.getPeopleText = function(item) {
  91. // note item.label is sent when the typedText wasn't found
  92. return '[~<i>' + (item.name || item.label) + '</i>]';
  93. };
  94. $scope.getPeopleTextRaw = function(item) {
  95. return '@' + item.name;
  96. };
  97. $scope.resetDemo = function() {
  98. // finally enter content that will raise a menu after everything is set up
  99. $timeout(function() {
  100. var html = "Try me @ or add a macro like brb, omw, (smile)";
  101. var htmlContent = document.querySelector('#htmlContent');
  102. if (htmlContent) {
  103. var ngHtmlContent = angular.element(htmlContent);
  104. ngHtmlContent.html(html);
  105. ngHtmlContent.scope().htmlContent = html;
  106. // select right after the @
  107. mentioUtil.selectElement(null, htmlContent, [0], 8);
  108. ngHtmlContent.scope().$apply();
  109. }
  110. }, 0);
  111. };
  112. $rootScope.$on('$routeChangeSuccess', function (event, current) {
  113. $scope.resetDemo();
  114. });
  115. $scope.theTextArea = 'Type an # and some text';
  116. $scope.theTextArea2 = 'Type an @';
  117. $scope.searchSimplePeople('');
  118. $scope.resetDemo();
  119. })
  120. .directive('contenteditable', ['$sce', function($sce) {
  121. return {
  122. restrict: 'A', // only activate on element attribute
  123. require: '?ngModel', // get a hold of NgModelController
  124. link: function(scope, element, attrs, ngModel) {
  125. function read() {
  126. var html = element.html();
  127. // When we clear the content editable the browser leaves a <br> behind
  128. // If strip-br attribute is provided then we strip this out
  129. if (attrs.stripBr && html === '<br>') {
  130. html = '';
  131. }
  132. ngModel.$setViewValue(html);
  133. }
  134. if(!ngModel) return; // do nothing if no ng-model
  135. // Specify how UI should be updated
  136. ngModel.$render = function() {
  137. if (ngModel.$viewValue !== element.html()) {
  138. element.html($sce.getTrustedHtml(ngModel.$viewValue || ''));
  139. }
  140. };
  141. // Listen for change events to enable binding
  142. element.on('blur keyup change', function() {
  143. scope.$apply(read);
  144. });
  145. read(); // initialize
  146. }
  147. };
  148. }])
  149. .filter('words', function () {
  150. return function (input, words) {
  151. if (isNaN(words)) {
  152. return input;
  153. }
  154. if (words <= 0) {
  155. return '';
  156. }
  157. if (input) {
  158. var inputWords = input.split(/\s+/);
  159. if (inputWords.length > words) {
  160. input = inputWords.slice(0, words).join(' ') + '\u2026';
  161. }
  162. }
  163. return input;
  164. };
  165. });