First version
[src/app-framework-demo.git] / afm-client / app / Frontend / services / JQueryEmu.js
1 /* 
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * Usage:
16  * 
17  *   // mouse event probably point on icon and not on button div
18  *   ... ng-click="LockChannel($event)
19      var target= angular.element(event.currentTarget);
20      var button= JQemu.FindInParent (target, 'div');
21  * 
22  */
23
24
25 (function () {
26     'use strict';
27
28
29           
30     // _all modules only reference dependencies
31     angular.module('JQueryEmu', [])
32
33             // Factory is a singleton and share its context within all instances.
34             .factory('JQemu', function () {
35
36                 var FindInParent = function (element, selector) {
37                     var parent = element;
38                     var search = selector.toUpperCase();
39                     while (parent[0]) {
40                         if (search === parent[0].tagName) {
41                             return parent;
42                         }  // HTMLDivElement properties
43                         parent = parent.parent();
44                     }
45                 };
46                 
47                 var  FindByTag= function (element, tag, selector) {
48                     var search = selector.toLowerCase();
49                     var type   = tag.toLowerCase()+ "Name";
50                     var children = element.children();
51                     while (children[0]) {
52                         if (search === children[0][type]) {
53                             return children;
54                         }  // HTMLDivElement properties
55                         children = children.next();
56                     }
57                 };
58                 
59                 var  FindByClass= function (element, selector) {
60                     var search = selector.toLowerCase();
61                     var children = element.children();
62                     while (children[0]) {
63                         if (children.hasClass(search)) {
64                             return children;
65                         }  // HTMLDivElement properties
66                         children = children.next();
67                     }
68                 };
69
70                 var myMethods = {
71                     FindInParent: FindInParent,
72                     FindByTag: FindByTag,
73                     FindByClass: FindByClass
74                 };
75
76                 return myMethods;
77             });
78
79 })();