trek (owner)

Revisions

gist: 125922 Download_button fork
public
Public Clone URL: git://gist.github.com/125922.git
controler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
var ImagesController = Controller.init({
  // attrs
  name :"ImagesController",
  backgroundImageUrl: null,
  backgroundImageRepeat : false,
  currentPage: 1,
  imagePickerShowing: false,
  newImageShowing: true,
  image : [],
  
  // actions
  goForward: function(el){
    this.attr('currentPage', this.currentPage + 1);
  },
  goBackward: function(el){
     this.attr('currentPage', Math.max(1, this.currentPage - 1));
  },
  showImagePicker: function(el){
    this.imagePicker.css({
      'left' : this.imageInput.offset().left,
      'top' : this.imageInput.offset().top + 30
    });
    ColorsController.attr('colorPickerShowing', false);
    this.attr('imagePickerShowing', true);
  },
  hideImagePicker: function(el){
    this.attr('imagePickerShowing', false);
  },
  setCurrentImage: function(el){
    console.log(el.attr('src'))
    this.attr('backgroundImageUrl', el.attr('src'));
  },
  loadImages: function(page){
    var controller = this;
    $.getJSON('http://www.colourlovers.com/api/patterns/top?format=json&numResults=12&jsonCallback=?', {
      resultOffset : page * 12
    }, function(data){
      controller.attr('images',data)
    });
  },
  
  // value transformers
  Transforms : {
    imageGrid : function(data){
      var container = $('<div>');
      $.each(data, function(){
        $('<img>').attr({
         'src' : this.imageUrl
        }).actions({
          'click' : 'ImagesController.setCurrentImage'
        }).appendTo(container);
      });
      return container.contents();
    }
  },
});
 
var ColorsController = Controller.init({
  name : "ColorsController",
  wells : [],
  colorsHaveChanged : false,
  colorPickerShowing : false,
  colorPickerLocation: {'left' : 0, 'top' : 0},
  
  // actions
  wellFocused : function(el){
    ImagesController.attr('imagePickerShowing', false);
    this.currentWell = el;
    this.attr('colorPickerLocation', {
      'left' : el.offset().left,
      'top' : el.offset().top + 30
    });
    this.attr('colorPickerShowing', true);
    var controller = this // need access to this controller inside farbtastic callback.
    this.colorWheel.linkTo(function(color){
      controller.currentWell.val(color);
      controller.currentWell.css({
        backgroundColor: color,
        color: controller.colorWheel.hsl[2] > 0.5 ? "#000": "#fff"
      });
      var dataSlotName = 'update_items_linked_to_' + controller.currentWell.attr('id')
      // ideal, but messy
      // controller.attr(dataSlotName, color);
      controller[dataSlotName](color);
    });
  },
  hideColorPicker : function(el){
    this.attr('colorPickerShowing', false);
  },
  update_items_linked_to_profile_text_color : function(color){
    $('#home').css('color',color);
    // links that need to look like text
    $("#home #me_name, #home .stats_count, #home li.active a, #home a.definition").css('color', color);
  },
  update_items_linked_to_profile_link_color : function(color){
    $("#home a, #home a strong").css('color', color);
    // reset the :nots back to text color. LAME.
    $("#home #me_name, #home .stats_count, #home li.active a, #home a.definition").css('color', this.profile_text_color.val());
  },
  update_items_linked_to_profile_sidebar_fill_color : function(color){
    $("#side_base").css("background-color", color);
    var highlight = function(dark){
      var colors = Color.aryFromHex(dark)
      var color = '#'
      for (var i=0; i < colors.length; i++) {
        color = color + (Math.min(255, parseInt(colors[i], 16) + 24)).toString(16)
      };
      return color;
    }(color);
    $('p.promotion, li.active a').css("background-color", highlight);
  },
  update_items_linked_to_profile_sidebar_border_color : function(color){
    $("#side_base").css("border-color", color);
    $(".stats td+td, #tabMenu li, #side div.section-header h3.faq-header").css("border-color", color);
  },
  update_items_linked_to_profile_background_color : function(color){
    $('#home').css('background-color', color);
  }
});
 
Routes = {
  installationsPath: function(id){
    return Routes.themePath + '/installations';
  },
  themePath: function(id){
    return '/themes/' + id;
  },
  themesPath: function(){
    return '/themes';
  }
};
 
 
$(document).ready(function() {
  // attatchment
  new AjaxUpload($('#add'), {
    action : '/images',
    name : 'image[uploaded_data]',
    responseType: 'json',
    onComplete : function(file, response){
      // $('#new').hide();
      ImagesController.attr('newImageShowing', false);
      ImagesController.attr('existingImageShowing', true);
      ImagesController.attr('backgroundImageUrl', response.image.public_filename);
      // var bg = 'url(' + response.image.public_filename + ')';
      // $('#existing').css('background-image', bg).show();
    }
  });
  
  // colors control views
  $("#picker").outlet(ColorsController, 'colorPicker').connect('toggle', 'ColorsController.colorPickerShowing').connect('css','ColorsController.colorPickerLocation');
  $("#picker .close").actions({
    'click' : 'ColorsController.hideColorPicker'
  });
  
  ColorsController.colorWheel = $.farbtastic('#wheel');
  
  // color well views
  $('.color.well input').outlet(ColorsController, 'wells').each(function(){
    $(this).outlet(ColorsController, $(this).attr('id')).actions({
      'focus' : 'ColorsController.wellFocused'
    }).blur(function(){
      $(this).val($(this).val().toUpperCase());
    });
    
    // initializes wells with correct coloring and hide the color wheel afterwards
    $(this).focus();
    ColorsController.colorWheel.setColor($(this).val());
    ColorsController.attr('colorPickerShowing', false);
  });
  
  // views on the page who need to update when color wells update
  
  // image preview control views
  $('#image-picker').outlet(ImagesController, 'imagePicker').connect('toggle', 'ImagesController.imagePickerShowing');;
  $('#image-picker .close').actions({
    'click' : 'ImagesController.hideImagePicker'
  });
  
  $("#image-picker #new .remote").outlet(ImagesController, 'remoteImages').connect('html', 'ImagesController.images', ImagesController.Transforms.imageGrid);
  
  $('#profile_image').outlet(ImagesController, 'imageInput').blur(function(){
    $(this).val('');
  }).keyup(function(){
    $(this).val('');
  }).actions({
    'focus' : 'ImagesController.showImagePicker'
  });
  
  $('.navigation img.forward').outlet(ImagesController, 'forwardButton').actions({
    'click' : 'ImagesController.goForward'
  });
  
  $('.navigation img.back').outlet(ImagesController, 'backButton').actions({
    'click' : 'ImagesController.goBackward'
  });
  
  // render statues
  $('#timeline').hide();
  $.ajax({
    dataType: 'json',
    url: '/preview',
    
    success: function(data){
      // replace default data with actual timeline json
      if(data){
         window.timeline = data
       }
       // render from json
       $('#timeline').autoRender(window.timeline).show();
 
       // pretty dates
       var dates = $('.published').each(function(){
         $(this).html(new Date($(this).html()).time_ago_in_words());
       });
 
       // turn plain text urls into <a>
       $('.text.entry-content').each(function(){
         $(this).html($(this).html().replace(/(https?:\/\/[\w\d\.\/]+\b)/g, "<a href='$1'>$1</a>"));
         $(this).html($(this).html().replace(/@(\b[\w\d]+\b)/g, "<a href='http://www.twitter.com/$1'>@$1</a>"));
       })
    }
  });
});