toolbar.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Generated by CoffeeScript 1.6.3
  2. (function () {
  3. !(function (window) {
  4. var Toolbar;
  5. var currUser = {
  6. //use to join avator
  7. AU: ""
  8. };
  9. var isNotifyShowed = false;
  10. var AUtoAvatar = function (AU) {
  11. _AUPath = AU.split("").join("/");
  12. return "http://avatar.csdn.net/" + _AUPath + "/2_" + currUser.userName + ".jpg";
  13. }
  14. var getActive = function () {
  15. return document.activeElement;
  16. }
  17. Toolbar = function (opts) {
  18. this.opts = opts;
  19. this.Doms = {};
  20. this.Curr = null;
  21. this.init.apply(this, [opts]);
  22. };
  23. Toolbar.prototype = {
  24. init: function (opts) {
  25. opts = opts || this.opts;
  26. this.getDoms(opts).bindEvent();
  27. this.initFavor();
  28. this.initNotify();
  29. this.initProfile();
  30. },
  31. /*
  32. * 聚合所有dom抓取
  33. */
  34. getDoms: function (opts) {
  35. opts = opts || this.opts;
  36. this.Doms.wraper = opts.wraper;
  37. this.folded = true;
  38. this.Doms.listBtns = $(opts.wraper + " ul.btns>li");
  39. this.Doms.profile = $(opts.wraper + " .profile");
  40. return this;
  41. },
  42. bindEvent: function (opts) {
  43. var _this = this;
  44. var $loginWrap = $(this.Doms.wraper + " .login-wrap");
  45. opts = opts || this.opts;
  46. this.Doms.listBtns.bind('mouseover', function (event) {
  47. _this.Curr = $(this).parent().children().filter(".on")[0];
  48. if ($(event.target).hasClass("notify") || $(event.target).parents().filter(".notify").length) {
  49. if (!isNotifyShowed || opts.notifyInst.isHasNewMsg()) {
  50. $("#header_notice_num").trigger("click");
  51. isNotifyShowed = true;
  52. }
  53. }
  54. if (_this.Curr == this) {
  55. return;
  56. }
  57. $(_this.Curr).removeClass("on");
  58. $(this).addClass("on");
  59. _this.folded = false;
  60. });
  61. $(this.Doms.wraper).bind('mouseout',function(e){
  62. if($(getActive()).parents().filter(_this.Doms.wraper).length>0){
  63. return;
  64. }
  65. $(document.body).trigger("toolbar-fold");
  66. });
  67. $(document).bind("click", function (e) {
  68. if ($(e.target).parents().filter(_this.Doms.wraper).length <= 0) {
  69. $(document.body).trigger("toolbar-fold");
  70. }
  71. });
  72. $(document.body).bind("toolbar-fold", function (e) {
  73. _this.Doms.listBtns.filter(".on").removeClass("on");
  74. });
  75. $(this.Doms.wraper + " .search form").bind("keyup", function (e) {
  76. if (e.keycode == 13) {
  77. this.submit();
  78. }
  79. });
  80. // $(document).bind("userLogined",function(e,userData){
  81. // $loginWrap.removeClass("unlogin");
  82. // userData.userName&&$loginWrap.show();
  83. // });
  84. // $(document).bind("userLoginOut",function(e,username){
  85. // $loginWrap.addClass("unlogin").removeClass("hide").show();
  86. // });
  87. return this;
  88. },
  89. initLogo: function (opts) {},
  90. initFavor: function () {
  91. var _wraper = this.Doms.wraper + " .favor"
  92. var _title = document.title;
  93. var _url = document.location.href;
  94. var $title = $(_wraper + " .title");
  95. var $url = $(_wraper + " .url");
  96. // var $tag = $(_wraper + " .tag");
  97. // var $description = $(_wraper + " .description");
  98. var $tag = $(_wraper + " #input-tag");
  99. var $description = $(_wraper + " #input-description");
  100. var $share = $(_wraper + " .share");
  101. var $cancel = $(_wraper + " .cancel");
  102. var $submit = $(_wraper + " .submit");
  103. var $ok = $(_wraper + " .ok");
  104. //set value
  105. $title.val(_title);
  106. $url.val(_url);
  107. $share.attr("checked", true);
  108. //bind event
  109. $cancel.bind("click", function (e) {
  110. $(document.body).trigger("toolbar-fold");
  111. });
  112. $ok.bind("click", function (e) {
  113. $(_wraper + " .favor-failed").hide();
  114. $(_wraper + " .favor-success").hide();
  115. $(_wraper + " .favor-form").show();
  116. $(document.body).trigger("toolbar-fold");
  117. });
  118. $submit.bind("click", function (e) {
  119. var _data = {
  120. title: $title.val(),
  121. //url:encodeURIComponent($url.val()),
  122. url: $url.val(),
  123. description: $description.val(),
  124. tag: $tag.val(),
  125. share: $share[0].checked ? 1 : 0
  126. }
  127. function resetForm() {
  128. $(_wraper + " .title").val($title.val());
  129. $(_wraper + " .url").val(window.location.href);
  130. $(_wraper + " .tag").val("");
  131. $(_wraper + " .description").val("");
  132. }
  133. function cbSuccess() {
  134. $(_wraper + " .favor-failed").show();
  135. $(_wraper + " .favor-success").hide();
  136. $(_wraper + " .favor-form").hide();
  137. }
  138. function cbError() {
  139. $(_wraper + " .favor-failed").hide();
  140. $(_wraper + " .favor-success").show();
  141. $(_wraper + " .favor-form").hide();
  142. }
  143. $.ajax({
  144. data: _data,
  145. jsonp: "jsonpcallback",
  146. dataType: "jsonp",
  147. url: "http://my.csdn.net/service/favorite/add_favorite",
  148. success: function (data) {
  149. if (data.success == "-1") {
  150. cbSuccess();
  151. resetForm();
  152. }
  153. if (data.success == "1") {
  154. cbError();
  155. resetForm();
  156. }
  157. }
  158. });
  159. return false;
  160. });
  161. },
  162. initNotify: function (opts) {
  163. var _this = this;
  164. var opts = opts || this.opts;
  165. if (!opts.notifyInst) {
  166. $(opts.wraper + " .notify").hide();
  167. throw "notify.js not loaded!";
  168. }
  169. $(document).bind("toolbar-setNotesNum", function (e, num) {
  170. var $number = $(_this.Doms.wraper + " .number");
  171. var $hasnotes = $(_this.Doms.wraper + " .icon-hasnotes-sm");
  172. if (num <= 0) {
  173. $hasnotes.hide();
  174. return;
  175. } else if (num > 0) {
  176. $hasnotes.show();
  177. }
  178. });
  179. if (opts.notifyInst) {
  180. opts.notifyInst.on("receive_unreads", function (e, data) {
  181. var num = opts.notifyInst.getlocalUnread().length;
  182. $(document).trigger("toolbar-setNotesNum", num);
  183. });
  184. opts.notifyInst.on("receive_setreaded", function (e, data) {
  185. var num = opts.notifyInst.getlocalUnread().length;
  186. $(document).trigger("toolbar-setNotesNum", num);
  187. });
  188. }
  189. },
  190. initProfile: function (userData) {
  191. var _this = this;
  192. }
  193. };
  194. return window["Toolbar"] = Toolbar;
  195. })(window);
  196. var $oScriptTag = $("#csdn-toolbar-id");
  197. var inst = typeof csdn_note == 'undefined' ? null : csdn_note;
  198. new Toolbar({
  199. wraper: ".csdn-toolbar",
  200. notifyInst: inst
  201. });
  202. }).call(this);