cl
2024-05-23 ba1a7a9ef126296e2798e313dc5b43f775a1123c
提交 | 用户 | 时间
71e81e 1 /*
2  * Activiti Modeler component part of the Activiti project
3  * Copyright 2005-2014 Alfresco Software, Ltd. All rights reserved.
4  * 
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 /**
21  * Utility methods are grouped together here.
22  */
23 var EDITOR = EDITOR || {};
24
25 EDITOR.UTIL = {
26
27     getParameterByName: function (name) {
28         name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
29         var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
30             results = regex.exec(location.search);
31         return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
32     },
33
34     /**
35      * Starts at the provided start element, and walks all preceding elements in the graph.
36      * Each element is tested to have a certain property and, if it has, adds this property value
37      * to the return result list.
38      */
39     collectPropertiesFromPrecedingElements: function (startElement, propertyType) {
40         var visitedElements = [];
41         var collectedProperties = [];
42         EDITOR.UTIL._visitElementAndCollectProperty(startElement, propertyType, visitedElements, collectedProperties);
43         return collectedProperties;
44     },
45
46     /**
47      * Starts at the provided start element, and walks all preceding elements in the graph.
48      * Each element is tested to be a specific stencil id and, if it has, adds the element
49      * to the return result list.
50      */
51     collectElementsFromPrecedingElements: function (startElement, stencilId) {
52         var visitedElements = [];
53         var collectedElements = [];
54
55         var incomingShapesIterator = startElement.getIncomingShapes();
56         if (incomingShapesIterator) {
57             for (var i = 0; i < incomingShapesIterator.length; i++) {
58                 var incomingShape = incomingShapesIterator[i];
59                 if (visitedElements.indexOf(incomingShape.id) < 0) {
60                     EDITOR.UTIL._visitElementAndCollectElement(incomingShape, stencilId, visitedElements, collectedElements);
61                 }
62             }
63         }
64
65         return collectedElements;
66     },
67
68     _visitElementAndCollectProperty: function (element, propertyType, visitedElementsArray, collectedProperties) {
69
70         visitedElementsArray.push(element.id);
71
72         var property = element.properties[propertyType]
73         if (property) {
74             collectedProperties.push(property);
75         }
76
77         var incomingShapesIterator = element.getIncomingShapes();
78         if (incomingShapesIterator) {
79             for (var i = 0; i < incomingShapesIterator.length; i++) {
80                 var incomingShape = incomingShapesIterator[i];
81                 if (visitedElementsArray.indexOf(incomingShape.id) < 0) {
82                     EDITOR.UTIL._visitElementAndCollectProperty(incomingShape, propertyType, visitedElementsArray, collectedProperties);
83                 }
84             }
85         }
86     },
87
88     _visitElementAndCollectElement: function (element, stencilId, visitedElementsArray, collectedElements) {
89
90         visitedElementsArray.push(element.id);
91
92         var elementStencilId = element.getStencil().id();
93         if (elementStencilId && elementStencilId.indexOf(stencilId) >= 0) {
94             collectedElements.push(element);
95         }
96
97         var incomingShapesIterator = element.getIncomingShapes();
98         if (incomingShapesIterator) {
99             for (var i = 0; i < incomingShapesIterator.length; i++) {
100                 var incomingShape = incomingShapesIterator[i];
101                 if (visitedElementsArray.indexOf(incomingShape.id) < 0) {
102                     EDITOR.UTIL._visitElementAndCollectElement(incomingShape, stencilId, visitedElementsArray, collectedElements);
103                 }
104             }
105         }
106     },
107
108     /**
109      * Goes up the chain of parents of the provided element.
110      * When the property is encountered, its value is immediately returned.
111      * If the chain of parents is completely walked through, undefined is returned.
112      */
113     getPropertyFromParent: function (element, propertyType) {
114         if (element.parent) {
115             return EDITOR.UTIL._getPropertyFromParent(element.parent, propertyType);
116         } else {
117             return undefined;
118         }
119
120     },
121
122     _getPropertyFromParent: function (parentElement, propertyType) {
123         var property = parentElement.properties[propertyType];
124         if (property) {
125             return property;
126         }
127
128         if (parentElement.parent) {
129             return EDITOR.UTIL._getPropertyFromParent(parentElement.parent, propertyType);
130         } else {
131             return undefined;
132         }
133     }
134
135 };