Advertisement
Guest User

Untitled

a guest
May 26th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.32 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var toBoolean = function(value) {
  4. if (value && value.length !== 0) {
  5. var v = angular.lowercase("" + value);
  6. value = !(v === 'f' || v === '0' || v === 'false' || v === 'no' || v === 'n' || v === '[]');
  7. } else {
  8. value = false;
  9. }
  10. return value;
  11. };
  12.  
  13. var isIE = function() {
  14. var ie = parseInt((/msie (\d+)/.exec(angular.lowercase(navigator.userAgent)) || [])[1], 10);
  15. if (isNaN(ie)) {
  16. ie = parseInt((/trident\/.*; rv:(\d+)/.exec(angular.lowercase(navigator.userAgent)) || [])[1], 10);
  17. }
  18. return ie;
  19. }
  20.  
  21. var showHideBinder = function(elm, attr, value) {
  22. var show = (attr === 'show') ? '' : 'none';
  23. var hide = (attr === 'hide') ? '' : 'none';
  24. elm.css('display', toBo olean(value) ? show : hide);
  25. };
  26.  
  27. var classBinder = function(elm, value) {
  28. if (angular.isObject(value) && !angular.isArray(value)) {
  29. var results = [];
  30. angular.forEach(value, function(value, index) {
  31. if (value) results.push(index);
  32. });
  33. value = results;
  34. }
  35. if (value) {
  36. elm.addClass(angular.isArray(value) ? value.join(' ') : value);
  37. }
  38. };
  39.  
  40. var transclude = function(transcluder, scope) {
  41. transcluder.transclude(scope, function(clone) {
  42. var parent = transcluder.element.parent();
  43. var afterNode = transcluder.element && transcluder.element[transcluder.element.length - 1];
  44. var parentNode = parent && parent[0] || afterNode && afterNode.parentNode;
  45. var afterNextSibling = (afterNode && afterNode.nextSibling) || null;
  46. angular.forEach(clone, function(node) {
  47. parentNode.insertBefore(node, afterNextSibling);
  48. });
  49. });
  50. };
  51.  
  52. ///-------Directive Methods
  53.  
  54.  
  55.  
  56. angular
  57. .module('tcApp')
  58. .directive('oneway', [function() {
  59. return {
  60. restrict: "AM",
  61. controller: ['$scope', '$element', '$attrs', '$interpolate', function($scope, $element, $attrs, $interpolate) {
  62. return {
  63. watcherRemover: undefined,
  64. binders: [],
  65. group: $attrs.oneName,
  66. element: $element,
  67. ran: false,
  68.  
  69. addBinder: function(binder) {
  70. this.binders.push(binder);
  71.  
  72. // In case of late binding (when using the directive bo-name/bo-parent)
  73. // it happens only when you use nested oneway, if the bo-children
  74. // are not dom children the linking can follow another order
  75. if (this.ran) {
  76. this.runBinders();
  77. }
  78. },
  79.  
  80. setupWatcher: function(onewayValue) {
  81. var that = this;
  82. this.watcherRemover = $scope.$watch(onewayValue, function(newValue) {
  83. if (newValue === undefined) return;
  84. that.removeWatcher();
  85. that.checkBindonce(newValue);
  86. }, true);
  87. },
  88.  
  89. checkBindonce: function(value) {
  90. var that = this,
  91. promise = (value.$promise) ? value.$promise.then : value.then;
  92. // since Angular 1.2 promises are no longer
  93. // undefined until they don't get resolved
  94. if (typeof promise === 'function') {
  95. promise(function() {
  96. that.runBinders();
  97. });
  98. } else {
  99. that.runBinders();
  100. }
  101. },
  102.  
  103. removeWatcher: function() {
  104. if (this.watcherRemover !== undefined) {
  105. this.watcherRemover();
  106. this.watcherRemover = undefined;
  107. }
  108. },
  109.  
  110. runBinders: function() {
  111. while (this.binders.length > 0) {
  112. var binder = this.binders.shift();
  113.  
  114. if (this.group && this.group != binder.group) continue;
  115.  
  116. var value = binder.scope.$eval( (binder.interpolate) ? $interpolate(binder.value) : binder.value );
  117.  
  118. switch (binder.attr) {
  119. case 'oneIf':
  120. if (toBoolean(value)) {
  121. transclude(binder, binder.scope.$new());
  122. }
  123. break;
  124. case 'oneSwitch':
  125. var selectedTranscludes, switchCtrl = binder.controller[0];
  126. if ((selectedTranscludes = switchCtrl.cases['!' + value] || switchCtrl.cases['?'])) {
  127. binder.scope.$eval(binder.attrs.change);
  128. angular.forEach(selectedTranscludes, function(selectedTransclude) {
  129. transclude(selectedTransclude, binder.scope.$new());
  130. });
  131. }
  132. break;
  133. case 'oneSwitchWhen':
  134. var ctrl = binder.controller[0];
  135. ctrl.cases['!' + binder.attrs.oneSwitchWhen] = (ctrl.cases['!' + binder.attrs.oneSwitchWhen] || []);
  136. ctrl.cases['!' + binder.attrs.oneSwitchWhen].push({
  137. transclude: binder.transclude,
  138. element: binder.element
  139. });
  140. break;
  141. case 'oneSwitchDefault':
  142. var ctrl = binder.controller[0];
  143. ctrl.cases['?'] = (ctrl.cases['?'] || []);
  144. ctrl.cases['?'].push({
  145. transclude: binder.transclude,
  146. element: binder.element
  147. });
  148. break;
  149. case 'hide':
  150. case 'show':
  151. showHideBinder(binder.element, binder.attr, value);
  152. break;
  153. case 'class':
  154. classBinder(binder.element, value);
  155. break;
  156. case 'text':
  157. binder.element.text(value);
  158. break;
  159. case 'html':
  160. binder.element.html(value);
  161. break;
  162. case 'style':
  163. binder.element.css(value);
  164. break;
  165. case 'disabled':
  166. binder.element.prop('disabled', value);
  167. break;
  168. case 'src':
  169. binder.element.attr(binder.attr, value);
  170. if (isIE()) binder.element.prop('src', value);
  171. break;
  172. case 'attr':
  173. angular.forEach(binder.attrs, function(attrValue, attrKey) {
  174. var newAttr, newValue;
  175.  
  176. if (attrKey.match(/^oneAttr./) && binder.attrs[attrKey]) {
  177. newAttr = attrKey.replace(/^oneAttr/, '').replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
  178. newValue = binder.scope.$eval(binder.attrs[attrKey]);
  179. binder.element.attr(newAttr, newValue);
  180. }
  181. });
  182. break;
  183. case 'href':
  184. case 'alt':
  185. case 'title':
  186. case 'id':
  187. case 'value':
  188. binder.element.attr(binder.attr, value);
  189. break;
  190. }
  191. }
  192. this.ran = true;
  193. }
  194. };
  195. }],
  196.  
  197. link: function(scope, elm, attrs, onewayController) {
  198. var value = attrs.oneway && scope.$eval(attrs.oneway);
  199. if (value !== undefined) {
  200. onewayController.checkBindonce(value);
  201. } else {
  202. onewayController.setupWatcher(attrs.oneway);
  203. elm.bind("$destroy", onewayController.removeWatcher);
  204. }
  205. }
  206. };
  207. });
  208.  
  209. angular.forEach(
  210. [{
  211. directiveName: 'oneShow',
  212. attribute: 'show'
  213. }, {
  214. directiveName: 'oneHide',
  215. attribute: 'hide'
  216. }, {
  217. directiveName: 'oneClass',
  218. attribute: 'class'
  219. }, {
  220. directiveName: 'oneText',
  221. attribute: 'text'
  222. }, {
  223. directiveName: 'oneBind',
  224. attribute: 'text'
  225. }, {
  226. directiveName: 'oneHtml',
  227. attribute: 'html'
  228. }, {
  229. directiveName: 'oneSrcI',
  230. attribute: 'src',
  231. interpolate: true
  232. }, {
  233. directiveName: 'oneSrc',
  234. attribute: 'src'
  235. }, {
  236. directiveName: 'oneHrefI',
  237. attribute: 'href',
  238. interpolate: true
  239. }, {
  240. directiveName: 'oneHref',
  241. attribute: 'href'
  242. }, {
  243. directiveName: 'oneAlt',
  244. attribute: 'alt'
  245. }, {
  246. directiveName: 'oneTitle',
  247. attribute: 'title'
  248. }, {
  249. directiveName: 'oneId',
  250. attribute: 'id'
  251. }, {
  252. directiveName: 'oneStyle',
  253. attribute: 'style'
  254. }, {
  255. directiveName: 'oneDisabled',
  256. attribute: 'disabled'
  257. }, {
  258. directiveName: 'oneValue',
  259. attribute: 'value'
  260. }, {
  261. directiveName: 'oneAttr',
  262. attribute: 'attr'
  263. },
  264.  
  265. {
  266. directiveName: 'oneIf',
  267. transclude: 'element',
  268. terminal: true,
  269. priority: 1000
  270. }, {
  271. directiveName: 'oneSwitch',
  272. require: 'oneSwitch',
  273. controller: function() {
  274. this.cases = {};
  275. }
  276. }, {
  277. directiveName: 'oneSwitchWhen',
  278. transclude: 'element',
  279. priority: 800,
  280. require: '^oneSwitch'
  281. }, {
  282. directiveName: 'oneSwitchDefault',
  283. transclude: 'element',
  284. priority: 800,
  285. require: '^oneSwitch'
  286. }
  287. ],
  288. function(oneDirective) {
  289.  
  290. return onewayModule.directive(oneDirective.directiveName, function() {
  291.  
  292. var onewayDirective = {
  293.  
  294. priority: oneDirective.priority || 200,
  295. transclude: oneDirective.transclude || false,
  296. terminal: oneDirective.terminal || false,
  297. require: ['^oneway'].concat(oneDirective.require || []),
  298. controller: oneDirective.controller,
  299.  
  300. compile: function(tElement, tAttrs, transclude) {
  301.  
  302. return function(scope, elm, attrs, controllers) {
  303. var onewayController = controllers[0],
  304. name = attrs.oneParent,
  305. element = onewayController.element.parent(),
  306. parentValue;
  307.  
  308. if (name && onewayController.group !== name) {
  309. onewayController = undefined;
  310.  
  311. while (element[0].nodeType !== 9 && element.length) {
  312.  
  313. if ((parentValue = element.data('$onewayController')) && parentValue.group === name) {
  314. onewayController = parentValue;
  315. break;
  316. }
  317. element = element.parent();
  318. }
  319.  
  320. if (!onewayController)
  321. throw new Error("Missing controller oneway: " + name);
  322. }
  323.  
  324. onewayController.addBinder({
  325. element : elm,
  326. attr : oneDirective.attribute || oneDirective.directiveName,
  327. attrs : attrs,
  328. value : attrs[oneDirective.directiveName],
  329. interpolate : oneDirective.interpolate,
  330. group : name,
  331. transclude : transclude,
  332. controller : controllers.slice(1),
  333. scope : scope
  334. });
  335. };
  336.  
  337. }
  338. };
  339.  
  340. return onewayDirective;
  341. });
  342. })
  343. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement