29
30:- module(rating,
31 [ rate//1
32 ]). 33:- use_module(library(http/http_path)). 34:- use_module(library(http/http_dispatch), []). 35:- use_module(library(http/html_head)). 36:- use_module(library(http/html_write)). 37:- use_module(library(option)). 38
39:- html_resource(jq('jRating.jquery.js'),
40 [ requires([ jquery,
41 jq('jRating.jquery.css')
42 ])
43 ]).
51rate(Options) -->
52 { option(class(Class), Options, jrating),
53 select_option(data_id(Id), Options, Options1, rating),
54 ( select_option(data_average(Avg), Options1, Options2)
55 -> Extra = ['data-average'(Avg)]
56 ; Extra = [],
57 Options2 = Options
58 )
59 },
60 html_requires(jq('jRating.jquery.js')),
61 html(div([ class(Class), 'data-id'(Id)| Extra], [])),
62 ( { option(post(Post), Options2) }
63 -> html_post(Post, \script(Options2))
64 ; script(Options2)
65 ).
66
67script(Options) -->
68 { option(length(Length), Options, 5),
69 option(rate_max(RateMax), Options, 20),
70 option(step(Step), Options, false),
71 option(type(Type), Options, big),
72 option(class(Class), Options, jrating),
73 option(can_rate_again(CanRateAgain), Options, false),
74 http_absolute_location(jq('icons/stars.png'), BSP, []),
75 http_absolute_location(jq('icons/small.png'), SSP, [])
76 },
77 html(script(type('text/javascript'),
78 [ \[ '$(document).ready(function(){\n',
79 '$(".',Class,'").jRating(\n',
80 ' { bigStarsPath:"',BSP,'",\n',
81 ' smallStarsPath:"',SSP,'",\n',
82 ' step:',Step,',\n',
83 ' type:"',Type,'",\n',
84 ' length:',Length,',\n',
85 ' rateMax:',RateMax,',\n',
86 ' canRateAgain:',CanRateAgain,',\n'
87 ],
88 \set_disabled(Options),
89 \set_action(Options),
90 \set_field(Options),
91 \[ ' });\n',
92 '});\n'
93 ]
94 ])).
95
96set_disabled(Options) -->
97 { option(disabled(true), Options) }, !,
98 html(\[ ' isDisabled:true,\n'
99 ]).
100set_disabled(_) --> [].
101
102set_action(Options) -->
103 { option(on_rating(OnRating), Options) }, !,
104 html(\[ ' phpPath:"',OnRating,'",\n'
105 ]).
106set_action(_) --> [].
107
108set_field(Options) -->
109 { option(set_field(Field), Options) }, !,
110 html(\[ ' onSuccess: function(e,r)\n',
111 ' { $(\'input[name=~w]\').val(r);\n'-[Field],
112 ' }\n'
113 ]).
114set_field(_) --> []
Provide a star-rating widget