Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.92 KB | None | 0 0
  1. <%= f.hidden_field :point %>
  2. <div class="rating-comments"></div>
  3.  
  4. <script type="text/javascript">
  5. $('.rating-comments').raty({
  6. click: function(score, evt) {
  7. $("#point").val(score);
  8. return console.log(score);
  9. }
  10. });
  11. </script>
  12.  
  13. def comment_params
  14. params.require(:comment).permit(:title, :content, :post_id, :user_id, :point)
  15. end
  16.  
  17. validates_numericality_of :point, greater_than_or_equal_to: 0
  18.  
  19. ;(function($) {
  20.  
  21. var methods = {
  22. init: function(settings) {
  23. return this.each(function() {
  24. methods.destroy.call(this);
  25.  
  26. this.opt = $.extend(true, {}, $.fn.raty.defaults, settings);
  27.  
  28. var that = $(this),
  29. inits = ['number', 'readOnly', 'score', 'scoreName'];
  30.  
  31. methods._callback.call(this, inits);
  32.  
  33. if (this.opt.precision) {
  34. methods._adjustPrecision.call(this);
  35. }
  36.  
  37. this.opt.number = methods._between(this.opt.number, 0, this.opt.numberMax);
  38.  
  39. this.opt.path = this.opt.path || '';
  40.  
  41. if (this.opt.path && this.opt.path.charAt( this.opt.path.length - 1 ) !== '/') {
  42. this.opt.path += '/';
  43. }
  44.  
  45. this.stars = methods._createStars.call(this);
  46. this.score = methods._createScore.call(this);
  47.  
  48. methods._apply.call(this, this.opt.score);
  49.  
  50. var space = this.opt.space ? 4 : 0,
  51. width = this.opt.width || (this.opt.number * this.opt.size + this.opt.number * space);
  52.  
  53. if (this.opt.cancel) {
  54. this.cancel = methods._createCancel.call(this);
  55.  
  56. width += (this.opt.size + space);
  57. }
  58.  
  59. if (this.opt.readOnly) {
  60. methods._lock.call(this);
  61. } else {
  62. that.css('cursor', 'pointer');
  63. methods._binds.call(this);
  64. }
  65.  
  66. if (this.opt.width !== false) {
  67. that.css('width', width);
  68. }
  69.  
  70. methods._target.call(this, this.opt.score);
  71.  
  72. that.data({ 'settings': this.opt, 'raty': true });
  73. });
  74. }, _adjustPrecision: function() {
  75. this.opt.targetType = 'score';
  76. this.opt.half = true;
  77. }, _apply: function(score) {
  78. if (score && score > 0) {
  79. score = methods._between(score, 0, this.opt.number);
  80. this.score.val(score);
  81. }
  82.  
  83. methods._fill.call(this, score);
  84.  
  85. if (score) {
  86. methods._roundStars.call(this, score);
  87. }
  88. }, _between: function(value, min, max) {
  89. return Math.min(Math.max(parseFloat(value), min), max);
  90. }, _binds: function() {
  91. if (this.cancel) {
  92. methods._bindCancel.call(this);
  93. }
  94.  
  95. methods._bindClick.call(this);
  96. methods._bindOut.call(this);
  97. methods._bindOver.call(this);
  98. }, _bindCancel: function() {
  99. methods._bindClickCancel.call(this);
  100. methods._bindOutCancel.call(this);
  101. methods._bindOverCancel.call(this);
  102. }, _bindClick: function() {
  103. var self = this,
  104. that = $(self);
  105.  
  106. self.stars.on('click.raty', function(evt) {
  107. self.score.val((self.opt.half || self.opt.precision) ? that.data('score') : this.alt);
  108.  
  109. if (self.opt.click) {
  110. self.opt.click.call(self, parseFloat(self.score.val()), evt);
  111. }
  112. });
  113. }, _bindClickCancel: function() {
  114. var self = this;
  115.  
  116. self.cancel.on('click.raty', function(evt) {
  117. self.score.removeAttr('value');
  118.  
  119. if (self.opt.click) {
  120. self.opt.click.call(self, null, evt);
  121. }
  122. });
  123. }, _bindOut: function() {
  124. var self = this;
  125.  
  126. $(this).on('mouseleave.raty', function(evt) {
  127. var score = parseFloat(self.score.val()) || undefined;
  128.  
  129. methods._apply.call(self, score);
  130. methods._target.call(self, score, evt);
  131.  
  132. if (self.opt.mouseout) {
  133. self.opt.mouseout.call(self, score, evt);
  134. }
  135. });
  136. }, _bindOutCancel: function() {
  137. var self = this;
  138.  
  139. self.cancel.on('mouseleave.raty', function(evt) {
  140. $(this).attr('src', self.opt.path + self.opt.cancelOff);
  141.  
  142. if (self.opt.mouseout) {
  143. self.opt.mouseout.call(self, self.score.val() || null, evt);
  144. }
  145. });
  146. }, _bindOverCancel: function() {
  147. var self = this;
  148.  
  149. self.cancel.on('mouseover.raty', function(evt) {
  150. $(this).attr('src', self.opt.path + self.opt.cancelOn);
  151.  
  152. self.stars.attr('src', self.opt.path + self.opt.starOff);
  153.  
  154. methods._target.call(self, null, evt);
  155.  
  156. if (self.opt.mouseover) {
  157. self.opt.mouseover.call(self, null);
  158. }
  159. });
  160. }, _bindOver: function() {
  161. var self = this,
  162. that = $(self),
  163. action = self.opt.half ? 'mousemove.raty' : 'mouseover.raty';
  164.  
  165. self.stars.on(action, function(evt) {
  166. var score = parseInt(this.alt, 10);
  167.  
  168. if (self.opt.half) {
  169. var position = parseFloat((evt.pageX - $(this).offset().left) / self.opt.size),
  170. plus = (position > .5) ? 1 : .5;
  171.  
  172. score = score - 1 + plus;
  173.  
  174. methods._fill.call(self, score);
  175.  
  176. if (self.opt.precision) {
  177. score = score - plus + position;
  178. }
  179.  
  180. methods._roundStars.call(self, score);
  181.  
  182. that.data('score', score);
  183. } else {
  184. methods._fill.call(self, score);
  185. }
  186.  
  187. methods._target.call(self, score, evt);
  188.  
  189. if (self.opt.mouseover) {
  190. self.opt.mouseover.call(self, score, evt);
  191. }
  192. });
  193. }, _callback: function(options) {
  194. for (var i in options) {
  195. if (typeof this.opt[options[i]] === 'function') {
  196. this.opt[options[i]] = this.opt[options[i]].call(this);
  197. }
  198. }
  199. }, _createCancel: function() {
  200. var that = $(this),
  201. icon = this.opt.path + this.opt.cancelOff,
  202. cancel = $('<img />', { src: icon, alt: 'x', title: this.opt.cancelHint, 'class': 'raty-cancel' });
  203.  
  204. if (this.opt.cancelPlace == 'left') {
  205. that.prepend('&#160;').prepend(cancel);
  206. } else {
  207. that.append('&#160;').append(cancel);
  208. }
  209.  
  210. return cancel;
  211. }, _createScore: function() {
  212. return $('<input />', { type: 'hidden', name: this.opt.scoreName }).appendTo(this);
  213. }, _createStars: function() {
  214. var that = $(this);
  215.  
  216. for (var i = 1; i <= this.opt.number; i++) {
  217. var title = methods._getHint.call(this, i),
  218. icon = (this.opt.score && this.opt.score >= i) ? 'starOn' : 'starOff';
  219.  
  220. icon = this.opt.path + this.opt[icon];
  221.  
  222. $('<img />', { src : icon, alt: i, title: title }).appendTo(this);
  223.  
  224. if (this.opt.space) {
  225. that.append((i < this.opt.number) ? '&#160;' : '');
  226. }
  227. }
  228.  
  229. return that.children('img');
  230. }, _error: function(message) {
  231. $(this).html(message);
  232.  
  233. $.error(message);
  234. }, _fill: function(score) {
  235. var self = this,
  236. hash = 0;
  237.  
  238. for (var i = 1; i <= self.stars.length; i++) {
  239. var star = self.stars.eq(i - 1),
  240. select = self.opt.single ? (i == score) : (i <= score);
  241.  
  242. if (self.opt.iconRange && self.opt.iconRange.length > hash) {
  243. var irange = self.opt.iconRange[hash],
  244. on = irange.on || self.opt.starOn,
  245. off = irange.off || self.opt.starOff,
  246. icon = select ? on : off;
  247.  
  248. if (i <= irange.range) {
  249. star.attr('src', self.opt.path + icon);
  250. }
  251.  
  252. if (i == irange.range) {
  253. hash++;
  254. }
  255. } else {
  256. var icon = select ? 'starOn' : 'starOff';
  257.  
  258. star.attr('src', this.opt.path + this.opt[icon]);
  259. }
  260. }
  261. }, _getHint: function(score) {
  262. var hint = this.opt.hints[score - 1];
  263. return (hint === '') ? '' : (hint || score);
  264. }, _lock: function() {
  265. var score = parseInt(this.score.val(), 10), // TODO: 3.1 >> [['1'], ['2'], ['3', '.1', '.2']]
  266. hint = score ? methods._getHint.call(this, score) : this.opt.noRatedMsg;
  267.  
  268. $(this).data('readonly', true).css('cursor', '').attr('title', hint);
  269.  
  270. this.score.attr('readonly', 'readonly');
  271. this.stars.attr('title', hint);
  272.  
  273. if (this.cancel) {
  274. this.cancel.hide();
  275. }
  276. }, _roundStars: function(score) {
  277. var rest = (score - Math.floor(score)).toFixed(2);
  278.  
  279. if (rest > this.opt.round.down) {
  280. var icon = 'starOn'; // Up: [x.76 .. x.99]
  281.  
  282. if (this.opt.halfShow && rest < this.opt.round.up) { // Half: [x.26 .. x.75]
  283. icon = 'starHalf';
  284. } else if (rest < this.opt.round.full) { // Down: [x.00 .. x.5]
  285. icon = 'starOff';
  286. }
  287.  
  288. this.stars.eq(Math.ceil(score) - 1).attr('src', this.opt.path + this.opt[icon]);
  289. } // Full down: [x.00 .. x.25]
  290. }, _target: function(score, evt) {
  291. if (this.opt.target) {
  292. var target = $(this.opt.target);
  293.  
  294. if (target.length === 0) {
  295. methods._error.call(this, 'Target selector invalid or missing!');
  296. }
  297.  
  298. if (this.opt.targetFormat.indexOf('{score}') < 0) {
  299. methods._error.call(this, 'Template "{score}" missing!');
  300. }
  301.  
  302. var mouseover = evt && evt.type == 'mouseover';
  303.  
  304. if (score === undefined) {
  305. score = this.opt.targetText;
  306. } else if (score === null) {
  307. score = mouseover ? this.opt.cancelHint : this.opt.targetText;
  308. } else {
  309. if (this.opt.targetType == 'hint') {
  310. score = methods._getHint.call(this, Math.ceil(score));
  311. } else if (this.opt.precision) {
  312. score = parseFloat(score).toFixed(1);
  313. }
  314.  
  315. if (!mouseover && !this.opt.targetKeep) {
  316. score = this.opt.targetText;
  317. }
  318. }
  319.  
  320. if (score) {
  321. score = this.opt.targetFormat.toString().replace('{score}', score);
  322. }
  323.  
  324. if (target.is(':input')) {
  325. target.val(score);
  326. } else {
  327. target.html(score);
  328. }
  329. }
  330. }, _unlock: function() {
  331. $(this).data('readonly', false).css('cursor', 'pointer').removeAttr('title');
  332.  
  333. this.score.removeAttr('readonly', 'readonly');
  334.  
  335. for (var i = 0; i < this.opt.number; i++) {
  336. this.stars.eq(i).attr('title', methods._getHint.call(this, i + 1));
  337. }
  338.  
  339. if (this.cancel) {
  340. this.cancel.css('display', '');
  341. }
  342. }, cancel: function(click) {
  343. return this.each(function() {
  344. if ($(this).data('readonly') !== true) {
  345. methods[click ? 'click' : 'score'].call(this, null);
  346. this.score.removeAttr('value');
  347. }
  348. });
  349. }, click: function(score) {
  350. return $(this).each(function() {
  351. if ($(this).data('readonly') !== true) {
  352. methods._apply.call(this, score);
  353.  
  354. if (!this.opt.click) {
  355. methods._error.call(this, 'You must add the "click: function(score, evt) { }" callback.');
  356. }
  357.  
  358.  
  359. this.opt.click.call(this, score, $.Event('click'));
  360.  
  361. methods._target.call(this, score);
  362. }
  363. });
  364. }, destroy: function() {
  365. return $(this).each(function() {
  366. var that = $(this),
  367. raw = that.data('raw');
  368.  
  369. if (raw) {
  370. that.off('.raty').empty().css({ cursor: raw.style.cursor, width: raw.style.width }).removeData('readonly');
  371. } else {
  372. that.data('raw', that.clone()[0]);
  373. }
  374. });
  375. }, getScore: function() {
  376. var score = [],
  377. value ;
  378.  
  379. $(this).each(function() {
  380. value = this.score.val();
  381.  
  382. score.push(value ? parseFloat(value) : undefined);
  383. });
  384.  
  385. return (score.length > 1) ? score : score[0];
  386. }, readOnly: function(readonly) {
  387. return this.each(function() {
  388. var that = $(this);
  389.  
  390. if (that.data('readonly') !== readonly) {
  391. if (readonly) {
  392. that.off('.raty').children('img').off('.raty');
  393.  
  394. methods._lock.call(this);
  395. } else {
  396. methods._binds.call(this);
  397. methods._unlock.call(this);
  398. }
  399.  
  400. that.data('readonly', readonly);
  401. }
  402. });
  403. }, reload: function() {
  404. return methods.set.call(this, {});
  405. }, score: function() {
  406. return arguments.length ? methods.setScore.apply(this, arguments) : methods.getScore.call(this);
  407. }, set: function(settings) {
  408. return this.each(function() {
  409. var that = $(this),
  410. actual = that.data('settings'),
  411. news = $.extend({}, actual, settings);
  412.  
  413. that.raty(news);
  414. });
  415. }, setScore: function(score) {
  416. return $(this).each(function() {
  417. if ($(this).data('readonly') !== true) {
  418. methods._apply.call(this, score);
  419. methods._target.call(this, score);
  420. }
  421. });
  422. }
  423. };
  424.  
  425. $.fn.raty = function(method) {
  426. if (methods[method]) {
  427. return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
  428. } else if (typeof method === 'object' || !method) {
  429. return methods.init.apply(this, arguments);
  430. } else {
  431. $.error('Method ' + method + ' does not exist!');
  432. }
  433. };
  434.  
  435. $.fn.raty.defaults = {
  436. cancel : false,
  437. cancelHint : 'Cancel this rating!',
  438. cancelOff : 'cancel-off.png',
  439. cancelOn : 'cancel-on.png',
  440. cancelPlace : 'left',
  441. click : undefined,
  442. half : false,
  443. halfShow : true,
  444. hints : ['awful', 'bad', 'average', 'good', 'great'],
  445. iconRange : undefined,
  446. mouseout : undefined,
  447. mouseover : undefined,
  448. noRatedMsg : 'Not rated yet!',
  449. number : 5,
  450. numberMax : 20,
  451. path : '/',
  452. precision : false,
  453. readOnly : false,
  454. round : { down: .25, full: .6, up: .76 },
  455. score : undefined,
  456. scoreName : 'score',
  457. single : false,
  458. size : 35,
  459. space : true,
  460. starHalf : 'star-half.PNG',
  461. starOff : 'star-off.PNG',
  462. starOn : 'star-on.PNG',
  463. target : undefined,
  464. targetFormat : '{score}',
  465. targetKeep : false,
  466. targetText : '',
  467. targetType : 'hint',
  468. width : undefined
  469. };
  470.  
  471. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement