Advertisement
Guest User

Untitled

a guest
Feb 8th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.92 KB | None | 0 0
  1. // client
  2.  
  3. DesktopContainer = createContainer(() => {
  4.  
  5. incomingTable = function() {
  6. if (!Meteor.userId()) {
  7. return false;
  8. }
  9.  
  10. var incoming_objects = _.filter( UserObjects.find({ $and: [{userId:Meteor.userId()},{status:'published'}] }).fetch() || [], function(obj3) {
  11. return typeof obj3.tableId == 'undefined' || obj3.tableId == null;
  12. });
  13.  
  14. _.each(incoming_objects, function(value, key, obj) {
  15. var object2 = Mongo.Collection.get(obj[key].type).find({_id:obj[key].objectId}).fetch()[0];
  16. if (object2) {
  17. obj[key].title = object2.title;
  18. obj[key].url = object2.url;
  19. obj[key].picture = object2.picture;
  20. }
  21. obj[key].objectId = obj[key].objectId;
  22. });
  23.  
  24. incoming_table = {
  25. name: 'Incoming',
  26. _id:'null',
  27. items: incoming_objects
  28. }
  29.  
  30. return incoming_table;
  31. }
  32.  
  33. userTables = function() {
  34. var tableIds = _.pluck(Tables.find({boardId:session.currentBoard.get(),status:'published'}).fetch(),'_id');
  35.  
  36. var sharedTables_ = SharedTables.find({userId: Meteor.userId(),boardId:session.currentBoard.get()}).fetch();
  37.  
  38. var sharedTableIds = _.pluck(sharedTables_,'tableId');
  39. var sharedTables = Tables.find({_id: { '$in': sharedTableIds.concat(tableIds) }, status:'published' }).fetch();
  40.  
  41. _.each(sharedTables, function(table, key) {
  42. sharedTables[key].shared = true;
  43. });
  44.  
  45. console.log('sharedTables',sharedTables);
  46.  
  47. _.each( sharedTables, function(table, key) {
  48.  
  49. var tableObjects = UserObjects.find({tableId:table._id}).fetch();
  50.  
  51. _.each(tableObjects, function(obj, index) {
  52.  
  53. var object1 = Mongo.Collection.get(tableObjects[index].type).find({_id:tableObjects[index].objectId}).fetch()[0];
  54.  
  55. if (object1) {
  56. tableObjects[index].objectId = object1._id;
  57. tableObjects[index].title = object1.title;
  58. tableObjects[index].url = object1.url;
  59. tableObjects[index].picture = object1.picture;
  60. tableObjects[index].alarm = object1.alarm;
  61. }
  62.  
  63. });
  64.  
  65. sharedTables[key].items = _.sortBy(tableObjects, function(o) { return o.tableIndex; });
  66.  
  67. });
  68.  
  69. return sharedTables;
  70. }
  71.  
  72. getMyBoards = function() {
  73. var myBoardIds = _.pluck(Boards.find({userId:Meteor.userId()}).fetch(), '_id');
  74. var sharedBoardIds = _.pluck(SharedBoards.find({userId:Meteor.userId()}).fetch(), 'boardId');
  75. var allBoardIds = myBoardIds.concat(sharedBoardIds);
  76.  
  77. return Boards.find({_id: { $in: allBoardIds }},{sort:{createdAt:1}}).fetch();
  78. }
  79.  
  80. currentBoardName = function() {
  81. var board = Boards.find({_id:session.currentBoard.get()}).fetch()[0];
  82. return typeof board == 'undefined' ? '' : board.name ;
  83. }
  84.  
  85. Meteor.subscribe('sharedTables', Meteor.userId(),{
  86. onReady: function () {
  87. //console.log("sharedTables onReady", arguments);
  88.  
  89. console.log('sharedTables onReady', Tables.find().fetch() );
  90.  
  91. },
  92. onStop: function () {
  93. console.log("sharedTables onError", arguments);
  94. }
  95. });
  96.  
  97. Meteor.subscribe('sharedTablesShared', Meteor.userId(),{
  98. onReady: function () {
  99. //console.log("sharedTables onReady", arguments);
  100.  
  101. console.log('sharedTablesShared onReady', Tables.find().fetch() );
  102.  
  103. },
  104. onStop: function () {
  105. console.log("sharedTablesShared onError", arguments);
  106. }
  107. });
  108.  
  109. var userTablesSub = Meteor.subscribe('userTables',{currentBoard:session.currentBoard.get(),newTables:desktopSession.newTables.get()}, {
  110. onReady: function () {
  111. // console.log("userTables onReady", arguments);
  112.  
  113. var mainBoardId = Boards.find({userId:Meteor.userId()},{sort:{createdAt:1}}).fetch()[0]._id;
  114. desktopSession.mainBoardId.set(mainBoardId);
  115. console.log(mainBoardId);
  116.  
  117. if ( (false == Roles.userIsInRole(Meteor.userId(), 'technical-support', 'briefcase')
  118. && false == Roles.userIsInRole(Meteor.userId(), 'super-admin', 'briefcase'))
  119. || session.currentBoard.get() == null ) {
  120.  
  121. session.currentBoard.set(Boards.find({userId:Meteor.userId()}).fetch()[0]._id)
  122. }
  123.  
  124. },
  125. onStop: function () {
  126. console.log("userTables onError", arguments);
  127. }
  128. });
  129.  
  130. return {
  131. boards: getMyBoards(),
  132. tables: userTables(),
  133. incomingtable: incomingTable(),
  134. deviceType: deviceType,
  135. touchDevice: touchDevice,
  136. desktopSession: desktopSession,
  137. session: session,
  138. currentBoardName: currentBoardName(),
  139. SharedTables: SharedTables.find().fetch()
  140. };
  141.  
  142. }, Desktop);
  143.  
  144. // server
  145. Meteor.publish("sharedTablesShared", function() {
  146. if (!this.userId) {
  147. return;
  148. }
  149. return SharedTables.find({userId: this.userId});
  150. });
  151.  
  152. Meteor.publish("sharedTables", function() {
  153. if (!this.userId) {
  154. return;
  155. }
  156. var sharedTableIds = _.pluck(SharedTables.find({userId: this.userId}).fetch(),'tableId');
  157. var tables = Tables.find({_id: { '$in': sharedTableIds }, status:'published' });
  158.  
  159. return tables;
  160.  
  161. /*
  162. var subscription = this;
  163.  
  164. var userHandle = SharedTables.find({userId: this.userId}).observe({
  165. added: function (obj, fields) {
  166. console.log('ADDED',obj._id);
  167. subscription.added("sharedtables", obj._id, fields);
  168. },
  169. changed: function(obj, fields) {
  170. // subscription.changed("sharedtables", id, fields);
  171. },
  172. removed: function (obj) {
  173. // subscription.removed("sharedtables", id);
  174. }
  175. });
  176.  
  177.  
  178.  
  179. subscription.ready();
  180.  
  181. subscription.onStop(function () {
  182. userHandle.stop();
  183. });
  184. */
  185.  
  186. });
  187.  
  188. Meteor.publish("userTables", function(data) {
  189. if (!this.userId) {
  190. return;
  191. }
  192. if (data.currentBoard != null) {
  193. check(data.currentBoard, String);
  194. }
  195. if (!Roles.userIsInRole(this.userId, 'super-admin', 'briefcase')) {
  196. data.currentBoard = Boards.find({userId:this.userId}).fetch()[0]._id;
  197. }
  198.  
  199. //boards
  200. var myBoardIds = _.pluck(Boards.find({userId:this.userId}).fetch(), '_id');
  201.  
  202. // sharedBoards
  203. var sharedBoardIds = _.pluck(SharedBoards.find({userId:this.userId}).fetch(), 'boardId');
  204.  
  205. var allBoardIds = myBoardIds.concat(sharedBoardIds);
  206. var allBoards = Boards.find({_id: { $in: allBoardIds } });
  207.  
  208. //tables
  209. var tableIds = _.pluck(Tables.find({ $and: [{userId:this.userId},{boardId: { '$in': allBoardIds } },{status:'published'}] }).fetch(),'_id');
  210.  
  211. // new tables
  212. var newTablesIds = _.pluck(Tables.find({ $and: [{_id: { '$in': data.newTables }},{boardId:data.currentBoard},{status:'published'}] }).fetch(),'_id');
  213.  
  214.  
  215.  
  216. var sharedTableIds = _.pluck(SharedTables.find({userId: this.userId}).fetch(),'tableId');
  217.  
  218. var sharedBoardTableIds = _.pluck(Tables.find({boardId: { $in: sharedBoardIds } }).fetch(),'_id');
  219.  
  220. var allTableIds = tableIds.concat(sharedTableIds,newTablesIds,sharedBoardTableIds);
  221. var sharedObjectIds = _.pluck(UserObjects.find({tableId: { '$in': sharedTableIds } }).fetch(),'objectId');
  222.  
  223. var allTables = Tables.find({_id: { '$in': allTableIds },status:'published' });
  224.  
  225. var inComingUserObjects = UserObjects.find({tableId: { $exists: false } });
  226. var userObjects = UserObjects.find({tableId: { '$in': allTableIds }});
  227.  
  228. var inComingUserObjectsIds = _.pluck(inComingUserObjects.fetch(),'objectId');
  229. var userObjectsIds = _.pluck(userObjects.fetch(),'objectId');
  230.  
  231. var allUserObjectsIds = userObjectsIds.concat(sharedObjectIds,inComingUserObjectsIds);
  232.  
  233. return [
  234. allBoards,
  235. allTables,
  236. userObjects,
  237. SharedBoards.find({userId:this.userId}),
  238. Faq.find({_id: { '$in': allUserObjectsIds }}),
  239. Products.find({_id: { '$in': allUserObjectsIds } }),
  240. References.find({_id: { '$in': allUserObjectsIds } }),
  241. News.find({_id: { '$in': allUserObjectsIds } }),
  242. Contact.find({_id: { '$in': allUserObjectsIds } }),
  243. Webinars.find({_id: { '$in': allUserObjectsIds } }),
  244. Custom.find({_id: { '$in': allUserObjectsIds } })
  245. ];
  246. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement