summaryrefslogtreecommitdiff
path: root/modules/organize/js/organize.js
blob: 045603a4a2867071e59af3e792a07022386bdc67 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
/*
 * @todo Trap resize of dialog and resize the child areas (tree, grid and edit form)
 */
var url;
var height;

function organize_dialog_init() {
  var size = viewport_size();
  height = size.height() - 100;
  var width = size.width() - 100;

  // Deal with ui.jquery bug: http://dev.jqueryui.com/ticket/4475
  $(".sf-menu li.sfHover ul").css("z-index", 70);

  $("#gDialog").dialog("option", "width", width);
  $("#gDialog").dialog("option", "height", height);

  $("#gDialog").dialog("open");
  if ($("#gDialog h1").length) {
    $("#gDialog").dialog('option', 'title', $("#gDialog h1:eq(0)").html());
  } else if ($("#gDialog fieldset legend").length) {
    $("#gDialog").dialog('option', 'title', $("#gDialog fieldset legend:eq(0)").html());
  }

  height -= 2 * parseFloat($("#gDialog").css("padding-top"));
  height -= 2 * parseFloat($("#gDialog").css("padding-bottom"));
  height -= $("#gMicroThumbPanel").position().top;
  height -= $("#gDialog #ft").height();
  height = Math.round(height);

  $("#gMicroThumbPanel").height(height);
  $("#gOrganizeTreeContainer").height(height);

  $(".gOrganizeBranch .ui-icon").click(organize_toggle_children);
  $(".gBranchText").click(organize_open_folder);
  retrieve_micro_thumbs(item_id);
  //showLoading("#gDialog");

  $("#gMicroThumbPanel").scroll(function() {
    get_more_data();
  });

  $("#gMicroThumbSelectAll").click(function(event) {
    select_all(true);
    event.preventDefault();
  });
  $("#gMicroThumbUnselectAll").click(function(event) {
    select_all(false);
    event.preventDefault();
  });

  $("#gMicroThumbGrid").selectable({
    count: 0,
    filter: ".gMicroThumbContainer",
    selected: function(event, ui) {
      /*
       * Count the number of selected items if it is greater than 1,
       * then click won't be called so we need to remove the gSelecting
       * class in the stop event.
       */
      var count = $("#gMicroThumbGrid").selectable("option", "count") + 1;
      $("#gMicroThumbGrid").selectable("option", "count", count);
      $(ui.selected).addClass("gSelecting");
    },
    stop: function(event) {
      var count = $("#gMicroThumbGrid").selectable("option", "count");
      if (count > 1) {
        $(".gMicroThumbContainer.gSelecting").removeClass("gSelecting");
      }
      $("#gMicroThumbGrid").selectable("option", "count", 0);
    }
  });
}

function get_album_content() {
  var grid_width = $("#.gMicroThumbPanel").width();
  url = $("#gMicroThumbPanel").attr("ref");
  url = url.replace("__WIDTH__", grid_width);
  url = url.replace("__HEIGHT__", height);

  retrieve_micro_thumbs(url);
}

