Ticket #3599: trac3599_sage_1.patch

File trac3599_sage_1.patch, 9.5 KB (added by itolkov, 13 years ago)
  • sage/server/notebook/interact.py

    # HG changeset patch
    # User Igor Tolkov <itolkov@gmail.com>
    # Date 1215475635 25200
    # Node ID 2e16e235d7fb40668b814bc45843b144214c31ac
    # Parent  aa99aecac97e583ecb30adf9c8bff77b5cb0dfd6
    Longer slider and label.
    
    diff -r aa99aecac97e -r 2e16e235d7fb sage/server/notebook/interact.py
    a b def html(s): 
    208208    """
    209209    print "<html>%s</html>"%s
    210210
    211 def html_slider(id, callback, steps, default=0, margin=0):
     211def html_slider(id, values, callback, steps, default=0, margin=0):
    212212    """
    213213    Return the HTML representation of a jQuery slider.
    214214
    215215    INPUT:
    216216        id      -- string -- the DOM id of the slider (better be unique)
     217        values  -- 'null' or javascript string containing array of values on slider
    217218        callback-- javascript that is executed whenever the slider is done moving
    218219        steps   -- number of steps from minimum to maximum value.
    219220        default -- (default: 0) the default position of the slider
    def html_slider(id, callback, steps, def 
    224225    you should obtain a slider that when moved pops up a window showing its
    225226    current position.
    226227        sage: from sage.server.notebook.interact import html_slider, html
    227         sage: html(html_slider('slider-007', 'alert(position)', steps=5, default=2, margin=5))
     228        sage: html(html_slider('slider-007', 'null', 'alert(position)', steps=5, default=2, margin=5))
    228229        <html>...</html>
    229230    """
    230     s = """<div id='%s' class='ui-slider-1' style='padding:0px;margin:%spx;'><span class='ui-slider-handle'></span></div>"""%(
    231         id, int(margin))
     231    s = """<table><tr><td>
     232        <div id='%s' class='ui-slider-3' style='padding:0px;margin:%spx;'><span class='ui-slider-handle'></span></div>
     233        </td>"""%(id, int(margin))
     234    if values != "null":
     235        s += "<td><font color='black' id='%s-lbl'></font></td>"%id
     236    s += "</tr></table>"
    232237
    233238    # We now generate javascript that gets run after the above div
    234239    # gets inserted. This happens because of the setTimeout function
    235240    # below which gets passed an anonymous function.
    236     s += """<script>setTimeout(function() { $('#%s').slider({
    237     stepping: 1, minValue: 0, maxValue: %s, startValue: %s,
    238     change: function () { var position = Math.ceil($('#%s').slider('value')); %s; }
    239 });}, 1);</script>"""%(id, steps-1, default, id, callback)
     241    s += """<script>(function(){ var values = %s; setTimeout(function() {
     242    $('#%s').slider({
     243        stepping: 1, minValue: 0, maxValue: %s, startValue: %s,
     244        change: function () { var position = Math.ceil($('#%s').slider('value')); if(values!=null) $('#%s-lbl').text(values[position]); %s; },
     245        slide: function() { if(values!=null) $('#%s-lbl').text(values[Math.ceil($('#%s').slider('value'))]); }
     246    });
     247    if(values != null) $('#%s-lbl').text(values[Math.ceil($('#%s').slider('value'))]);
     248    }, 1); })();</script>"""%(values, id, steps-1, default, id, id, callback, id, id, id, id)
    240249    # change 'change' to 'slide' and it changes the slider every time it moves;
    241250    # needs much more work to actually work, since server gets fludded by
    242251    # requests.
    class Selector(InteractControl): 
    886895
    887896   
    888897class Slider(InteractControl):
    889     def __init__(self, var, values, default_position, label=None):
     898    def __init__(self, var, values, default_position, label=None, display_value=True):
    890899        """
    891900        A slider interact control that takes on the given list of
    892901        values.
    class Slider(InteractControl): 
    897906            default_position -- int; default location that the slider is set to.
    898907            label -- alternative label to the left of the slider,
    899908                     instead of the variable.
     909            display_value -- boolean, whether to display the current value right
     910                             of the slider
    900911
    901912        EXAMPLES:
    902913            sage: sage.server.notebook.interact.Slider('x', [1..5], 2, 'alpha')
    class Slider(InteractControl): 
    905916        InteractControl.__init__(self, var, values[default_position], label=label)
    906917        self.__values = values
    907918        self.__default_position = default_position
     919        self.__display_value = display_value
    908920
    909921    def __repr__(self):
    910922        """
    class Slider(InteractControl): 
    941953            'position'
    942954        """
    943955        return "position"
    944    
     956
    945957    def _adaptor(self, position, globs):
    946958        """
    947959        Adapt a user input, which is the slider position, to be an
    class Slider(InteractControl): 
    972984
    973985        EXAMPLES:
    974986            sage: sage.server.notebook.interact.Slider('x', [1..5], 2, 'alpha').render()
    975             '<div ...'
     987            '<table>...<div ...var values = ["1","2","3","4","5"];...'
     988
     989            sage: sage.server.notebook.interact.Slider('x', [1..5], 2, 'alpha', display_value=False).render()
     990            '<table>...<div ...var values = null;...'
    976991        """
     992        if self.__display_value == False:
     993            s = "null"
     994        else:
     995            s = "["
     996            for i in self.__values:
     997                ie = str(i).replace("\\","\\\\").replace("\"","\\\"").replace("'","\\'")
     998                s += "\"%s\","%ie
     999            s = s[:-1] + ']'
     1000       
    9771001        return html_slider('slider-%s-%s'%(self.var(), self.cell_id()),
    978                            self.interact(), steps=len(self.__values),
     1002                           s, self.interact(), steps=len(self.__values),
    9791003                           default=self.default_position())
    9801004   
    9811005
    class checkbox(input_box): 
    16791703   
    16801704   
    16811705class slider(control):
    1682     def __init__(self, vmin, vmax=None, step_size=1, default=None, label=None):
     1706    def __init__(self, vmin, vmax=None, step_size=1, default=None, label=None, display_value=True):
    16831707        r"""
    16841708        An interactive slider control, which can be used in conjunction
    16851709        with the interact command.
    class slider(control): 
    16941718            default -- object or None; default value is ``closest'' in vmin or range
    16951719                       to this default.
    16961720            label -- string
     1721            display_value -- boolean, whether to display the current value to the right
     1722                             of the slider
    16971723
    16981724        EXAMPLES:
    16991725        We specify both vmin and vmax.  We make the default 3, but
    class slider(control): 
    17111737            Slider: alpha [1--|x|---2/3]           
    17121738        """
    17131739        control.__init__(self, label=label)
     1740        self.__display_value = display_value
    17141741        if isinstance(vmin, list):
    17151742            self.__values = vmin
    17161743        else:
    class slider(control): 
    18111838            sage: slider(2, 5, 2/7, 3, 'alpha').render('x')
    18121839            Slider Interact Control: alpha [2--|20/7|---5]
    18131840        """
    1814         return Slider(var, self.__values, self.__default, label=self.label())
     1841        return Slider(var, self.__values, self.__default, label=self.label(), display_value=self.__display_value)
    18151842       
    18161843class selector(control):
    18171844    def __init__(self, values, label=None, default=None,
    def automatic_control(default): 
    19732000        Interact input box labeled None with default value 15
    19742001        sage: sage.server.notebook.interact.automatic_control(('start', 15))
    19752002        Interact input box labeled 'start' with default value 15
    1976         sage: sage.server.notebook.interact.automatic_control((1,100))
    1977         Slider: None [1.0--|1.0|---100.0]
    1978         sage: sage.server.notebook.interact.automatic_control(('alpha', (1,100)))
    1979         Slider: alpha [1.0--|1.0|---100.0]
    1980         sage: sage.server.notebook.interact.automatic_control((2,(1,100)))
    1981         Slider: None [1.0--|2.0|---100.0]
    1982         sage: sage.server.notebook.interact.automatic_control(('alpha label', (2,(1,100))))
    1983         Slider: alpha label [1.0--|2.0|---100.0]
    1984         sage: sage.server.notebook.interact.automatic_control((2, ('alpha label',(1,100))))
    1985         Slider: alpha label [1.0--|2.0|---100.0]
     2003        sage: sage.server.notebook.interact.automatic_control((1,250))
     2004        Slider: None [1.0--|1.0|---250.0]
     2005        sage: sage.server.notebook.interact.automatic_control(('alpha', (1,250)))
     2006        Slider: alpha [1.0--|1.0|---250.0]
     2007        sage: sage.server.notebook.interact.automatic_control((2,(1,250)))
     2008        Slider: None [1.0--|2.0|---250.0]
     2009        sage: sage.server.notebook.interact.automatic_control(('alpha label', (2,(1,250))))
     2010        Slider: alpha label [1.0--|2.0|---250.0]
     2011        sage: sage.server.notebook.interact.automatic_control((2, ('alpha label',(1,250))))
     2012        Slider: alpha label [1.0--|2.0|---250.0]
    19862013        sage: C = sage.server.notebook.interact.automatic_control((1,52, 5)); C
    19872014        Slider: None [1--|1|---52]
    19882015        sage: C.values()
    def automatic_control(default): 
    20252052        C = input_box(default, label=label, type=Color)
    20262053    elif isinstance(default, tuple):
    20272054        if len(default) == 2:
    2028             # The default 99.0 below is a sort of "heuristic value" so there are 100 steps
    2029             C = slider(srange(default[0], default[1], (default[1]-default[0])/99.0,
     2055            # The default 249.0 below is for a slider with length 500px, so there are 250 steps with 2px per step
     2056            C = slider(srange(default[0], default[1], (default[1]-default[0])/249.0,
    20302057                              include_endpoint=True), default = default_value, label=label)
    20312058        elif len(default) == 3:
    20322059            C = slider(default[0], default[1], default[2], default=default_value, label=label)