not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
8.5 KiB

  1. // Listview view
  2. enyo.kind({
  3. name: "Sugar.DesktopListView",
  4. kind: "Scroller",
  5. published: { activities: [] },
  6. components: [
  7. {name: "nomatch", classes: "listview-nomatch", showing: false},
  8. {name: "message", classes: "listview-message", showing: false},
  9. {name: "nofilter", kind: "Sugar.IconButton", icon: {directory: "icons", icon: "dialog-cancel.svg"}, classes: "listview-button", ontap: "nofilter", showing: false},
  10. {name: "activityPopup", kind: "Sugar.Popup", showing: false},
  11. {name: "activityList", classes: "activity-list", kind: "Repeater", onSetupItem: "setupItem", onresize: "resize", components: [
  12. {name: "item", classes: "activity-list-item", components: [
  13. {name: "favorite", kind: "Sugar.Icon", x: 0, y: 4, size: constant.iconSizeLargeFavorite, ontap: "doSwitchFavorite"},
  14. {name: "activity", kind: "Sugar.Icon", x: 60, y: 5, size: constant.iconSizeList, ontap:"doRunNewActivity"},
  15. {name: "name", classes: "activity-name"},
  16. {name: "version", classes: "activity-version"},
  17. {name: "help", kind: "Sugar.Icon", classes: "activity-help", x: 800, y: 4, showing: false, size: constant.iconSizeList, ontap:"doHelp"}
  18. ]}
  19. ]}
  20. ],
  21. handlers: {
  22. onScroll: "onscroll"
  23. },
  24. // Constructor: init list
  25. create: function() {
  26. this.inherited(arguments);
  27. this.realLength = 0;
  28. this.favoriteActivityButton = null;
  29. if (!window.sugarizerOS) {
  30. this.activitiesChanged();
  31. this.computeSize();
  32. this.draw();
  33. } else {
  34. var t = this;
  35. var a = arguments;
  36. sugarizerOS.initActivitiesPreferences(function (){
  37. t.activitiesChanged();
  38. t.computeSize();
  39. t.draw();
  40. });
  41. }
  42. },
  43. localize: function() {
  44. this.activitiesChanged();
  45. },
  46. computeSize: function() {
  47. var toolbar = document.getElementById("toolbar");
  48. var canvas = document.getElementById("canvas");
  49. var canvas_height = canvas.offsetHeight;
  50. this.applyStyle("height", canvas_height+"px");
  51. },
  52. resize: function() {
  53. this.computeSize();
  54. app.resize();
  55. },
  56. // Initialize information for tutorial
  57. beforeHelp: function() {
  58. tutorial.setElement("owner", app.$.owner.getAttribute("id"));
  59. tutorial.setElement("journal", app.$.journal.getAttribute("id"));
  60. if (this.favoriteActivityButton) {
  61. tutorial.setElement("favoriteitembutton", this.favoriteActivityButton.getAttribute("id"));
  62. }
  63. },
  64. // Get linked toolbar, same than the desktop view
  65. getToolbar: function() {
  66. return app.getToolbar();
  67. },
  68. // Draw screen
  69. draw: function() {
  70. // Set no matching activities
  71. var canvas_center = util.getCanvasCenter();
  72. this.$.nomatch.applyStyle("margin-left", (canvas_center.x-constant.sizeEmpty/4)+"px");
  73. var margintop = (canvas_center.y-constant.sizeEmpty/4);
  74. this.$.nomatch.applyStyle("margin-top", margintop+"px");
  75. this.$.message.setContent(l10n.get("NoMatchingActivities"));
  76. this.$.nofilter.setText(l10n.get("ClearSearch"));
  77. },
  78. // Property changed
  79. activitiesChanged: function() {
  80. var noFilter = app.getToolbar().getSearchText().length == 0;
  81. var length = (noFilter && this.activities.length > constant.listInitCount) ? constant.listInitCount : this.activities.length;
  82. this.realLength = this.activities.length;
  83. this.$.activityList.set("count", length, true);
  84. if (this.activities.length == 0) {
  85. this.$.nomatch.show();
  86. this.$.message.show();
  87. this.$.nofilter.show();
  88. } else {
  89. this.$.nomatch.hide();
  90. this.$.message.hide();
  91. this.$.nofilter.hide();
  92. }
  93. },
  94. // Init setup for a line
  95. setupItem: function(inSender, inEvent) {
  96. // Set item in the template
  97. var activitiesList = sorted(this.activities);
  98. if (activitiesList[inEvent.index].type !== 'undefined' && activitiesList[inEvent.index].type == "native") {
  99. activitiesList[inEvent.index].isNative = true;
  100. }
  101. if (inEvent.index == 0) {
  102. this.favoriteActivityButton = inEvent.item.$.favorite;
  103. }
  104. inEvent.item.$.activity.setIcon(activitiesList[inEvent.index]);
  105. inEvent.item.$.activity.setPopupShow(enyo.bind(this, "showActivityPopup"));
  106. inEvent.item.$.activity.setPopupHide(enyo.bind(this, "hideActivityPopup"));
  107. inEvent.item.$.favorite.setIcon({directory: "icons", icon: "emblem-favorite-large.svg"});
  108. inEvent.item.$.favorite.setColorized(activitiesList[inEvent.index].favorite);
  109. inEvent.item.$.name.setContent(activitiesList[inEvent.index].name);
  110. inEvent.item.$.version.setContent(l10n.get("VersionNumber", {number:activitiesList[inEvent.index].version}));
  111. inEvent.item.$.help.setIcon({directory: "icons", icon: "help-rev.svg"});
  112. inEvent.item.$.help.setColorized(false);
  113. inEvent.item.$.help.setShowing(!activitiesList[inEvent.index].isNative);
  114. if (l10n.language.direction == "rtl") {
  115. inEvent.item.$.name.addClass("rtl-14");
  116. inEvent.item.$.version.addClass("rtl-14");
  117. }
  118. },
  119. // Handle scroll to lazy display content
  120. onscroll: function(inSender, inEvent) {
  121. var scrollBounds = inEvent.scrollBounds;
  122. var currentCount = this.$.activityList.get("count");
  123. if (app.getToolbar().getSearchText().length == 0 && scrollBounds && (scrollBounds.maxTop - scrollBounds.top) < constant.listScrollLimit && this.realLength > currentCount) {
  124. var length = Math.min(currentCount + constant.listStepCount, this.activities.length);
  125. humane.log(l10n.get("Loading"));
  126. this.$.activityList.set("count", length, true);
  127. }
  128. },
  129. // Switch favorite value for clicked line
  130. doSwitchFavorite: function(inSender, inEvent) {
  131. var activitiesList = sorted(this.activities);
  132. this.switchFavorite(inEvent.dispatchTarget.container, activitiesList[inEvent.index]);
  133. },
  134. switchFavorite: function(favorite, activity) {
  135. stats.trace(constant.viewNames[app.getView()], 'switch_favorite', activity.id, null);
  136. util.vibrate();
  137. favorite.setColorized(preferences.switchFavoriteActivity(activity));
  138. favorite.container.render();
  139. preferences.save();
  140. preferences.saveToServer(myserver);
  141. this.getToolbar().askRedraw();
  142. this.$.activityPopup.hidePopup();
  143. },
  144. // Run new activity
  145. doRunNewActivity: function(inSender, inEvent) {
  146. var activitiesList = sorted(this.activities);
  147. this.runNewActivity(activitiesList[inEvent.index])
  148. },
  149. runNewActivity: function(activity) {
  150. // Start a new activity instance
  151. util.vibrate();
  152. this.$.activityPopup.hidePopup();
  153. preferences.runActivity(activity, null);
  154. },
  155. // Popup menu handling
  156. showActivityPopup: function(icon) {
  157. // Create popup
  158. var activity = icon.icon;
  159. this.$.activityPopup.setHeader({
  160. icon: activity,
  161. colorized: true,
  162. name: activity.name,
  163. title: null,
  164. action: null
  165. });
  166. var items = [];
  167. items.push({
  168. icon: icon.parent.container.$.favorite.icon,
  169. colorized: !activity.favorite,
  170. name: activity.favorite ? l10n.get("RemoveFavorite") : l10n.get("MakeFavorite"),
  171. action: enyo.bind(this, "switchFavorite"),
  172. data: [icon.parent.container.$.favorite, icon.icon]
  173. });
  174. items.push({
  175. icon: activity,
  176. colorized: false,
  177. name: l10n.get("StartNew"),
  178. action: enyo.bind(this, "runNewActivity"),
  179. data: [activity, null]
  180. });
  181. this.$.activityPopup.setFooter(items);
  182. // Show popup
  183. this.$.activityPopup.setMargin({left: 0, top: (icon.owner.index*60)+20-mouse.position.y});
  184. this.$.activityPopup.showPopup();
  185. },
  186. hideActivityPopup: function(icon) {
  187. // Hide popup
  188. if (!this.$.activityPopup) {
  189. return true;
  190. }
  191. if (this.$.activityPopup.cursorIsInside() || icon.cursorIsInside()) {
  192. return false;
  193. }
  194. this.$.activityPopup.hidePopup();
  195. return true;
  196. },
  197. // Remove filter
  198. nofilter: function() {
  199. app.getToolbar().setSearchText("");
  200. app.filterActivities();
  201. },
  202. // Display help popup
  203. doHelp: function(inSender, inEvent) {
  204. var activitiesList = sorted(this.activities);
  205. var activities = [];
  206. for (var i = 0 ; i < activitiesList.length ; i++) {
  207. var activity = activitiesList[i];
  208. if (activity.isNative || activity.type == "native") {
  209. continue;
  210. }
  211. var title = l10n.get('NameActivity', {name: activity.name});
  212. var icon = {icon: activity.icon, directory: activity.directory, size: constant.sizeEmpty};
  213. var description = l10n.get('TutoActivity'+activity.directory.substr(activity.directory.indexOf("/")+1).replace('.',''));
  214. if (i == inEvent.index) {
  215. tutorial.setElement("step", activities.length);
  216. }
  217. activities.push({title: title, icon: icon, description: description, step: i});
  218. }
  219. tutorial.setElement("activities", activities);
  220. stats.trace(constant.viewNames[app.getView()], 'tutorial', 'start', null);
  221. tutorial.start();
  222. }
  223. });
  224. // Sort activities
  225. function sorted(activities) {
  226. var result = [];
  227. for (var i in activities) {
  228. result.push(activities[i]);
  229. }
  230. result.sort(function (a, b) {
  231. return a.name == b.name ? 0 : (a.name > b.name ? 1 : -1);
  232. });
  233. return result;
  234. }