ubb.insert.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. (function($) {
  2. $.fn.ncUBB = function(options){
  3. var defaults = {
  4. c_id : 0, // circle id
  5. t_id : 0, // theme id
  6. UBBContent: '', // content object (required)
  7. UBBSubmit : '', // submit button object (required)
  8. UBBform : '', // form object (required)
  9. UBBfileuploadurl : '', // file upload url (required)
  10. UBBcontentleast : 0, // Fill in the contents of the minimal number of characters
  11. run : '' // Need immediate execution of a function, multiple use commas
  12. };
  13. var options = $.extend(defaults, options);
  14. var c_id = options.c_id; var t_id = options.t_id; var UBBContent = options.UBBContent; var UBBSubmit = options.UBBSubmit;
  15. var UBBform = options.UBBform; var UBBfileuploadurl = options.UBBfileuploadurl; var UBBcontentleast = options.UBBcontentleast;
  16. this.each(function() {
  17. if(options.run != '') {eval(options.run);}
  18. var $this = $(this);
  19. // 加粗
  20. $this.find('a[nctype="b"]').click(function(){
  21. UBBContent.inserEitherEndt({
  22. part_one : '[B]',
  23. part_two : '[/B]'
  24. });
  25. });
  26. // 斜体
  27. $this.find('a[nctype="i"]').click(function(){
  28. UBBContent.inserEitherEndt({
  29. part_one : '[I]',
  30. part_two : '[/I]'
  31. });
  32. });
  33. // 下划线
  34. $this.find('a[nctype="u"]').click(function(){
  35. UBBContent.inserEitherEndt({
  36. part_one : '[U]',
  37. part_two : '[/U]'
  38. });
  39. });
  40. // 字体大小颜色
  41. $this.find('.ubb-layer').children().click(function(){
  42. var P='';var C=0;var parent=$(this).parent();
  43. if(parent.hasClass('font-family-layer')){
  44. P = 'FONT';
  45. C = $(this).attr('data-param');
  46. }else if(parent.hasClass('font-size-layer')){
  47. P = 'FONT-SIZE';
  48. C = $(this).attr('class').substring(1,3);
  49. }else if(parent.hasClass('font-color-layer')){
  50. P = 'FONT-COLOR';
  51. C = $(this).attr('class').substring(2,8);
  52. }
  53. if(P == '' || C == 0){
  54. return false;
  55. }
  56. UBBContent.inserEitherEndt({
  57. part_one : '['+P+'='+C+']',
  58. part_two : '[/'+P+']'
  59. });
  60. });
  61. // 表情
  62. $this.find('.smilier-layer').children().click(function(){
  63. var C = $(this).children().attr('data-param');
  64. UBBContent.insert({
  65. text : '[SMILIER='+C+'/]'
  66. });
  67. });
  68. $this.find('.url-handle').toggle(function(){$(this).find('.url-layer').show()},function(){$(this).find('.url-layer').hide()});
  69. // hyperlink
  70. $this.find('.url-layer').find('input[type="submit"]').click(function(){
  71. var C = $this.find('input[name="content"]').val();
  72. var U = $this.find('input[name="url"]').val();
  73. UBBContent.insert({
  74. text : '[URL='+U+']'+C+'[/URL]'
  75. });
  76. $this.find('input[name="content"]').val('');
  77. $this.find('input[name="url"]').val('');
  78. $this.find('.url-handle').click();
  79. });
  80. $this.find('.flash-handle').toggle(function(){$(this).find('.flash-layer').show()},function(){$(this).find('.flash-layer').hide()});
  81. $this.find('.flash-layer').find('input[type="submit"]').click(function(){
  82. var C = $this.find('input[name="flash"]').val();
  83. UBBContent.insert({
  84. text : '[FLASH]'+C+'[/FLASH]'
  85. });
  86. $this.find('input[name="flash"]').val();
  87. $this.find('.flash-handle').click();
  88. });
  89. // 图片上传
  90. $this.find("#test_file").fileupload({
  91. dataType: 'json',
  92. url: UBBfileuploadurl,
  93. add: function (e,data) {
  94. $('div[nctype="affix"]').hide();
  95. $.each(data.files, function (index, file) {
  96. $('<li id=' + file.name.replace(/\./g, '_') + '><p class="loading"></p></li>').appendTo('#scrollbar > ul');
  97. });
  98. data.submit();
  99. },
  100. done: function (e,data) {
  101. var param = data.result;
  102. if(param.msg == 'success'){
  103. updateAffixInsert(param);
  104. }else{
  105. $('#' + param.origin_file_name.replace(/\./g, '_')).remove();
  106. checkInsertAffix();
  107. }
  108. }
  109. });
  110. $this.find('a[nctype="chooseGoods"]').click(function(){
  111. // 已经插入的商品数量计算
  112. var count = 10 - $('.insert-goods > dl').length; // 0 已经插入的商品数量
  113. var _uri = CIRCLE_SITE_URL+'/index.php?act=theme&op=choose_goods&c_id='+c_id+'&count='+count;
  114. CUR_DIALOG = ajax_form("choosegoods", '选择商品', _uri, 510);
  115. });
  116. $this.find('a[nctype="uploadImage"]').click(function(){
  117. var _uri = CIRCLE_SITE_URL+'/index.php?act=theme&op=choose_image&c_id='+c_id;
  118. CUR_DIALOG = ajax_form("uploadimage", '选择图片', _uri, 480);
  119. });
  120. // insert affix
  121. $this.find('a[nctype="affix_insert"]').click(function(){
  122. affixInsert($(this));
  123. });
  124. // delete affix
  125. $this.find('a[nctype="affix_delete"]').click(function(){
  126. affixDelete($(this));
  127. });
  128. // Administrator delete appendage affix
  129. $this.find('a[nctype="maffix_delete"]').click(function(){
  130. affixDeleteManage($(this));
  131. });
  132. // delete goods
  133. $this.find('.goods-del').click(function(){
  134. $(this).parent().remove();
  135. checkInsertGoods();
  136. });
  137. // 相册图片插入到话题
  138. $('a[nctype="imagealbum"]').die().live('click',function(){
  139. var data_str = $('.choose-image').find('a[nctype="chooseimage"][class="selected"]').attr('data-param'); eval('data_str = ' + data_str);
  140. if(typeof(data_str) == 'undefined') {return false;}
  141. insertImgUBB(data_str.img);
  142. DialogManager.close('uploadimage');
  143. });
  144. // 链接地址插入话题
  145. $('a[nctype="imageurl"]').die().live('click',function(){
  146. var img = $('input[nctype="imageurl"]').val();
  147. insertImgUBB(img);
  148. DialogManager.close('uploadimage');
  149. });
  150. // 插入到主题
  151. $('a[nctype="insertGoods"]').die().live('click',function(){
  152. insertGoods($('.selected-goods > dd'));
  153. DialogManager.close('choosegoods');
  154. });
  155. // 提交表单
  156. UBBSubmit.click(function(){
  157. UBBform.submit();
  158. });
  159. // Senior reply
  160. $this.find('a[nctype="highReply"]').click(function(){
  161. _uri = 'index.php?act=theme&op=reply&c_id='+c_id+'&t_id='+t_id;
  162. window.location.href = _uri;
  163. });
  164. /* group */
  165. // 点击发话题
  166. $('.thread-layer').find('p').click(function(){
  167. getUnusedAffix();
  168. $(this).parents('.thread-layer:first').fadeOut('fast',function(){
  169. $('.theme-editor').fadeIn('slow');
  170. });
  171. });
  172. });
  173. function insertImgUBB(C){
  174. UBBContent.insert({
  175. text : '[IMG]'+C+'[/IMG]'
  176. });
  177. }
  178. //插入商品
  179. function insertGoods(o){
  180. o.each(function(){
  181. var data_str = $(this).attr('data-param'); eval( "data_str = " + data_str);
  182. var key = 'k'+$('.insert-goods').find('dl').length;
  183. $('<dl></dl>').append('<dt class="goods-name">'+data_str.name+'</dt><dd class="goods-pic"><a href="javascript:void(0);"><img src="'+data_str.img+'" /></a></dd><dd class="goods-price"><em>'+data_str.price+'</em></dd><dd class="goods-del">删除</dd>')
  184. .append('<input type="hidden" name="goods['+key+'][id]" value="'+data_str.id+'" /><input type="hidden" name="goods['+key+'][name]" value="'+data_str.name+'" /><input type="hidden" name="goods['+key+'][price]" value="'+data_str.price+'" /><input type="hidden" name="goods['+key+'][image]" value="'+data_str.image+'" />')
  185. .append('<input type="hidden" name="goods['+key+'][storeid]" value="'+data_str.storeid+'" /><input type="hidden" name="goods['+key+'][type]" value="'+data_str.type+'" /><input type="hidden" name="goods['+key+'][uri]" value="'+data_str.uri+'" />')
  186. .appendTo('.insert-goods').find('.goods-del').click(function(){
  187. $(this).parent().remove();
  188. checkInsertGoods();
  189. });
  190. });
  191. checkInsertGoods();
  192. }
  193. // 验证已插入商品数量,决定$('div[class="insert-goods"]')是否显示
  194. function checkInsertGoods(){
  195. var igs = $('.quick-thread').find('.insert-goods');
  196. var len = igs.children('dl').length;
  197. if(len > 0){
  198. igs.fadeIn('slow');
  199. }else{
  200. igs.fadeOut('slow');
  201. }
  202. }
  203. // 验证已插入附件数量,决定$('div[nctype="affix"]')是否显示
  204. function checkInsertAffix(){
  205. var len = $('div[class="affix"]').find('li').length;
  206. if(len == 0){
  207. $('div[nctype="affix"]').show();
  208. }
  209. }
  210. // 附件插图到编辑器
  211. function affixInsert(o){
  212. var C = o.attr('data-param');
  213. insertImgUBB(C);
  214. }
  215. // 删除附件
  216. function affixDelete(o){
  217. // 删除图片
  218. var id = o.attr('data-param');
  219. $.getJSON(CIRCLE_SITE_URL+'/index.php?act=theme&op=delimg&c_id='+c_id+'&id='+id, function(){
  220. o.parents('li:first').remove();
  221. checkInsertAffix();
  222. });
  223. }
  224. // Administrator delete appendage affix
  225. function affixDeleteManage(o){
  226. // 删除图片
  227. var id = o.attr('data-param');
  228. $.getJSON(CIRCLE_SITE_URL+'/index.php?act=manage&op=delimg&c_id='+c_id+'&id='+id, function(){
  229. o.parents('li:first').remove();
  230. checkInsertAffix();
  231. });
  232. }
  233. // 附件上传插入html中
  234. function updateAffixInsert(param){
  235. $('#' + param.origin_file_name.replace(/\./g, '_')).removeAttr('id').html('').append('<p><img src="'+param.file_url+'" /></p>')
  236. .append('<div class="handle"><a href="javascript:void(0);" nctype="affix_insert" data-param="'+param.file_insert+'"><i class="c"></i>插入</a><a href="javascript:void(0);" nctype="affix_delete" data-param="'+param.file_id+'"><i class="d"></i>删除</a></div>')
  237. .find('a[nctype="affix_insert"]').click(function(){
  238. affixInsert($(this));
  239. }).end().find('a[nctype="affix_delete"]').click(function(){
  240. affixDelete($(this));
  241. });
  242. }
  243. // get unused affixes
  244. function getUnusedAffix(){
  245. var len = $('.affix').find('li').length;
  246. if(len == 0){
  247. $.getJSON('index.php?act=theme&op=unused_img&c_id='+c_id, function(data){ // 获取未使用附件
  248. if(data != null){
  249. $('div[nctype="affix"]').hide();
  250. $.each(data, function(i, param){
  251. $('<li></li>').append('<p><img src="'+param.file_url+'" /></p>')
  252. .append('<div class="handle"><a href="javascript:void(0);" nctype="affix_insert" data-param="'+param.file_insert+'"><i class="c"></i>插入</a><a href="javascript:void(0);" nctype="affix_delete" data-param="'+param.file_id+'"><i class="d"></i>删除</a></div>')
  253. .appendTo('#scrollbar > ul')
  254. .find('a[nctype="affix_insert"]').click(function(){
  255. affixInsert($(this));
  256. }).end().find('a[nctype="affix_delete"]').click(function(){
  257. affixDelete($(this));
  258. });
  259. });
  260. }
  261. });
  262. }
  263. }
  264. }
  265. })(jQuery);