inettuts.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Script from NETTUTS.com [by James Padolsey]
  3. * @requires jQuery($), jQuery UI & sortable/draggable UI modules
  4. */
  5. var iNettuts = {
  6. jQuery : $,
  7. settings : {
  8. columns : '.column',
  9. widgetSelector: '.widget',
  10. handleSelector: '.widget-head',
  11. contentSelector: '.widget-content',
  12. widgetDefault : {
  13. movable: true,
  14. removable: true,
  15. collapsible: true,
  16. editable: false,
  17. colorClasses : ['color-yellow', 'color-red', 'color-blue', 'color-white', 'color-orange', 'color-green']
  18. },
  19. widgetIndividual : {
  20. intro : {
  21. movable: true,
  22. removable: true,
  23. collapsible: true,
  24. editable: false
  25. }
  26. }
  27. },
  28. init : function () {
  29. this.attachStylesheet('inettuts.js.css');
  30. this.addWidgetControls();
  31. this.makeSortable();
  32. },
  33. getWidgetSettings : function (id) {
  34. var $ = this.jQuery,
  35. settings = this.settings;
  36. return (id&&settings.widgetIndividual[id]) ? $.extend({},settings.widgetDefault,settings.widgetIndividual[id]) : settings.widgetDefault;
  37. },
  38. addWidgetControls : function () {
  39. var iNettuts = this,
  40. $ = this.jQuery,
  41. settings = this.settings;
  42. $(settings.widgetSelector, $(settings.columns)).each(function () {
  43. var thisWidgetSettings = iNettuts.getWidgetSettings(this.id);
  44. if (thisWidgetSettings.removable) {
  45. $('<a href="#" class="remove">CLOSE</a>').mousedown(function (e) {
  46. e.stopPropagation();
  47. }).click(function () {
  48. if(confirm('È·¶¨¹Ø±Õ¸ÃÄ£¿é?')) {
  49. $(this).parents(settings.widgetSelector).animate({
  50. opacity: 0
  51. },function () {
  52. $(this).wrap('<div/>').parent().slideUp(function () {
  53. $(this).remove();
  54. });
  55. });
  56. }
  57. return false;
  58. }).appendTo($(settings.handleSelector, this));
  59. }
  60. if (thisWidgetSettings.editable) {
  61. $('<a href="#" class="edit">EDIT</a>').mousedown(function (e) {
  62. e.stopPropagation();
  63. }).toggle(function () {
  64. $(this).css({backgroundPosition: '-66px 0', width: '55px'})
  65. .parents(settings.widgetSelector)
  66. .find('.edit-box').show().find('input').focus();
  67. return false;
  68. },function () {
  69. $(this).css({backgroundPosition: '', width: ''})
  70. .parents(settings.widgetSelector)
  71. .find('.edit-box').hide();
  72. return false;
  73. }).appendTo($(settings.handleSelector,this));
  74. $('<div class="edit-box" style="display:none;"/>')
  75. .append('<ul><li class="item"><label>Change the title?</label><input value="' + $('h3',this).text() + '"/></li>')
  76. .append((function(){
  77. var colorList = '<li class="item"><label>Available colors:</label><ul class="colors">';
  78. $(thisWidgetSettings.colorClasses).each(function () {
  79. colorList += '<li class="' + this + '"/>';
  80. });
  81. return colorList + '</ul>';
  82. })())
  83. .append('</ul>')
  84. .insertAfter($(settings.handleSelector,this));
  85. }
  86. if (thisWidgetSettings.collapsible) {
  87. $('<a href="#" class="collapse">COLLAPSE</a>').mousedown(function (e) {
  88. e.stopPropagation();
  89. }).toggle(function () {
  90. $(this).css({backgroundPosition: '-38px 0'})
  91. .parents(settings.widgetSelector)
  92. .find(settings.contentSelector).hide();
  93. return false;
  94. },function () {
  95. $(this).css({backgroundPosition: '-52px 0'})
  96. .parents(settings.widgetSelector)
  97. .find(settings.contentSelector).show();
  98. return false;
  99. }).prependTo($(settings.handleSelector,this));
  100. }
  101. });
  102. $('.edit-box').each(function () {
  103. $('input',this).keyup(function () {
  104. $(this).parents(settings.widgetSelector).find('h3').text( $(this).val().length>20 ? $(this).val().substr(0,20)+'...' : $(this).val() );
  105. });
  106. $('ul.colors li',this).click(function () {
  107. var colorStylePattern = /\bcolor-[\w]{1,}\b/,
  108. thisWidgetColorClass = $(this).parents(settings.widgetSelector).attr('class').match(colorStylePattern)
  109. if (thisWidgetColorClass) {
  110. $(this).parents(settings.widgetSelector)
  111. .removeClass(thisWidgetColorClass[0])
  112. .addClass($(this).attr('class').match(colorStylePattern)[0]);
  113. }
  114. return false;
  115. });
  116. });
  117. },
  118. attachStylesheet : function (href) {
  119. var $ = this.jQuery;
  120. return $('<link href="' + href + '" rel="stylesheet" type="text/css" />').appendTo('head');
  121. },
  122. makeSortable : function () {
  123. var iNettuts = this,
  124. $ = this.jQuery,
  125. settings = this.settings,
  126. $sortableItems = (function () {
  127. var notSortable = '';
  128. $(settings.widgetSelector,$(settings.columns)).each(function (i) {
  129. if (!iNettuts.getWidgetSettings(this.id).movable) {
  130. if(!this.id) {
  131. this.id = 'widget-no-id-' + i;
  132. }
  133. notSortable += '#' + this.id + ',';
  134. }
  135. });
  136. return $('> li:not(' + notSortable + ')', settings.columns);
  137. })();
  138. $sortableItems.find(settings.handleSelector).css({
  139. cursor: 'move'
  140. }).mousedown(function (e) {
  141. $sortableItems.css({width:''});
  142. $(this).parent().css({
  143. width: $(this).parent().width() + 'px'
  144. });
  145. }).mouseup(function () {
  146. if(!$(this).parent().hasClass('dragging')) {
  147. $(this).parent().css({width:''});
  148. } else {
  149. $(settings.columns).sortable('disable');
  150. }
  151. });
  152. $(settings.columns).sortable({
  153. items: $sortableItems,
  154. connectWith: $(settings.columns),
  155. handle: settings.handleSelector,
  156. placeholder: 'widget-placeholder',
  157. forcePlaceholderSize: true,
  158. revert: 300,
  159. delay: 100,
  160. opacity: 0.8,
  161. containment: 'document',
  162. start: function (e,ui) {
  163. $(ui.helper).addClass('dragging');
  164. },
  165. stop: function (e,ui) {
  166. $(ui.item).css({width:''}).removeClass('dragging');
  167. $(settings.columns).sortable('enable');
  168. }
  169. });
  170. }
  171. };
  172. iNettuts.init();