function retrieve_micro_thumbs() {
  var offset = $("#gMicroThumbGrid li").length;
  if (url == null) {
    var grid_width = $("#gMicroThumbPanel").width();
    url = $("#gMicroThumbPanel").attr("ref");
    url = url.replace("__WIDTH__", grid_width);
    url = url.replace("__HEIGHT__", height);
  }
  var url_data = url.replace("__OFFSET__", offset);
  url_data = url.replace("__ITEM_ID__", item_id);
  $.get(url_data, function(data) {
    $("#gMicroThumbGrid").append(data);
    get_more_data();
    $(".gMicroThumbContainer").click(function(event) {
      if ($(this).hasClass("gSelecting")) {
        $(this).removeClass("gSelecting");
      } else {
        $(this).removeClass("ui-selected");
      }
    });
    $(".gMicroThumbContainer").draggable({
      cancel: ".gMicroThumbContainer:not(.ui-selected)",
      handle: ".gMicroThumbContainer.ui-selected",
      zindex: 2000,
      helper: function(event, ui) {
        $("body").append("<div id=\"gDragHelper\"><ul></ul></div>");
        var beginTop = event.pageY;
        var beginLeft = event.pageX;
        var zindex = $(".gMicroThumbContainer").draggable("option", "zindex");
        $("#gDragHelper").css('top', event.pageY - 22.5);
        $("#gDragHelper").css('left', event.pageX + 22.5);
        var placeHolder = $(this).clone();
        $(placeHolder).attr("id", "gPlaceHolder");
        $(placeHolder).css("visibility", "hidden");
        $(placeHolder).removeClass("ui-selected");
        $(placeHolder).removeClass("ui-draggable");
        $(this).after(placeHolder);

        $("li.ui-selected").each(function(i) {
          var clone = $(this).clone();
	  $(clone).attr("id", "drag_clone_" + $(this).attr("ref"));
          $("#gDragHelper ul").append(clone);
          $(clone).css("position", "absolute");
          $(clone).css("top", beginTop);
          $(clone).css("left", beginLeft);
          $(clone).css("z-index", zindex--);
          $(this).hide();

          var children = $(clone).find(".gMicroThumb .gThumbnail");
          var width = new String(children.css("width")).replace(/[^0-9]/g,"") * .5;
          height = new String(children.css("height")).replace(/[^0-9]/g,"") * .5;
          var marginTop = new String(children.css("margin-top")).replace(/[^\.0-9]/g,"") * .5;
          children.attr("width", width);
          children.attr("height", height);
          children.css("margin-top", marginTop);
          if (i < 9) {
            beginTop -= 5;
            beginLeft += 5;
          }
        });
        return $("#gDragHelper");
      },
      stop: function(event, ui) {
        $("#gDragHelper li").each(function(i) {
	  $("#gPlaceHolder").before($("#thumb_" + $(this).attr("ref")).show());
        });
        $(".gMicroThumbContainer.ui-selected").css("z-index", null);
        $("#gDragHelper").remove();
        $("#gPlaceHolder").remove();
      }
    });
    $(".gMicroThumbContainer").droppable( {
      tolerance: "pointer",
      over: function(event, ui) {
        $(this).after($("#gPlaceHolder"));
      },
      drop: function(event, ui) {
        $("#gDragHelper li").each(function(i) {
	  $("#gPlaceHolder").before($("#thumb_" + $(this).attr("ref")).show());
        });
        $(".gMicroThumbContainer.ui-selected").css("z-index", null);
        $("#gDragHelper").remove();
        $("#gPlaceHolder").remove();
      }
    });
  });
}

function get_more_data() {
  var element = $("#gMicroThumbPanel").get(0);
  var scrollHeight = element.scrollHeight;
  var scrollTop = element.scrollTop;
  var height = $("#gMicroThumbPanel").height();
  var scrollPosition = scrollHeight - (scrollTop + height);
  if (scrollPosition > 0 && scrollPosition <= 100) {
    retrieve_micro_thumbs();
  }
}

function organize_toggle_children(event) {
  var id = $(this).attr("ref");
  var span_children = $("#gOrganizeChildren-" + id);
  if ($(this).hasClass("ui-icon-plus")) {
    $(this).removeClass("ui-icon-plus");
    $(this).addClass("ui-icon-minus");
    $("#gOrganizeChildren-" + id).removeClass("gBranchCollapsed");
  } else {
    $(this).removeClass("ui-icon-minus");
    $(this).addClass("ui-icon-plus");
    $("#gOrganizeChildren-" + id).addClass("gBranchCollapsed");
  }
  event.preventDefault();
}

function organize_open_folder(event) {
  var selected = $(".gBranchSelected");
  if ($(selected).attr("id") != $(this).attr("id")) {
    $(selected).removeClass("gBranchSelected");
    $(this).addClass("gBranchSelected");
    item_id = $(this).attr("ref");
    $("#gMicroThumbGrid").empty();
    retrieve_micro_thumbs();
  }
  event.preventDefault();
}

function select_all(select) {
  if (select) {
    $(".gMicroThumbContainer").addClass("ui-selected");
    $("#gMicroThumbSelectAll").hide();
    $("#gMicroThumbUnselectAll").show();
  } else {
    $(".gMicroThumbContainer").removeClass("ui-selected");
    $("#gMicroThumbSelectAll").show();
    $("#gMicroThumbUnselectAll").hide();
  }
}

function viewport_size() {
  return {
      width : function() {
        return window.innerWidth
          || document.documentElement && document.documentElement.clientWidth
          || document.body.clientWidth;
      },
      height : function() {
        return window.innerHeight
          || document.documentElement && document.documentElement.clientHeight
          || document.body.clientHeight;
      }
  };
}