initialScriptText.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. bind = {};
  2. var library = {
  3. 'version': '4.0',
  4. "defineProperties": Object.defineProperties || function (obj, properties) {
  5. function convertToDescriptor(desc) {
  6. function hasProperty(obj, prop) {
  7. return Object.prototype.hasOwnProperty.call(obj, prop);
  8. }
  9. function isCallable(v) {
  10. // NB: modify as necessary if other values than functions are callable.
  11. return typeof v === "function";
  12. }
  13. if (typeof desc !== "object" || desc === null)
  14. throw new TypeError("bad desc");
  15. var d = {};
  16. if (hasProperty(desc, "enumerable"))
  17. d.enumerable = !!obj.enumerable;
  18. if (hasProperty(desc, "configurable"))
  19. d.configurable = !!obj.configurable;
  20. if (hasProperty(desc, "value"))
  21. d.value = obj.value;
  22. if (hasProperty(desc, "writable"))
  23. d.writable = !!desc.writable;
  24. if (hasProperty(desc, "get")) {
  25. var g = desc.get;
  26. if (!isCallable(g) && typeof g !== "undefined")
  27. throw new TypeError("bad get");
  28. d.get = g;
  29. }
  30. if (hasProperty(desc, "set")) {
  31. var s = desc.set;
  32. if (!isCallable(s) && typeof s !== "undefined")
  33. throw new TypeError("bad set");
  34. d.set = s;
  35. }
  36. if (("get" in d || "set" in d) && ("value" in d || "writable" in d))
  37. throw new TypeError("identity-confused descriptor");
  38. return d;
  39. }
  40. if (typeof obj !== "object" || obj === null)
  41. throw new TypeError("bad obj");
  42. properties = Object(properties);
  43. var keys = Object.keys(properties);
  44. var descs = [];
  45. for (var i = 0; i < keys.length; i++)
  46. descs.push([keys[i], convertToDescriptor(properties[keys[i]])]);
  47. for (var i = 0; i < descs.length; i++)
  48. Object.defineProperty(obj, descs[i][0], descs[i][1]);
  49. return obj;
  50. },
  51. 'typeOf': function(item){
  52. if (item == null) return 'null';
  53. if (item.$family != null) return item.$family();
  54. if (item.constructor == Array) return 'array';
  55. if (item.nodeName){
  56. if (item.nodeType == 1) return 'element';
  57. if (item.nodeType == 3) return (/\S/).test(item.nodeValue) ? 'textnode' : 'whitespace';
  58. } else if (typeof item.length == 'number'){
  59. if (item.callee) return 'arguments';
  60. //if ('item' in item) return 'collection';
  61. }
  62. return typeof item;
  63. },
  64. 'JSONDecode': function(string, secure){
  65. if (!string || library.typeOf(string) != 'string') return null;
  66. return eval('(' + string + ')');
  67. },
  68. 'JSONEncode': function(obj){
  69. if (obj && obj.toJSON) obj = obj.toJSON();
  70. switch (library.typeOf(obj)){
  71. case 'string':
  72. return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"';
  73. case 'array':
  74. var string = [];
  75. for (var i=0; i<obj.length; i++){
  76. var json = library.JSONEncode(obj[i]);
  77. if (json) string.push(json);
  78. }
  79. return '[' + string + ']';
  80. case 'object': case 'hash':
  81. var string = [];
  82. for (key in obj){
  83. var json = library.JSONEncode(obj[key]);
  84. if (json) string.push(library.JSONEncode(key) + ':' + json);
  85. }
  86. return '{' + string + '}';
  87. case 'number': case 'boolean': return '' + obj;
  88. case 'null': return 'null';
  89. }
  90. return null;
  91. }
  92. };
  93. (function(){
  94. var o={"indexOf": {
  95. "value": function(item, from){
  96. var length = this.length >>> 0;
  97. for (var i = (from < 0) ? Math.max(0, length + from) : from || 0; i < length; i++){
  98. if (this[i] === item) return i;
  99. }
  100. return -1;
  101. }
  102. }};
  103. library.defineProperties(Array.prototype, o);
  104. })();
  105. var wrapWorkContext = {
  106. "getTask": function(){return library.JSONDecode(workContext.getCurrentTaskCompleted());},
  107. "getWork": function(){return library.JSONDecode(workContext.getWork());},
  108. "getActivity": function(){return library.JSONDecode(workContext.getActivity());},
  109. "getTaskList": function(){return library.JSONDecode(workContext.getTaskList());},
  110. "getTaskCompletedList": function(){return library.JSONDecode(workContext.getTaskCompletedList());},
  111. "getWorkLogList": function(){return library.JSONDecode(workContext.getWorkLogList());},
  112. "getAttachmentList": function(){return library.JSONDecode(workContext.getAttachmentList());},
  113. "getRouteList": function(){return library.JSONDecode(workContext.getRouteList());},
  114. "getInquiredRouteList": function(){return library.JSONDecode(workContext.getInquiredRouteList());},
  115. "setTitle": function(title){workContext.setTitle(title);},
  116. "getControl": function(){return null;}
  117. };
  118. var includedScripts = [];
  119. var _self = this;
  120. var include = function(name, callback){
  121. if (includedScripts.indexOf(name)==-1){
  122. var json = library.JSONDecode(_self.workContext.getScript(name, includedScripts));
  123. includedScripts = includedScripts.concat(json.importedList);
  124. if (json.text){
  125. MWF.Macro.exec(json.data.text, bind);
  126. if (callback) callback.apply(bind);
  127. }
  128. }
  129. };
  130. var define = function(name, fun, overwrite){
  131. var over = true;
  132. if (overwrite===false) over = false;
  133. var o = {};
  134. o[name] = {"value": fun, "configurable": over};
  135. library.defineProperties(bind, o);
  136. };
  137. var Dict = function(name){
  138. var dictionary = _self.dictionary;
  139. this.name = name;
  140. this.get = function(path){
  141. return library.JSONDecode(dictionary.select(this.name, path));
  142. };
  143. this.set = function(path, value){
  144. try {
  145. dictionary.update(this.name, library.JSONEncode(value), path);
  146. return true;
  147. }catch(e){
  148. return false;
  149. }
  150. };
  151. this.add = function(path, value){
  152. try {
  153. dictionary.insert(this.name, library.JSONEncode(value), path);
  154. return true;
  155. }catch(e){
  156. return false;
  157. }
  158. };
  159. };
  160. if ((typeof JSON) == 'undefined'){
  161. JSON = {};
  162. }
  163. JSON.validate = function(string){
  164. string = string.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, '');
  165. return (/^[\],:{}\s]*$/).test(string);
  166. };
  167. JSON.encode = JSON.stringify ? function(obj){
  168. return JSON.stringify(obj);
  169. } : function(obj){
  170. if (obj && obj.toJSON) obj = obj.toJSON();
  171. switch (typeof obj){
  172. case 'string':
  173. return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"';
  174. case 'array':
  175. var string = [];
  176. for (var i=0; i<obj.length; i++){
  177. var json = JSON.encode(obj[i]);
  178. if (json) string.push(json);
  179. }
  180. return '[' + string + ']';
  181. case 'object': case 'hash':
  182. var string = [];
  183. for (key in obj){
  184. var json = JSON.encode(obj[key]);
  185. if (json) string.push(JSON.encode(key) + ':' + json);
  186. }
  187. return '{' + string + '}';
  188. case 'number': case 'boolean': return '' + obj;
  189. case 'null': return 'null';
  190. }
  191. return null;
  192. };
  193. JSON.decode = function(string, secure){
  194. if (!string || (typeof string) !== 'string') return null;
  195. if (secure || JSON.secure){
  196. if (JSON.parse) return JSON.parse(string);
  197. if (!JSON.validate(string)) throw new Error('JSON could not decode the input; security is enabled and the value is not secure.');
  198. }
  199. return eval('(' + string + ')');
  200. };
  201. var body = {
  202. set: function(data){
  203. if ((typeof data)=="string"){
  204. if (jaxrsBody) jaxrsBody.set(data);
  205. }else{
  206. if (jaxrsBody) jaxrsBody.set(JSON.encode(data));
  207. }
  208. }
  209. };
  210. bind.library = library;
  211. bind.data = this.data;
  212. bind.workContext = wrapWorkContext;
  213. bind.service = this.webserviceClient;
  214. bind.org = this.organization;
  215. bind.include = include;
  216. bind.define = define;
  217. bind.Dict = Dict;
  218. bind.form = null;
  219. bind.body = body || null;
  220. bind.parameters = this.parameters || null;
  221. bind.response = (function(){
  222. if (this.jaxrsResponse){
  223. res = this.jaxrsResponse.toString();
  224. if (JSON.validate(res)){
  225. return JSON.decode(res);
  226. }else{
  227. return {"data": res}
  228. }
  229. }
  230. return null;
  231. }).apply(this);